控制器总是使用html.beginform()ASP.NET MVC从视图中null null

发布于 2025-02-09 03:15:11 字数 2099 浏览 3 评论 0原文

我想将多个模型对象的多个实例作为列表发送给控制器,但是由于某种原因,我总是会得到无效的例外,而在按下提交时实际上没有任何发布。

我的文本框正常工作,但是我找不到这些值或如何将这些值发送到控制器以及为什么我的代码不起作用的原因。

这是我的观点:

@model MVCModel.Models.Student

 <b>Result</b>    @ViewBag.s <br />
  
@using (Html.BeginForm("Form", "Home", FormMethod.Post))
{
<table>
    <tbody id="GenaratorList">
        <tr>
            <td>Enter Name: </td>
            <td>@Html.TextBoxFor(m => m.Name)</td>
            <td>Enter Age: </td>
            <td>@Html.TextBoxFor(m => m.Age)</td>
            <td><input type="button" value="Add" onclick="addGenarator()" /></td>
        </tr>
     <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>

这是我的控制器:

[HttpPost]
        public ActionResult Form(List<Student> s)
        {
          
            return View("Index");
        }

这是我的模型:

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; }
    }
}

I want to get multiple instances of my model objects sent to the controller as a list but for some reason, I always get the null exception where nothing is actually posted when submit is pressed.

My textbox works fine, but I can't find why or how these values are sent to the controller and why my code doesn't work.

This is my View:

@model MVCModel.Models.Student

 <b>Result</b>    @ViewBag.s <br />
  
@using (Html.BeginForm("Form", "Home", FormMethod.Post))
{
<table>
    <tbody id="GenaratorList">
        <tr>
            <td>Enter Name: </td>
            <td>@Html.TextBoxFor(m => m.Name)</td>
            <td>Enter Age: </td>
            <td>@Html.TextBoxFor(m => m.Age)</td>
            <td><input type="button" value="Add" onclick="addGenarator()" /></td>
        </tr>
     <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)
        {
          
            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; }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文