Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 仅MVC数据验证插入表单_C#_Asp.net Mvc - Fatal编程技术网

C# 仅MVC数据验证插入表单

C# 仅MVC数据验证插入表单,c#,asp.net-mvc,C#,Asp.net Mvc,我和MVC5一起工作。我有一个模型调用User,它有几个属性。其中一些是必需的,如Password和ConfirmPassword 看起来像这样 public partial class Users { public long User_id { get; set; } [Required] public string Password { get; set; } [NotMapped] // Does not effect with database

我和MVC5一起工作。我有一个模型调用
User
,它有几个属性。其中一些是必需的,如
Password
ConfirmPassword

看起来像这样

public partial class Users
{
    public long User_id { get; set; }
     [Required]
     public string Password { get; set; }
    [NotMapped] // Does not effect with database
     [Compare("Password")]
    public virtual string ConfirmPassword { get; set; }
}
public class UserViewModel
{
    public Users user { get; set; }
    public IList<SelectListItem> AvailableCountries { get; set; }
}
public async Task<ActionResult> Update(UserViewModel model)
{
     ModelState.Remove("Password");
     ModelState.Remove("ConfirmPassword");
     if (ModelState.IsValid)
     {
     }
}
视图继承自如下所示的模型

public partial class Users
{
    public long User_id { get; set; }
     [Required]
     public string Password { get; set; }
    [NotMapped] // Does not effect with database
     [Compare("Password")]
    public virtual string ConfirmPassword { get; set; }
}
public class UserViewModel
{
    public Users user { get; set; }
    public IList<SelectListItem> AvailableCountries { get; set; }
}
public async Task<ActionResult> Update(UserViewModel model)
{
     ModelState.Remove("Password");
     ModelState.Remove("ConfirmPassword");
     if (ModelState.IsValid)
     {
     }
}

不要在实体中添加数据批注。必须在ViewModel中添加数据注释,以便视图是用户ViewModel的强类型

模式 国家考虑UserViwMode

因此,您必须删除UserViewModel中的
public Users user{get;set;}
,并添加以下更改:

public class UserViewModel
{
  puclic string Name { get; set; }

  puclic string LastName { get; set; }

  puclic bool sex { get; set; }

  // and more ...

  public IList<SelectListItem> AvailableCountries { get; set; }
}
public类UserViewModel
{
puclic字符串名称{get;set;}
puclic字符串LastName{get;set;}
puclic bool sex{get;set;}
//还有更多。。。
公共IList可用国家/地区{get;set;}
}

我解决了问题,正如上面所说的斯蒂芬。我复制用户实体和用户模型,并使用Mapper映射这些模型。就这样

您的问题是什么?????我不明白您的问题我刚刚完成,我的代码有错误,因此无法正确提交。感谢您需要单独的视图模型来“注册”和“编辑”用户。视图模型不应该包含数据模型的属性。当我绑定数据库时,我的用户在实体框架中创建类…OK Soheil。。。如果UserViewModel继承自Users实体。。。如何从用户属性在UserViewModel中添加数据批注?谢谢