简体   繁体   English

Model 列表属性为 NULL 发布在 controller

[英]Model List property is NULL on Post in controller

So I have tested on a clean project an issue I've been getting, and have done the following code setup for checking if a custom Class object still returns null placed in a List:所以我在一个干净的项目上测试了一个我遇到的问题,并完成了以下代码设置来检查自定义 Class object 是否仍然返回 null 放置在列表中:

VIEW看法

<div>
<div class="jumbotron">

    <h1 class="display-4"><span class="fas fa-user-secret"></span> Babcock Canada - Application Template</h1>
    <br />
    <div class="alert alert-primary" role="alert">
        <span class="fas fa-info-circle"></span>
        <span> This is the Babcock Canada MVC Application template for use in developing content-rich web applications.</span>
    </div>
    <hr class="my-4" />
</div>

<div>

    <div class="container-fluid">


        @if (!string.IsNullOrEmpty(Model.errorMessage))
        {
            <div class="alert alert-danger" role="alert">
                <span class="fas fa-stop-circle"></span> @Html.DisplayFor(alert => alert.errorMessage)
            </div>
        }
        @if (!string.IsNullOrEmpty(Model.successMessage))
        {
            <div class="alert alert-success" role="alert">
                <span class="fas fa-check-circle"></span> @Html.DisplayFor(alert => alert.successMessage)
            </div>
        }

        <div>
            @using (Html.BeginForm("TestAction", "Default", FormMethod.Post))
            {
                @Html.HiddenFor(m => Model.tester[0].tester)
                <button type="submit" class="btn btn-primary">
                    Submit 1
                </button>
            }
        </div>
        <div>
            @using (Html.BeginForm("TestAction", "Default", FormMethod.Post))
            {
                @Html.HiddenFor(m => Model.tester[1].tester)
                <button type="submit" class="btn btn-primary">
                    Submit 2
                </button>
            }
        </div>

    </div>
</div>

TestClass.cs测试类.cs

namespace Test.Models
{
    public class TestClass
    {
        public string tester { get; set; }
    }
}

MODEL MODEL

namespace Test.Models
{
/// <summary>
/// This is the default template model.
/// </summary>
public class DefaultModel : SharedModel
{
    public string errorMessage = string.Empty;
    public string successMessage = string.Empty;

    public List<TestClass> tester { get; set; }

    public DefaultModel()
    {

    }

    public void Init()
    {
        tester = new List<TestClass>
                 {
                     new TestClass { tester = "Testing..." },
                     new TestClass { tester = "Testing2..." }
                 };
    }
}
}

CONTROLLER CONTROLLER

[HttpPost]
    public ActionResult TestAction(DefaultModel model)
    {
        return View(model);
    }

So the result is that the second one returns NULL in the list, but the first one returns just fine.所以结果是第二个返回列表中的 NULL,但第一个返回就好了。

In my other project index 0 of a list looped in the same way returns the error: "An item with the same key has already been added."在我的另一个项目中,以相同方式循环的列表的索引 0 返回错误:“已添加具有相同键的项目。”

So what am I doing wrong?那么我做错了什么?

try using the helper Html.Hidden() instead尝试使用助手Html.Hidden()代替

    <div>
        @using (Html.BeginForm("TestAction", "Default", FormMethod.Post))
        {
            @Html.Hidden("tester", Model.tester[0].tester)
            <button type="submit" class="btn btn-primary">
                Submit 1
            </button>
        }
    </div>
    <div>
        @using (Html.BeginForm("TestAction", "Default", FormMethod.Post))
        {
            @Html.Hidden("tester", Model.tester[1].tester)
            <button type="submit" class="btn btn-primary">
                Submit 2
            </button>
        }
    </div>

Turns out it doesn't work when the form is inside the loop, has to be outside and reference an ID value through the submit button holding the name "ID" and the value of that list item.事实证明,当表单在循环内部时它不起作用,必须在外部并通过包含名称“ID”的提交按钮和该列表项的值引用一个 ID 值。

Suppose using Html.Hidden() would work, but that isn't what was required for the project.假设使用Html.Hidden()可以工作,但这不是项目所需要的。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM