在以下代码中,提交表单时,所选国家/地区的ID正确传递回控制器。但是,我也想传递国家名称。如何将名称和ID一起发送回控制器?
模型
public class MyModel
{
public Country Country { get; set; }
public List<Country> CountryList { get; set; }
}
public class Country
{
public int? ID { get; set; }
public string Name { get; set; }
}
查看
@using (Html.BeginForm())
{
<div>
@Html.DropDownListFor(m => m.Country.ID,
new SelectList(Model.CountryList, "ID", "Name"))
</div>
}
控制器
MyModel model = new MyModel();
public ActionResult Index()
{
using (var service = new Service())
{
model.CountryList = service.GetCountries();
return View(model);
}
}
public ActionResult Index(MyModel tmpModel)
{
return RedirectToAction("Index");
}