对于在MVC 4控制和提交表格(Html.BeginForm)每个循环 [英] For each loop with controls and submitting form (Html.BeginForm) in MVC 4

查看:159
本文介绍了对于在MVC 4控制和提交表格(Html.BeginForm)每个循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的视图模型,我有对象的列表。我遍历这些对象,并为他们每个人创建控件。在下面的情况,我想向人们展示一个文本框,并为每个对象的按钮。当用户点击该按钮,后制成,我可以在我的控制器保存数据。

In my view model, I have a list of objects. I iterate these objects, and create controls for each of them. In the situation below, I want to show people a textbox and a button for each object. When the user clicks the button, a post is made, and I can save the data in my controller.

在用户界面中,用户可以改变他们想要的形式,并点击保存。

In the UI, a user can change the form they want, and click save.

我的问题是模型为空时,它贴到控制器。

My problem is the model is null when it's posted to the controller..

我的剃须刀code

       using (Html.BeginForm())
        {
            foreach (var contributor in Model.Contributor)
            {

        @Html.HiddenFor(model => contributor.Id)

        <div class="formrow">
            @Html.ValidationSummary(true)
        </div>

        <h2>@Html.TextRaw("AuthorInfo", "Author")</h2>
        <div class="formrow">

            @Html.EditorFor(model => contributor.FirstName)
            <div class="formvalidation">
                @Html.ValidationMessageFor(model => contributor.FirstName)
            </div>
        </div>


        <div class="formrow right">
            <input type="hidden" name="formsubmitted" value="true" />
            <input type="submit"  class="button" value="@Html.Text("ButtonText", "Save")" />
        </div>
            }
        }

我的视图模型code

public class ProfileModel
{
    public string Message { get; set; }
    public List<PublisherModel> Publisher { get; set; }
    public List<ContributorModel> Contributor { get; set; }

    public ContributorModel NewContributor { get; set; }
}

我的控制器code

[HttpPost]
public ActionResult Mine(ProfileModel model, string newuser)
{
    //
}

如何解决?

我想我必须扩大我的看法模型的方式来存储某种方式的变化。但我真的看不出。

I guess I have to expand my view model with a way to store the changes in some way. But I really can't see how.

现在在ProfileModel所有属性为空,当它到达控制器。

Right now all the properties in the ProfileModel is null when it reaches the controller.

任何想法?

推荐答案

基本上问题是,默认的模型绑定无法在foreach循环正确绑定藏品。换句话说,它的名字元素错误,这就是为什么收集显示为空值的参数。

Basically the problem is that default model binder is unable to bind collection items correctly in foreach loop. In other words, it names the element incorrectly and that's why the collection displays as null in parameters.

我敢肯定,有各种不同的解决方法,助手之类的东西,但我不熟悉这些,所以我只是使用的循环,而不是的foreach,这样的元素是正确命名。

I'm sure there are all kinds of different workarounds, helpers and stuff but I'm not familiar with those, so I just use for loop instead of foreach, this way the elements are named correctly.

试试这个:

    @for (int i = 0; i < Model.Contributor.Count(); i++)
    {

        @Html.HiddenFor(model => Model.Contributor[i].Id)

        <div class="formrow">
            @Html.ValidationSummary(true)
        </div>

        <h2>@Html.TextRaw("AuthorInfo", "Author")</h2>
        <div class="formrow">

            @Html.EditorFor(model => Model.Contributor[i].FirstName)
            <div class="formvalidation">
                @Html.ValidationMessageFor(model => Model.Contributor[i].FirstName)
            </div>
        </div>


        <div class="formrow right">
            <input type="hidden" name="formsubmitted" value="true" />
            <input type="submit"  class="button" value="@Html.Text("ButtonText", "Save")" />
        </div>
    }

我建议你使用的调试工具,看是否元素有正确的名称属性,在你的情况下,他们应该像贡献者[0] .ID,投稿[0] .FirstName等。

I suggest you to use a debugging tool to see if elements have correct name attribute, in your case they should look like Contributor[0].Id, Contributor[0].FirstName etc.

这篇关于对于在MVC 4控制和提交表格(Html.BeginForm)每个循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆