简体   繁体   English

ASP.NET MVC - 控制器使用 Html.BeginForm() 从视图接收列表错误

[英]ASP.NET MVC - Controller receives the list incorret from view using Html.BeginForm()

I want to get multiple instances of my model objects sent to the controller as a list but for some reason, the count of the list is always 1 and the value of the object is always 0 so nothing is actually posted when submit is pressed.我想将模型对象的多个实例作为列表发送到控制器,但由于某种原因,列表的计数始终为 1,对象的值始终为 0,因此按下提交时实际上没有发布任何内容。

This is my View:这是我的观点:

@model List<MVCModel.Models.Student>

 <b>Items</b>    @ViewBag.Items <br />
 <b>Value</b>    @ViewBag.Value <br />
  
@using (Html.BeginForm("Form", "Home", FormMethod.Post))
{
<table>
    <tbody id="GenaratorList">
        <tr>
          @{
            int counter = 0;
            <td>Enter Name: </td>
            <td>@Html.TextBoxFor(m => m[counter].Name, new {id = "Name"})</td>
            <td>Enter Age: </td>
            <td>@Html.TextBoxFor(m => m[counter].Age, new {id = "Age"})</td>
            <td><input type="button" value="Add" onclick="addGenarator()" /></td>
        </tr>
        counter++;
        }
     <tbody>
</table>
<input type="submit" value="Submit Form"/>
}

<script>
    count = 0;
    function addGenarator() {
        var Name = $('#Name').val();
        var Age = $('#Age').val();
        var Student = "studentModels[" + count + "]";
        $('#GenaratorList').append('<tr><td><input id="' + Student + '.Name" Name="' + Student + '.Name" type="text" class="form-control" readonly value="' + Name + '"></td><td><input id="' + Student + '.Age" Age="' + Student + '.Age" type="text" class="form-control" readonly value="' + Age + '"></td></tr>');
        $('#Name,#Age').val('');
        count++;
    }
</script>

This is my Controller:这是我的控制器:

[HttpPost]
public ActionResult Form(List<Student> s)
{
    ViewBag.Items = s.Count; // ALWAYS RETURNS 1
    ViewBad.Value = s[0].Age // TEST VALUE OF FIRST ITEM RETURNS 0
    return View("Index");
}

This is my model:这是我的模型:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MVCModel.Models
{
    public class Student
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }
}

Your Form (HttpPost) API was expected to receive a request body with an array named s .您的Form (HttpPost) API 应该会收到一个请求正文,其中包含一个名为s的数组。 You will pass the array as (name) studentModels based on your JS code.您将根据您的 JS 代码将数组作为(名称) studentModels传递。

var Student = "studentModels[" + count + "]";

And you miss out on the name attribute for each Age input.而且您错过了每个 Age 输入的 name 属性。

<input id="' + Student + '.Age" Age="' + Student + '.Age" type="text" class="form-control" readonly value="' + Age + '">

To fix, make sure the array name in View must be matched with the Controller parameter name.要解决此问题,请确保 View 中的数组名称必须与 Controller 参数名称匹配。 For the below code, I name the array as s which is the same as the Controller API expected parameter name.对于下面的代码,我将数组命名为s ,这与 Controller API 预期的参数名称相同。

Also, apply with template literals and string interpolation to make the HTML string looks clean and easier to bind the values with variables.此外,应用模板文字和字符串插值以使 HTML 字符串看起来干净,并且更容易将值与变量绑定。

function addGenarator() {
    var Name = $('#Name').val();
    var Age = $('#Age').val();
    var Student = "s[" + count + "]";

    var html = `
        <tr>
            <td><input id="${Student}.Name" name="${Student}.Name" type="text" class="form-control" readonly value="${Name}" /></td>
            <td><input id="${Student}.Age" name="${Student}.Age" type="text" class="form-control" readonly value="${Age}" /></td>
        </tr>
    `;
    $('#GenaratorList').append(html);
    $('#Name,#Age').val('');
    count++;
}

Submitted form & Request Form提交表格和申请表格

在此处输入图像描述

Debugging Output调试输出

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 JavaScript 从 Html.BeginForm() ASP.NET MVC 获取值 - How to get values using JavaScript from Html.BeginForm() ASP.NET MVC ASP.NET MVC 3覆盖Html.BeginForm - ASP.NET MVC 3 override Html.BeginForm 从 Html.BeginForm ASP.NET MVC 调用两个动作 - Call two actions from Html.BeginForm ASP.NET MVC ASP.NET MVC在Html.BeginForm提交之前动态保存数据 - ASP.NET MVC dynamicaly save data prior to Html.BeginForm submission ASP.Net MVC 5 Html.BeginForm onsubmit和具有必需属性的模型 - ASP.Net MVC 5 Html.BeginForm onsubmit and model with required attribute 如何在ASP.NET MVC中使用Razor View从对象列表中获取控制器中的单个对象 - How to get Single Object in Controller from List of Objects using Razor View in ASP.NET MVC ASP.NET Core MVC controller 接收 null 来自 Z2705A83A5A0659CCE34583972 调用的输入参数 - ASP.NET Core MVC controller receives null for input parameter from ajax call 如何从 AJAX 方法返回列表以从 controller ASP.NET MVC 查看 - How to return a list from AJAX method to view from controller ASP.NET MVC 在 ASP.NET MVC 中将 HTML 表单数据从视图传递到控制器 - Passing HTML form data from View to Controller in ASP.NET MVC 如何使用html.beginform从服务器端获取结果? - how to get the result from the server side using the html.beginform?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM