2

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

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

视图模型

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

账户控制器

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

请帮助我,我收到这样的错误

4

1 回答 1

2
  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)
    
于 2013-02-24T09:43:39.460 回答