ASP.NET MVC post a dropdownlist
When I post via the click on "btSave", in the controller I receive the model. In the model, I see the value off all fields (I show here only "FirstName" but there are others). But the doropdownlist value are null all the time.
Do you have an idea why ?
Thanks,
//Dropfown content
public class LkpTypeCompany
{
public virtual int Id { get; set; }
public virtual int Code { get; set; }
public virtual string NL { get; set; }
public virtual string FR { get; set; }
public virtual string Value { get; set; }
}
//Model Definition
public class CustomerModel
{
public List<LkpTypeCompany> LkpTypeCompany { get; set; }
public Customer Customer { get; set; }
}
//Posting开发者_运维知识库 form
jQuery('#btGeneralSave').click(function (event) {
var jqxhr = $.post("Controller/Actio,", $("form").serialize(),
function (data) {
});
});
//HTML
@model eSIT.GC.WebUI.Models.CustomerModel
@using (Html.BeginForm())
{
@Html.TextBoxFor(m => m.Customer.FirstName, new { maxlength = 50 })
@Html.DropDownListFor( m => m.Customer.LkpTypeCompany, new SelectList(Model.LkpTypeCompany, "Code", "FR", Model.Customer.LkpTypeCompany.Code))
<input type="button" id="btSave" value="Save"/>
}
I see the overload that you were attempting to use but I have had good luch with using SelectListItem
Try
@Html.DropDownListFor( m => m.Customer.LkpTypeCompany,
new SelectList(Model.LkpTypeCompany
.Select(i => new SelectListItem
{
Text = i.Code,
Value = (*somecondition*) ? i.FR : i.NL,
Selected = i.Code == Model.Customer.LkpTypeCompany.Code
}));
精彩评论