如何将 mvc4 单选按钮绑定到模型

how to bind mvc4 radiobutton to a model

你好,我有一个简单的用户 class,在这个用户 class 中,我有 属性 类型 List<Contact>

public  class User
{
  public User()
  {
    this.AddressContact = new List<Contact>();
  }           
  public string FirstName { get; set; }
  public string LastName { get; set; }
  public string Email { get; set; }
  public List<Contact> AddressContact { get; set; }                         
}

public class Contact
{
  public int Id { get;set; }
  public string NameCompany {get;set;}
  public string Street {get; set; }
  public string Town {get; set; }
  public string Telephone {get; set;}
  public string Fax {get; set;}
}

我有一个操作,其中 return 一个视图模型

public ActionResult ConfirmationOrder()
{
  ConfirmationModel confmodel = new ConfirmationModel();
  Cart cart = (Cart)Session["Cart"];
  User contactUser = WebServiceBea.GetUserAddress(HttpContext.User.Identity.Name);
  confmodel.cart = cart;
  confmodel.user = contactUser;
  return PartialView(confmodel);
}

这是我的观点

@model MvcBeaWeb.Models.ConfirmationModel
<div class="pmc-page-container" id="" style="position: relative; width: 85%; ">
  <!-- Address Div -->
  @using (Html.BeginForm("Index", "OrderSummary", new { mod = Model }, FormMethod.Get, null))
  { 
    <div class="address" style="overflow: hidden;">
      <div style="width: 100%; float: left; line-height: 15px; margin: 5px 0px;" class="radio-line">
        @foreach (var item in Model.user.AddressContact)
        {
          <div>
            @Html.RadioButtonFor(model => model.user.AddressContact, new { id = "item" + item.Id })
            @Html.Label(item.NameCompany, item.NameCompany)
          </div>
        }
      </div>
    </div>      
    <input type="submit" value="Submit" />
  }
</div>   

问题是:我有一个单选按钮,它应该显示用户列表中的所有地址。但是当我点击提交按钮时,我没有选择(我无法获得选定的地址),实际上我的 OrderSummary Controller 中的 ConfirmationModel 是空的(我的表单正在发布到这个控制器) - 就像绑定根本不起作用

public class OrderSummaryController : Controller
{
  [Authorize]
  public ActionResult Index(ConfirmationModel mod)

您的代码有很多问题。首先,单选按钮组 post 返回单个值,但您试图绑定到一个复杂的对象。您的视图表明您正在创建一个用于选择联系人的表单,因此请创建一个视图模型来表示您想要显示和编辑的内容

查看模型

public class ContactVM
{
  public int SelectedContact { get; set; }
  public List<Contact> ContactList { get; set; }
  // Add any other properties that you want to display/edit
}

控制器

public ActionResult ConfirmationOrder()
{
  ContactVM model= new ContactVM();
  // populate the collection of contacts (and set the value of SelectedContact if you want a default item selected)
  return View(model);
}

public ActionResult ConfirmationOrder(ContactVM model)
{
  // model.SelectedContact now contains the ID of the selected contact
}

查看

@model ContactVM
@using (Html.BeginForm())
{
  @foreach (var contact in Model.Contacts)
  {
    @Html.RadioButtonFor(m => m.SelectedContact, contact.ID, new { id = contact.Id })
    <label for="@contact.Id>@contact.NameCompany</label>
  }
  <input type="submit" value="Submit" />
}

请注意 label 标记中的 for 属性以将文本与按钮相关联。

接下来你已经声明你想要 POST 但在 BeginForm 中使用 FormMethod.Get,但更糟糕的是,你试图将模型作为路由参数传递。在只有几个值类型属性的简单模型的情况下,这将起作用,但是您有一个具有复杂属性和集合的模型。除了这会创建丑陋的查询字符串,以及它很快就会超过查询字符串限制(抛出异常)这一事实之外,它就不会工作。检查 form 标签中的 html。你会看到像 user=MvcBeaWeb.Models.User" 这样的东西,所以当你提交模型活页夹试图将你的 user 属性 设置为失败的字符串 "MvcBeaWeb.Models.User" (你可以将一个字符串分配给一个复杂的对象)并且 user 属性 重置为 null。不清楚你为什么要这样做(为什么你会通过将整个模型发送给客户端来降低性能,然后 post 一切都保持不变?)。

您应该 post 将您的视图模型返回到 POST 方法,保存数据,然后重定向到您的 Index() 方法(但您还没有 posted您的 ConfirmationModel 或主视图,因此有点难以更具体)。