鉴于我有一个模型对象像...
public class MyModel { public int SomeProperty { get; set; } public int SomeOtherProperty { get; set; } public IList<DeliveryDetail> DeliveryDetails { get; set; } } public DeliveryDetail { public string Description { get; set; } public bool IsSelected { get; set; } }我将它传递给像这样的视图...
// Controller public ActionResult Index() { MyModel myModel = Factory.CreateModelWithDeliveryDetails(x); return View(myModel); }我将如何呈现/绑定一组单选按钮(在视图中)? 使用下面的代码不会发回数据:
@foreach(var deliveryDetail in @Model.DeliveryDetails) { @deliveryDetail.Description @Html.RadioButtonFor(x => deliveryDetail, false) }Given I have a Model object like ...
public class MyModel { public int SomeProperty { get; set; } public int SomeOtherProperty { get; set; } public IList<DeliveryDetail> DeliveryDetails { get; set; } } public DeliveryDetail { public string Description { get; set; } public bool IsSelected { get; set; } }and I pass it through to a View like this ...
// Controller public ActionResult Index() { MyModel myModel = Factory.CreateModelWithDeliveryDetails(x); return View(myModel); }How would I render / bind a set of radio buttons (in the view)? Using the following code doesn't post the data back:
@foreach(var deliveryDetail in @Model.DeliveryDetails) { @deliveryDetail.Description @Html.RadioButtonFor(x => deliveryDetail, false) }最满意答案
单选按钮列表中的选择是互斥的。 您只能选择一个值。 因此,将单选按钮列表绑定到IEnumerable类型的属性没有任何意义。 您可能需要根据视图的要求调整视图模型(在您的情况下显示的是单选按钮列表,其中只能进行单个选择)。 如果您使用了复选框列表,绑定到IEnumerable属性会有意义,因为您可以检查多个复选框。
所以,让我们根据这种情况调整视图模型:
模型:
public class MyModel { public string SelectedDeliveryDetailId { get; set; } public IList<DeliveryDetail> DeliveryDetails { get; set; } } public class DeliveryDetail { public string Description { get; set; } public int Id { get; set; } }控制器:
public class HomeController : Controller { public ActionResult Index() { var model = new MyModel { DeliveryDetails = new[] { new DeliveryDetail { Description = "detail 1", Id = 1 }, new DeliveryDetail { Description = "detail 2", Id = 2 }, new DeliveryDetail { Description = "detail 3", Id = 3 }, } }; return View(model); } [HttpPost] public ActionResult Index(MyModel model) { // Here you will get the id of the selected delivery detail // in model.SelectedDeliveryDetailId ... } }视图:
@model MyModel @using (Html.BeginForm()) { foreach (var deliveryDetail in Model.DeliveryDetails) { @deliveryDetail.Description @Html.RadioButtonFor(x => x.SelectedDeliveryDetailId, deliveryDetail.Id) } <button type="submit">OK</button> }Selections in a radio button list are mutually exclusive. You can select only a single value. So binding a radio button list to a property of type IEnumerable doesn't make any sense. You probably need to adapt your view model to the requirements of the view (which in your case is displaying a radio button list where only a single selection can be made). Had you used a checkbox list, binding to an IEnumerable property would have made sense as you can check multiple checkboxes.
So let's adapt the view model to this situation:
Model:
public class MyModel { public string SelectedDeliveryDetailId { get; set; } public IList<DeliveryDetail> DeliveryDetails { get; set; } } public class DeliveryDetail { public string Description { get; set; } public int Id { get; set; } }Controller:
public class HomeController : Controller { public ActionResult Index() { var model = new MyModel { DeliveryDetails = new[] { new DeliveryDetail { Description = "detail 1", Id = 1 }, new DeliveryDetail { Description = "detail 2", Id = 2 }, new DeliveryDetail { Description = "detail 3", Id = 3 }, } }; return View(model); } [HttpPost] public ActionResult Index(MyModel model) { // Here you will get the id of the selected delivery detail // in model.SelectedDeliveryDetailId ... } }View:
@model MyModel @using (Html.BeginForm()) { foreach (var deliveryDetail in Model.DeliveryDetails) { @deliveryDetail.Description @Html.RadioButtonFor(x => x.SelectedDeliveryDetailId, deliveryDetail.Id) } <button type="submit">OK</button> }更多推荐
public,set,deliveryDetail,电脑培训,计算机培训,IT培训"/> <meta name="des
发布评论