MVC 4列表模型向控制器返回null [英] MVC 4 List Model returning null to the controller

查看:78
本文介绍了MVC 4列表模型向控制器返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

模型

public class InformationsModel
{
    public List<InformationModel> infos { get; set; }

    public InformationsModel()
    {
    }
}

public class InformationModel
{
    public InformationModel() {

    }

    public string Name { get; set; }
    public string Value { get; set; }
    public string Group { get; set; }
    public string Type { get; set; }
    public bool Avaiable { get; set; }    
}

查看

@model InterfaceWeb.Models.InformationsModel


@using (Html.BeginForm("UpdateInformations", "Main", FormMethod.Post, new { @id = "frmInformations" }))
{    
    @Html.EditorFor(x => x.infos)                
}

模板

@model InterfaceWeb.Models.Information.InformationModel

<div class="infoMain">
    <div class="colunas">
        @Html.TextBoxFor(x => x.Name, new { style = "width:250px;" })
    </div>
    <div class="colunas">
            @Html.TextBoxFor(x => x.Value, new { style = "width:120px;" })
    </div>
    <div class="colunas">
        @Html.TextBoxFor(x => x.Group, new { style = "width:120px;" })
    </div>
    <div class="colunas">
        @Html.TextBoxFor(x => x.Type, new { style = "width:150px;" })
    </div>
    <div class="colunas">
        @Html.CheckBoxFor(x => x.Avaiable, new { style = "width:10px; margin-left:20px;" })
    </div>
</div>

控制器

[HttpPost]
public ActionResult UpdateInformations(InformationsModel infos)
{                

}

当我到达控制器时,我的模型是空的,我不知道为什么.

And when I reach the controller, my model is empty and I can't figure out why.

我试图更改模板,视图,在之后,之前初始化列表,但是没有任何效果.

I tried to change the template, the view, initialize the list after, before, but nothing worked..

谢谢您的帮助! =)

推荐答案

由于模型前缀的工作方式,您遇到了此问题.具体来说,问题出在这里:

You're experiencing this problem because of the way model prefixes work. Specifically, the problem is here:

[HttpPost]
public ActionResult UpdateInformations(InformationsModel infos)
{             

}

如果您查看模板生成的HTML,您会发现它遵循以下原则:

If you take a look at the HTML that your template generates, you'll see that it's along the lines of this:

<input id="infos_0__Name" name="infos[0].Name" type="text" value="" />

在这种情况下,要知道的重要一点是infos是与InformationsModel.infos属性的值关联的前缀.通过在控制器infos中也命名参数,就可以摆脱模型绑定器.您只需要重命名它即可获取值,就像这样:

In this case, the important thing to know is that infos is the prefix associated with the values of your InformationsModel.infos property. By also naming the parameter in your controller infos, you're throwing off the model binder. You simply need to rename it to pick up the values, like so:

[HttpPost]
public ActionResult UpdateInformations(InformationsModel model)
{             

}

顺便说一句,我建议将InformationModel重命名为更合适的名称,例如PersonEmployee或您要建模的任何名称.遵循您的尝试有点困难,因为类名几乎是相同的,并且这些名称实际上并未传达其意图.

As an aside, I'd recommend renaming InformationModel to something more suitable, like Person, Employee or whatever it is you're modelling. It was a little difficult following what you were trying to do, just because the class names are almost identical and the names don't really convey their intent.

这篇关于MVC 4列表模型向控制器返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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