6

我有一个索引页面,它有两个部分视图:登录和注册。我正在使用数据模型验证。

登录.cshtml

@model Project.ViewModel.UserModel

<div style="position:inherit;">
@using (Html.BeginForm("_Login", "account"))
{
    @Html.ValidationSummary(true)
    <div class="label">@Html.Label("Username")</div>
    <div class="field">@Html.TextBoxFor(m => m.Username)</div>
    <div class="error">@Html.ValidationMessageFor(model => model.Username)</div>

    <div class="label">@Html.Label("Password")</div>
    <div class="field">@Html.PasswordFor(m => m.Password)</div>
    <div class="error">@Html.ValidationMessageFor(model => model.Password)</div>

    <input class="field" id="submit" type="submit" value="Login" />
}

注册.cshtml

@model Project.ViewModel.UserModel

<link href="~/Content/Site.css" rel="stylesheet" />
<div style="position: inherit; margin-top: 20px">
@using (Html.BeginForm("_Register","account"))
{
    <div class="label">@Html.Label("Name")</div>
    <div class="field">@Html.TextBoxFor(m => m.FullName)</div>
    <div class="error">@Html.ValidationMessageFor(model => model.FullName)</div>

    <div class="label">@Html.Label("Username")</div>
    <div class="field">@Html.TextBoxFor(m => m.Username)</div>
    <div class="error">@Html.ValidationMessageFor(model => model.Username)</div>

    <div class="label">@Html.Label("Password")</div>
    <div class="field">@Html.PasswordFor(m => m.Password)</div>
    <div class="error">@Html.ValidationMessageFor(model => model.Password)</div>

    <div class="label">@Html.Label("Confirm Password")</div>
    <div class="field">@Html.PasswordFor(m => m.ConfirmPassword)</div>
    <div class="error">@Html.ValidationMessageFor(model => model.Password)</div>

    <div class="label">@Html.Label("Email")</div>
    <div class="field">@Html.TextBoxFor(m => m.Email)</div>
    <div class="error">@Html.ValidationMessageFor(model => model.Email)</div>

    <div class="label">@Html.Label("Country")</div>
    <div class="field">@Html.TextBoxFor(m => m.Country)</div>
    <div class="error">@Html.ValidationMessageFor(model => model.Email)</div>

    <input class="field" id="submit" type="submit" value="Sign Up" />
    @Html.ValidationSummary()   
}

索引.cshtml

 @model Project.ViewModel.UserModel
 @{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
 }

<div id="regiserandlogin">
<div id="registerandlogin-header">
    <label style="margin-left:50px;">Sign Up For Free!!!</label>
    <label style="margin-left:350px;color:#28a1e2">Already Have An Account?</label>
   </div>
   <div id="registerbox">
        @Html.Partial("_Register", new ProjectHub.ViewModel.UserModel())
   </div>
   <div id="loginbox">
        @Html.Partial("_Login", new ProjectHub.ViewModel.UserModel())
   </div>

    public ViewResult _Register()
    {
        return View("_Register");
    }
    [HttpPost]
    public ActionResult _Register(UserModel usermodel)
    {
        if (!ModelState.IsValid)
        {
            return View("Index");
        }
        try
        {
            FormsAuthentication.SetAuthCookie(usermodel.Username, false);
            return RedirectToAction("activationemail", new {username= Username});
        }
        catch (Exception ae)
        {
            ModelState.AddModelError("", ae.Message);
            return View();
        }
    }
   public ViewResult _Login()
    {
        return View("_Login");
    }
    [HttpPost]
    public ActionResult _Login(string username, string password)
    {
        if (ModelState.IsValid)
        {
            if (MembershipService.ValidateUser(username, password))
            {
                if (!repository.IsVerified(username))
                {
                    ModelState.AddModelError("","Account is not activated.;
                    return View();

                }
                FormsAuthentication.SetAuthCookie(username,false);
                  return RedirectToAction("Index","Home");

            }
            return RedirectToAction("Index", "account"); ;    
        }
        else
        {
            ModelState.AddModelError("","Invalid Username/Password");
            return View();
        }
    }

用户模型.cs

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace ProjectHub.ViewModel
{
    public class UserModel
    {
         [Required(ErrorMessage="Username is Required")]
         public string Username { get; set; }
         [Required(ErrorMessage = "Password is Required")]
         public string Password { get; set; }
         [Required(ErrorMessage = "Password is Required")]
         public string ConfirmPassword { get; set; }
         [Required(ErrorMessage = "Name is Required")]
         public string FullName { get; set; }
         [Required(ErrorMessage = "Email is Required")]
         public string Email { get; set; }
         [Required(ErrorMessage = "Country is Required")]
         public string Country { get; set; }
     }
 }

当我像这样按下注册按钮时,出现验证错误

在此处输入图像描述

如果我使用 RedirectToAction 方法,我不会收到验证错误。请给我提意见。

4

2 回答 2

5

您不应该对两个部分使用相同的视图模型。您应该有 2 个不同的视图模型。

例如:

public class LoginViewModel
{
    [Required(ErrorMessage="Username is Required")]
    public string Username { get; set; }

    [Required(ErrorMessage = "Password is Required")]
    public string Password { get; set; }
}

对于寄存器部分:

public class RegisterViewModel
{
     [Required(ErrorMessage="Username is Required")]
     public string Username { get; set; }

     [Required(ErrorMessage = "Password is Required")]
     public string Password { get; set; }

     [Required(ErrorMessage = "Password is Required")]
     public string ConfirmPassword { get; set; }

     [Required(ErrorMessage = "Name is Required")]
     public string FullName { get; set; }

     [Required(ErrorMessage = "Email is Required")]
     public string Email { get; set; }

     [Required(ErrorMessage = "Country is Required")]
     public string Country { get; set; }
}

然后你的主视图模型应该聚合这两个视图模型:

public class MyViewModel
{
    public LoginViewModel Login { get; set; }
    public LoginViewModel Register { get; set; }
}

接着:

<div id="registerbox">
    @Html.Partial("_Register", Model.Login)
</div>
<div id="loginbox">
    @Html.Partial("_Login", Model.Register)
</div>
于 2013-02-23T17:40:12.810 回答
1

在您的视图中添加以下对 JQuery 脚本的引用。
我经历了同样的情况,它解决了我的问题。

"~/Scripts/jquery.unobtrusive*"
"~/Scripts/jquery.validate*"
于 2013-09-11T13:54:57.707 回答