对于MVC 4中带有控件和提交表单(Html.BeginForm)的每个循环 [英] For each loop with controls and submitting form (Html.BeginForm) in MVC 4

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

问题描述

在我的视图模型中,我有一个对象列表.我迭代这些对象,并为每个对象创建控件.在下面的情况下,我想向人们展示每个对象的文本框和按钮.当用户点击按钮时,发布一个帖子,我可以将数据保存在我的控制器中.

在 UI 中,用户可以更改他们想要的表单,然后单击保存.

我的问题是模型在发布到控制器时为空..

我的 Razor 代码:

 using (Html.BeginForm()){foreach(Model.Contributor 中的 var 贡献者){@Html.HiddenFor(model =>contributor.Id)<div class="formrow">@Html.ValidationSummary(true)

<h2>@Html.TextRaw("AuthorInfo", "Author")</h2><div class="formrow">@Html.EditorFor(model =>contributor.FirstName)<div class="formvalidation">@Html.ValidationMessageFor(model =>contributor.FirstName)

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

}}

我的视图模型代码

公共类 ProfileModel{公共字符串消息 { 获取;放;}公共列表<PublisherModel>出版商{得到;放;}公共列表<贡献者模型>贡献者{得到;放;}公共贡献者模型新贡献者 { 获取;放;}}

我的控制器代码

[HttpPost]公共 ActionResult Mine(ProfileModel 模型,字符串 newuser){//}

如何解决?

我想我必须扩展我的视图模型,以某种方式存储更改.但我真的看不出来.

现在 ProfileModel 中的所有属性在它到达控制器时都是空的.

有什么想法吗?

解决方案

基本上问题是默认模型绑定器无法在 foreach 循环中正确绑定集合项.换句话说,它错误地命名了元素,这就是集合在参数中显示为 null 的原因.

我确定有各种不同的解决方法、帮助程序和东西,但我不熟悉这些,所以我只使用 for 循环而不是 foreach,这样元素就可以正确命名.

试试这个:

 @for (int i = 0; i  Model.Contributor[i].Id)<div class="formrow">@Html.ValidationSummary(true)

<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 class="formrow right"><input type="hidden" name="formsubmitted" value="true"/><input type="submit" class="button" value="@Html.Text("ButtonText", "Save")"/>

}

我建议您使用调试工具来查看元素是否具有正确的名称属性,在您的情况下,它们应该类似于 Contributor[0].Id、Contributor[0].FirstName 等.

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..

My Razor 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>
            }
        }

My view model 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; }
}

My controller code

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

How to fix it?

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.

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

Any ideas?

解决方案

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.

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.

Try this:

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

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屋!

查看全文
相关文章
C#/.NET最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆