MVC 4 List Model returning null to the controller

2019-07-16 04:49发布

Here's my code:

Models

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

View

@model InterfaceWeb.Models.InformationsModel


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

Template

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

Controller

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

Thx for any help! =)

2条回答
Deceive 欺骗
2楼-- · 2019-07-16 05:47

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

[HttpPost]
public ActionResult UpdateInformations(InformationsModel infos)
{             

}

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="" />

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

}

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.

查看更多
虎瘦雄心在
3楼-- · 2019-07-16 05:51

Your problem is probably with naming of things :)

Change your controller:

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

With:

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

OR in your model change list name "infos" into something else.

Example:

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

    public InformationsModel()
    {
    }
}

Change with:

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

    public InformationsModel()
    {
    }
}
查看更多
登录 后发表回答