mvc4宣布模型类 - 困境

时间:2013-11-09 05:38:06

标签: c# asp.net-mvc asp.net-mvc-4

以下是我的用户模型类

public class User
{
    [System.ComponentModel.DataAnnotations.Key]
    public int UserId { get; set; }
    public Int16 RoleID { get; set; }
    [DisplayName("First Name :")]
    public string FirstName { get; set; }
    [DisplayName("Last Name :")]
    public string LastName { get; set; }

    [DisplayName("Email :")]
    [Required(ErrorMessage = "Email is required")]
    [EmailAddress(ErrorMessage = "Invalid Email Address")]
    public string Email { get; set; }

    [DataType(DataType.Password)]
    [Required(ErrorMessage = "Password is required")]
    [DisplayName("Password :")]
    public string Password { get; set; }

    [DataType(DataType.Password)]
    [Compare("Password")]
    [Required(ErrorMessage = "Confirm password is required")]
    [DisplayName("Confirm Password :")]
    public string CPassword { get; set; }
}

以下代码位于AccountController

 [HttpPost]
 [AllowAnonymous]
 [ValidateAntiForgeryToken]
 public ActionResult Login(User model, string returnUrl)
 {
      if(ModelState.IsValid) //This turns out to be always false
 }

这是Login.cshtml

@using (Html.BeginForm())
{
 @Html.AntiForgeryToken()
 @Html.ValidationSummary(true)
 <fieldset class="cf addForm">
     @Html.LabelFor(model => model.Email)
     @Html.TextBoxFor(model => model.Email, new { @class = "wd293 inputtext" })
     @Html.ValidationMessageFor(model => model.Email)
     @Html.LabelFor(model => model.Password)
     @Html.PasswordFor(model => model.Password, new { @class = "wd293 inputtext" })
     @Html.ValidationMessageFor(model => model.Password)
     <div class="cf signBtn">
        <input type="submit" value="Sign in" class="pageBtn alignleft savebtn" />
     </div>
 </fieldset>
 }

以上模型类应适用于注册表格,即如上所述需要所有详细信息,例如:电子邮件/密码/确认密码

但是对于只需要电子邮件和密码的登录表单字段,因此在这种情况下,ModelState.IsValid总是给出错误。

我处于两难境地应该是什么解决方案,创建另一个模型类?就像登录表格一样,会有另一个型号UserLoginViewModel,它只有2个属性的电子邮件/密码,而对于注册表格UserRegisterViewModel,这将具有所有必需的属性?

请耐心等待,如果这听起来很愚蠢,因为我对MVC4相当新鲜。如果还需要进一步的代码,请告诉我。

修改

public class MyDBContext : DbContext
{
    public DbSet<User> Users { get; set; }
    public DbSet<Category> Categories { get; set; } // It has CategoryID,CategoryName properties
}

上面是我的数据库上下文类,用于保存数据库中的类别,我按照代码编写

 new MyDBContext().Categories.Add(category);

因此,我想创建一个新类UserRegisterViewModel,其中包含注册表单所需的所有属性,在这种情况下,我需要再次将此UserRegisterViewModel转换为User,这是否可以,或者是否会增加内存开销?< / p>

转换就像

 User newUser = new User(); //Assign all the properties from UserRegisterViewModel to this and save 
 new MyDBContext().Users.Add(newUser);

请帮忙。

3 个答案:

答案 0 :(得分:2)

在这种情况下,每个视图应该有一个类。如果在Visual Studio中从Internet模板创建MVC项目,则默认情况下会生成此类/模型。如果需要,可以使用继承。 但是,有一些可能性可以避免多个模型,但我认为每个视图都有一个模型更清晰,更快。

答案 1 :(得分:0)

尝试从电子邮件地址中删除必填字段属性

 [EmailAddress(ErrorMessage = "Invalid Email Address")]
试试这个,它会起作用还是不起作用?你的模型现在有效吗?

答案 2 :(得分:0)

事实证明我们也可以验证特定属性并使用以下代码检查ModelState,

//Instead of this - which checks all attributes of model class
if (ModelState.IsValid)

已更改为

//This will only verify against email/password property only so other property are ignored
if (ModelState.IsValidField("Email") && ModelState.IsValidField("Password")) 

使用上面的方式更改登录代码并且有效。

谢谢大家的支持。