So I have a partial view...
<%@ 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>
<% }
}%>
When I hit submit and look what's in the posted data I see
EmailAddress:someone@somewhere.co.uk
AreaUnsubscribe:true
AreaUnsubscribe:false
MonthlyUnsubscribe:true
MonthlyUnsubscribe:false
As a result TryUpdateModel returns true but doesn't populate any fields
This gets posted to the controller...
[HttpPost]
public ActionResult GetRecord(IntegraRecord model)
{
if (TryUpdateModel(model))
{
try
{
BusinessLayer.UpdateEmailAddress(model);
}
catch (ArgumentException)
{
return View("Error", ViewBag.Message = "Could Not Update开发者_JAVA百科 Email Address.");
}
}
return PartialView("GetRecord", model);
}
Any help massively appreciated...
Update: So following the clarification below (Thanks!)
I'm not using a custom model binder so I guess I'm missing some other convention too...
Here's my 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;
}
}
That's how MVC handles checkboxes: asp.net mvc: why is Html.CheckBox generating an additional hidden input (and many other places) The problem is onthe server side (default model binder is aware of that and doesn't have a problem). Are you using custom model binder?
精彩评论