asp.net - 两个部分 View 中的验证错误

标签 asp.net asp.net-mvc asp.net-mvc-3

我有一个索引页面,其中有两个部分 View _Register 和_Login。我有三个 View 模型,即 LoginViewModel、RegisterViewModel、LoginOrRegisterViewModel。现在,我希望如果 modelstate 无效,我想重定向回具有登录和注册表单的索引页面。但是,在这里,它重定向到注册 View 或登录 View ,并在其他 View 模型中进行验证。请指导我正确的方法返回类型和正确的 View 。

索引.cshtml

@model Project.ViewModels.LoginOrRegisterModel
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
 }
    @Html.Partial("_Register", Model.Registermodel)
    @Html.Partial("_Login", Model.Loginmodel)

_Register.cshtml

@model ProjectHub.ViewModels.RegisterModel

@using (Html.BeginForm("Register", "account", FormMethod.Post))
{
    <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>


    //similar html for other attributes

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

_Login.cshtml

@model ProjectHub.ViewModels.LoginModel
@using (Html.BeginForm("Login", "account", FormMethod.Post))
{
    @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" />
}

View 模型

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

namespace Project.ViewModels
{
    public class LoginModel
    {
    [Required(ErrorMessage="Username is Required")]
    public string Username { get; set; }
    [Required(ErrorMessage = "Password is Required")]
    public string Password { get; set; }
    }

    public class RegisterModel
    {
    [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 LoginOrRegisterModel
    {
         public LoginModel Loginmodel { get; set; }
         public RegisterModel Registermodel { get; set; }
    }
}

账户 Controller

    public ActionResult Index()
    {
        LoginOrRegisterModel model = new LoginOrRegisterModel();
        model.Loginmodel = new LoginModel();
        model.Registermodel = new RegisterModel();
        return View(model);
    }
    [HttpPost]
    public ActionResult Index(LoginOrRegisterModel model)
    {
        return View(model);
    }

    [HttpGet]
    public ViewResult Login()
    {
        LoginModel model = new LoginModel();
        return View("_Login",model);
    }
    [HttpPost]
    public ActionResult Login(LoginModel usermodel)
    {
           if (ModelState.IsValid)
           {
                //some validation loic
                return RedirectToAction("Index", "Home");
            }
            return View("Index", new LoginOrRegisterModel() { Loginmodel = usermodel, Registermodel = new RegisterModel() });
        }
        else
        {
            ModelState.AddModelError("", "Invalid Username/Password");
            return View("Index",new LoginOrRegisterModel() { Loginmodel = usermodel, Registermodel = new RegisterModel() });
        }
    }

    public ViewResult Register()
    {
        RegisterModel model = new RegisterModel();
        return View("_Register",model);
    }
    [HttpPost]
    public ActionResult Register(RegisterModel usermodel)
    {
        if (!ModelState.IsValid)
        {
            return View("Index", new LoginOrRegisterModel() { Loginmodel = new LoginModel(), Registermodel = usermodel });
        }
        try
        {
            //Some logic for validation
            return RedirectToAction("activationemail", new { username = usermodel.Username, email = usermodel.Email });
        }
        catch (Exception ae)
        {
           return View("Index", new LoginOrRegisterModel() { Loginmodel = new      LoginModel(), Registermodel = usermodel });
        }
    }

请帮助我,我收到错误 like this

最佳答案

  1. 渲染部分时添加 HtmlFieldPrefix:

    @Html.Partial("_Register", Model.Registermodel, new ViewDataDictionary(this.ViewData) { TemplateInfo = new TemplateInfo { HtmlFieldPrefix = "Registermodel" } })
    @Html.Partial("_Login", Model.Loginmodel, new ViewDataDictionary(this.ViewData) { TemplateInfo = new TemplateInfo { HtmlFieldPrefix = "Loginmodel" } })
    

    或使用PartialFor

  2. 在操作方法 BindAttribute 中添加前缀:

    public ActionResult Register([Bind(Prefix = "Registermodel")]RegisterModel usermodel)
    public ActionResult Login([Bind(Prefix = "Loginmodel")]LoginModel usermodel)
    

关于asp.net - 两个部分 View 中的验证错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15048640/

相关文章:

c# - 如何允许 IIS 使用 ASP.NET MVC 项目中的本地数据库?

javascript - MVC html5 验证

jquery - 谷歌浏览器中的 ReportViewer 问题

javascript - 在链接按钮的顶部显示工具提示

asp.net - 如何组织 ASP.NET app_code 文件夹?

c# - 显示 2 位小数(除非有更多有效数字)

asp.net-mvc-3 - 带有可选第一个参数的 ASP.net MVC 路由

c# - 如何使用 Mvc 3 Razor 填充列表?

c# - 数据库插入后如何返回唯一ID

c# - 在 ASP.NET 中设置 DropDownList 的值