通过下面的设置,所有的国籍都被发送回服务器,我真的不关心整个列表,只关心选定的值。如何修改我的代码,以便只有选定的国籍与模型的其余部分一起发回?
模型:
// SearchCriteria model
public class SearchCriteria()
{
public SearchCriteria()
{
Nationalities = new List<SelectListItem>();
Nationalities.Add(new SelectListItem { Text = "Any Nationality", Value = "Any Nationality", Selected = true });
Nationalities.Add(new SelectListItem { Text = "United States", Value = "United States", Selected = false });
Nationalities.Add(new SelectListItem { Text = "Japanese", Value = "Japanese", Selected = false });
Nationalities.Add(new SelectListItem { Text = "Mexican", Value = "Mexican", Selected = false });
Nationalities.Add(new SelectListItem { Text = "Canadidan", Value = "Canadidan", Selected = false });
Nationalities.Add(new SelectListItem { Text = "Chinese", Value = "Chinese", Selected = false });
}
public string Name {get;set;}
public string Nationality { get; set; }
public List<SelectListItem> Nationalities { get; set; }
}
控制器:
[HttpPost]
public ActionResult Find(SearchCriteria model)
{
try
{
// find it
return View(model);
}
catch
{
return View(model);
}
}
看法:
@Html.Label("Name:");
@Html.TextBoxFor(model => model.Name);
@Html.Label("Nationality:");
@Html.DropDownListFor(model => model.Nationality, Model.Nationalities);