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

时间:2015-01-23 13:01:35

标签: asp.net-mvc

您好我有一个简单的用户类,在这个用户类中我有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;}
}

我有一个返回视图模型的动作

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)

1 个答案:

答案 0 :(得分:1)

您的代码存在很多问题。首先,单选按钮组会回发单个值但您尝试绑定到复杂对象。您的视图表示您创建了一个用于选择联系人的表单,因此创建一个表示您要显示和编辑的视图模型

查看模型

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" />
}

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

接下来,您已声明要进行POST但在FormMethod.Get中使用BeginForm,但更糟糕的是,您尝试将模型作为路由参数传递。对于只有少量值类型属性的简单模型,这将起作用,但是您有一个具有复杂属性和集合的模型。除了这将创建的丑陋的查询字符串,以及它将很快超过查询字符串限制(抛出异常)的事实,它只是不会工作。检查form标记中的html。您将看到user=MvcBeaWeb.Models.User"之类的内容,因此当您提交模型绑定器时,尝试将user属性设置为失败的字符串"MvcBeaWeb.Models.User"(您可以将字符串分配给复杂对象)并且user属性已重置为null。目前还不清楚你为什么要这样做(为什么你会把你的整个模型发送给客户端,然后将它们全部保持不变来降低性能?)。

您应该将您的视图模型发布回POST方法,保存数据,然后重定向到您的Index()方法(但您尚未发布ConfirmationModel或主视图,因此它有点难更具体一点。)