在我的小应用程序中,我有一个强类型的局部视图(登录表单),它在Masterpage中呈现,如下所示:
<!-- Login Box -->
<div class="login">
<div class="login-bg clearingfix">
<% Html.RenderPartial("LoginViewControl", Model); %>
</div>
</div>
<!-- / Login Box -->
这个局部视图的模型是:
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; }
}
}
View本身包含Validationsummary:<%= Html.ValidationSummary() %>
我创建了另一个名为SignUp的View,它也是强类型的,类型如下:
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; }
}
}
这里是我的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">
<%: 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>
和登录部分查看:
<%@ 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>
如果现在有人点击“SignUp” - 子按钮,验证错误会显示在signUp视图的validationsummary中,但也会显示在部分视图“LoginViewControl”的validationsummary中......任何想法为什么?我想也许Modelstate可能是全球性的handeld但是如果我点击“login”-Submitbutton,验证消息只显示在局部视图中...
有什么想法吗?
提前致谢 约翰内斯