验证不是在局部视图工作 [英] Validation not working in partial view

查看:148
本文介绍了验证不是在局部视图工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个局部视图索引页:登录和register.I正在使用的数据模型验证

I have an Index page which has two partial views: login and register.I am using data model validation.

Login.cshtml

Login.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" />
}

Register.cshtml

Register.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()   
}

Index.cshtml

Index.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();
        }
    }

UserModel.cs

UserModel.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; }
     }
 }

当我这样的preSS注册按钮,我得到验证错误

When I press register button like this, I get a validation error

如果我使用RedirectToAction方法,我没有得到验证错误。
请帮我,没有评论我的previous帐户得到受阻不downvote。

If I use RedirectToAction Method, I don't get the validation error. Please help me and don't downvote without commenting as my previous account got blocked.

推荐答案

您不应使用这两个谐音相同视图模型。你应该有2个不同的视图模型。

You should not use the same view model for both partials. You should have 2 different view models.

例如:

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; }
}

和那么你的主视图模型应该聚集的2视图模型:

and then your main view model should aggregate those 2 view models:

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>

这篇关于验证不是在局部视图工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆