2011-08-04 29 views
0

所以我有一個局部視圖...型號CheckBoxFor屬性被張貼兩次ASP網MVC 3

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<NewsletterUnsubscribe_MVC3v2.Models.IntegraRecord>" %> 

<% if (!String.IsNullOrEmpty(Model.ErrorMessage)) 
    {%> 
    <div class="input-validation-error"> 
    <%:Model.ErrorMessage %> 
    </div> 
<% } 
    else 
    {%> 

<% using (Html.BeginForm()) 
{%> 
    <%:Html.ValidationSummary(true)%> 
    <fieldset> 
     <legend>IntegraRecord</legend> 
     <div class="editor-field"> 
       <%:Html.LabelFor(m => m.EmailAddress)%>: <strong><%:Model.EmailAddress%></strong> 
     </div> 
     <%:Html.HiddenFor(m=>m.EmailAddress) %> 
     <div class="editor-field"> 
       Unsubscribe from Area mailings: <%:Html.CheckBoxFor(m => m.AreaUnsubscribe)%> 
     </div> 
     <div class="editor-field"> 
       Unsubscribe from Monthly newsletters: <%:Html.CheckBoxFor(m => m.MonthlyUnsubscribe)%> 
     </div> 
     <p> 
      <input type="submit" value="Save" /> 
     </p> 
    </fieldset> 
<% } 
    }%> 

當我打提交,看看有什麼在發佈的數據我看

EmailAddress:[email protected] 
AreaUnsubscribe:true 
AreaUnsubscribe:false 
MonthlyUnsubscribe:true 
MonthlyUnsubscribe:false 

由於結果TryUpdateModel返回true,但不填充任何字段

這張貼到控制器上...

[HttpPost] 
     public ActionResult GetRecord(IntegraRecord model) 
     { 
      if (TryUpdateModel(model)) 
      { 
       try 
       { 
        BusinessLayer.UpdateEmailAddress(model); 
       } 
       catch (ArgumentException) 
       { 
        return View("Error", ViewBag.Message = "Could Not Update Email Address."); 
       } 
      } 
      return PartialView("GetRecord", model); 
     } 

任何幫助大規模讚賞...


更新:那麼按照下面的說明(!謝謝)我不使用自定義的模型綁定

,所以我想我錯過了一些其他公約太...

這裏是我的模型......

public class IntegraRecord 
    { 
     private const string EmailRegex = @"[snip]"; 

     [Required(ErrorMessage = "Email Address is required")] 
     [RegularExpression(EmailRegex, ErrorMessage = "This does not appear to be an email address")] 
     public string EmailAddress; 

     public bool AreaUnsubscribe; 
     public bool MonthlyUnsubscribe; 

     public string ErrorMessage; 

     public IntegraRecord() 
     { 
     } 

     public IntegraRecord(string email, bool area, bool monthly) 
     { 
      EmailAddress = email; 
      AreaUnsubscribe = area; 
      MonthlyUnsubscribe = monthly; 
     } 
    } 

回答