简体   繁体   English

DropDownListFor SelectList的模型未绑定到HttpPost

[英]Model with DropDownListFor SelectList not binding on the HttpPost

When I have the following code and I submit the form my post action shows the Contact object as null. 当我有以下代码并提交表单后,我的发布操作会将Contact对象显示为null。 If I remove the DropDownListFor from the view the Contact object contains the expected information (FirstName). 如果我从视图中删除DropDownListFor,则Contact对象包含预期的信息(FirstName)。 Why? 为什么? How do I get the SelectList value to work? 如何获取SelectList值以起作用?

My classes: 我的课程:

public class ContactManager
{       
    public Contact Contact { get; set; }       
    public SelectList SalutationList { get; set; }
}
public class Contact
{
    public int Id{get;set;}
    public string FirstName{get; set;}
    public SalutationType SalutationType{get; set;}
}
 public class SalutationType
{      
    public int Id { get; set; }
    public string Name { get; set; }      
}

My view: 我的观点:

@model ViewModels.ContactManager

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)
    @Html.HiddenFor(model => model.Contact.Id)
    @Html.DropDownListFor(model => model.Contact.SalutationType.Id, Model.SalutationList, "----", new { @class = "form-control" })
    @Html.EditorFor(model => model.Contact.FirstName)
    <input type="submit" value="Save" />
}

My Controller: 我的控制器:

public ActionResult Edit(int? id)
{
    Contact contact = db.Contacts.FirstOrDefault(x => x.Id == id);
    ContactManager cm = new ContactManager();
    cm.Contact = contact;
    cm.SalutationList = new SelectList(db.SalutationTypes.Where(a => a.Active == true).ToList(), "Id", "Name");
    return View(cm);
}
[HttpPost]
public ActionResult Edit(ContactManger cm)
{
//cm at this point is null
    var test = cm.Contact.FirstName;
    return View();
}

You will pass the DropdownList using ViewBag: 您将使用ViewBag传递DropdownList:

ViewBag.SalutationList = new SelectList(db.SalutationTypes.Where(a => a.Active == true).ToList(), "Id", "Name");

than u have to call this list inside your edit view: 您不必在编辑视图中调用此列表:

 @Html.DropDownList("SalutationList",String.Empty)

The problem is that the DefaultModelBinder won't be able to map nested models properly if you use a different parameter name. 问题是,如果您使用其他参数名称,则DefaultModelBinder将无法正确映射嵌套模型。 You must use the same parameter name as the model name. 您必须使用与模型名称相同的参数名称。

public ActionResult Edit(ContactManager contactManager)

As a general practice, always use the name of the model as the parameter name to avoid mapping problems. 通常,请始终将模型名称用作参数名称,以避免映射问题。

Further Suggestion: 进一步建议:

You can just use Contact as the parameter model, no need to use ContactManager if you only need the contact model. 您可以仅将Contact用作参数模型,如果只需要Contact模型,则无需使用ContactManager

[HttpPost]
public ActionResult Edit(Contact contact)
{
    var test = contact.FirstName;
    return View();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM