Validation on RenderAction

by 26. July 2010 18:51

Consider the following parent page Index.aspx

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<HomeData>" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<div style="background-color:Red;">
<% Html.RenderAction("SignUp", "Account"); %>
</div>
</body>
</html>

Notice the RenderAction for the following SignUp.ascx page.

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

<% using (Html.BeginForm("SignUp", "Account"))
{ %>
<div>
<%: Html.LabelFor(m => m.Email) %>
</div>
<div>
<%: Html.TextAreaFor(m => m.Email) %>
</div>
<div>
<%: Html.ValidationMessageFor(m => m.Email) %>
</div>
<br />
<input type="submit" />
<%} %>

My SignUpData model looks something like this:

public class SignUpData : BaseViewData
{
/// <summary>
/// The email address you are signing up with.
/// </summary>
[DataType(DataType.EmailAddress)]
[Required(ErrorMessage = "Email is required.")]
[StringLength(200, ErrorMessage = "Email must be 200 characters or less.")]
[RegularExpression(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", ErrorMessage = "Valid Email Address is required.")]
[DisplayName("Email Address")]
public string Email
{
get;
set;
}

...

 

My controller AccountController looks something like this:

[HttpPost]
public ActionResult SignUp(SignUpData data)
{
if (ModelState.IsValid)
{
return View("SignUp", data);
}

return RedirectToAction("Index", "Home", data);
//return View();

}

The problem I have is that although this all renders nicely when i enter an invalid email, it *does* set the ModelState as invalid and if u use Ajax i can get things working nicely.

However, I want this to work on the server too. I'm not sure whether there is NOW any way to pass this invalid model state such that the original Index page (top) is rendered with the RenderPartial called such that the invalid email address state is passed in and rendered - i just don't see the best practice way (sure, i can think of 50 hacks) to get the error information back on the postback (ideally it would work OOTB as it does normally).

I also disable Session state in my applications so i can't use TempData.

Tags: , ,

tech