2

这是我的代码:

楷模

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)
{                

}

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

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

感谢您的帮助!=)

4

2 回答 2

2

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

[HttpPost]
public ActionResult UpdateInformations(InformationsModel infos)
{             

}

如果您查看您的模板生成的 HTML,您会发现它是这样的:

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

在这种情况下,重要的是要知道与您的属性infos值相关联的前缀。InformationsModel.infos通过在你的控制器中命名参数infos,你就抛弃了模型绑定器。您只需重命名它即可获取值,如下所示:

[HttpPost]
public ActionResult UpdateInformations(InformationsModel model)
{             

}

顺便说一句,我建议重命名InformationModel为更合适的名称,例如PersonEmployee或者您正在建模的任何内容。遵循您尝试做的事情有点困难,只是因为类名几乎相同,而且名称并没有真正传达他们的意图。

于 2013-09-30T16:25:16.240 回答
0

您的问题可能与事物的命名有关:)

更改您的控制器:

[HttpPost] public ActionResult UpdateInformations(InformationsModel infos) { }

和:

[HttpPost] public ActionResult UpdateInformations(InformationsModel model) { }

或在您的模型更改列表名称“infos”中更改为其他名称。

例子:

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

    public InformationsModel()
    {
    }
}

更改为:

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

    public InformationsModel()
    {
    }
}
于 2018-04-12T07:02:25.277 回答