In my little app I've a strongly typed partial view (the Login Form) this is rendered within the Masterpage, this looks as follows:
<!-- Login Box -->
<div class="login">
<div class="login-bg clearingfix">
<% Html.RenderPartial("LoginViewControl", Model); %>
</div>
</div>
<!-- / Login Box -->
The Model of this partial view is:
using System;
using System.Web;
using System.ComponentModel.DataAnnotations;
using mpws.Util.Validation;
namespace mpws.Models
{
public class LoginViewModel
{
[Required]
public string UserName { get; set; }
[Required]
public string Password { get; set; }
}
}
And the View itself contains a Validationsummary: <%= Html.ValidationSummary() %>
I created another View called SignUp, also this is strongly typed with the type as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using mpws.Util.Validation;
namespace mpws.Models
{
public class SignUpViewModel
{
[Required]
public string UserName { get; set; }
[Required]
public string Password { get; set; }
[Required]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
[Required]
public string EmailAddress { get; set; }
[Required]
public string EmailAddressRepeat { get; set; }
public string Occupation { get; set; }
public string SuggestedBy { get; set; }
public IValidationState validationState { get; set; }
}
}
here my SignUp View:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<mpws.Models.SignUpViewModel>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
SignUp
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>SignUp</h2>
<div id="SignUpForm">
<% using (Html.BeginForm("SignUp","Account", new mpws.Models.SignUpViewModel())) {%>
<%: Html.ValidationSummary() %>
<fieldset>
<legend>Fields</legend>
<div class="editor-label">
<%: Html.Resource("Strings, Username") %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.UserName) %>
<%: Html.ValidationMessageFor(model => model.UserName) %>
</div>
<div class="editor-label">
<%: Html.Resource("Strings, Password") %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Password) %>
<%: Html.ValidationMessageFor(model => model.Password) %>
</div>
<div class="editor-label">
<%: Html.Resource("Strings, FirstName") %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.FirstName) %>
<%: Html.ValidationMessageFor(model => model.FirstName) %>
</div>
<div class="editor-label">
<%: Html.Resource("Strings, LastName") %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.LastName) %>
<%: Html.ValidationMessageFor(model => model.LastName) %>
</div>
<div class="editor-label">
<%: Html.Resource("Strings, EmailAddress") %>
</div>
<div class="editor-field">
<开发者_如何学Go;%: Html.TextBoxFor(model => model.EmailAddress) %>
<%: Html.ValidationMessageFor(model => model.EmailAddress) %>
</div>
<div class="editor-label">
<%: Html.Resource("RepeatEmailAddress") %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.EmailAddressRepeat) %>
<%: Html.ValidationMessageFor(model => model.EmailAddressRepeat) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Occupation) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Occupation) %>
<%: Html.ValidationMessageFor(model => model.Occupation) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.SuggestedBy) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.SuggestedBy) %>
<%: Html.ValidationMessageFor(model => model.SuggestedBy) %>
</div>
<p>
<button class="button positive" type="submit">
<img alt="" src="<%= ResolveUrl("~/Content/public/ui/button/tick.png") %>" />
<%: Html.Resource("Strings, SignUp") %>
</button>
<button class="button negative" type="reset">
<img alt="" src="<%= ResolveUrl("~/Content/public/ui/button/cross.png") %>" />
<%: Html.Resource("Strings, Cancel") %>
</button>
</p>
</fieldset>
<% } %>
</div>
</asp:Content>
and the login partial View:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<mpws.Models.LoginViewModel>" %>
<!-- This should be outsourced into it own .js fila -->
<script type="text/javascript">
/// <reference path="jquery-1.4.1-vsdoc.js" />
$(document).ready(function () {
$('#LoginForm form').live('submit', function () {
$.post($(this).attr('action'), $(this).serialize(), function (result) {
if ($.isString(result)) {
$("#LoginForm").replaceWith($(result));
}
else if (result["Successfull"]) {
window.location.href = result["RedirectUrl"];
}
else {
alert('Unknown Error...');
}
});
return false;
});
});
jQuery.isString = function (o) {
return (typeof o === "string");
}
</script>
<div id="LoginForm">
<% using (Html.BeginForm("SignIn", "Account"))
{%>
<fieldset>
<legend>Login</legend>
<%if(!ViewContext.ViewData.ModelState.IsValid) {%>
<%= Html.ValidationSummary_jQueyUIfriendly() %>
<%= Html.ValidationSummary() %>
<br />
<%} %>
<div class="editor-label">
<%: Html.Resource("Strings, Username") %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.UserName)%> <br />
<%: Html.ValidationMessageFor(model => model.UserName)%>
</div>
<div class="editor-label">
<%: Html.Resource("Strings, Password") %>
</div>
<div class="editor-field">
<%: Html.PasswordFor(model => model.Password)%><br />
<%: Html.ValidationMessageFor(model => model.Password)%>
</div>
<p>
<button class="button positive" type="submit">
<img alt="" src="<%= ResolveUrl("~/Content/public/ui/button/tick.png") %>" />
<%: Html.Resource("Strings, SignIn") %>
</button>
</p>
</fieldset>
<% } %>
</div>
Well if now someone clicks the "SignUp"-Submitbutton, the validation errors are shown in the validationsummary of the signUp view but also in the validationsummary of the partial view "LoginViewControl"... Any idea why? I thought maybe the Modelstate is globally handeld but if I click the "login"-Submitbutton the validation Messages are only shown in the partial view...
Any idea?
Thanks in advance Johannes
精彩评论