数据注释 MVC3 必需属性 [英] Data annotations MVC3 Required attribute

查看:34
本文介绍了数据注释 MVC3 必需属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的模型(用户),我用它来添加新用户和更新现有用户.添加新用户时需要输入用户名和密码,更新时只需要输入用户名,因为不允许更改密码.这是问题,添加一个新用户一切正常,因为我输入了名称和密码值,因此 ModelState.IsValid 返回 true,但是在更新用户时没有输入密码,因此它始终具有空值,这使得ModelState.IsValid 一直返回 false.有没有办法使用相同的模型,即使在添加视图中需要密码而在更新视图中不需要密码?拜托,有什么建议吗?

I have the Model (User) below, I use it to add new users and to update existing users. When I'm adding a new user it's required to enter the user name and the password, and when I'm updating it's required to enter only the user name because it's not allowed to change the password. Here is the problem, adding a new user everything works ok because I enter both name and password values so ModelState.IsValid returns true, but when updating an user there is no input to the password, so it always have the null value what makes the ModelState.IsValid returns false all the time. Is there a way to use the same model even if in the Add View the password is required and in the Update View it's not? please, any suggestions?

     public class User {

        public int ID { get; set; }

        [Display(Name = "Nome do Usuário")]
        [Required(ErrorMessage = "Digite o Nome do Usuário.")]
        public string name { get; set; }

        [Display(Name = "Senha")]
        [Required(ErrorMessage = "Digite a Senha.")]
        public string password { get; set; }

    }



public ActionResult Add()
        {
            return View();
        }

      [HttpPost]
            public ActionResult Add(User user)
            {
                UsuariosViewModel viewModel = new UsuariosViewModel();


                if (ModelState.IsValid)
                {
                    viewModel.Msg = new AdmUsuarios().CadastraUsuario(user);
                }
                return View(viewModel);
            }



    public ActionResult Update(int id)
            {
                UsuariosViewModel viewModel = new UsuariosViewModel();

            using (var dataContext = new DBEntities()) 
            {
                Repository<User> repository = new Repository<User>(dataContext);
                viewModel.User = repository.GetById(id);
            }

                return View(viewModel);
            }

        [HttpPost]
        public ActionResult Update(User user, int id)
        {          
            UsuariosViewModel viewModel = new UsuariosViewModel();

            if (ModelState.IsValid)
            {
                viewModel.Msg = new AdmUsuarios().AlteraUsuario(user, id);
            }
            return View();
        }

推荐答案

你应该使用视图模型.

您的数据注释将属于传递给视图的视图模型

Your data annotations would then belong on the view model being passed to the view

public class CreateViewModel
{
  public int ID { get; set; }

  [Display(Name = "Nome do Usuário")]
  [Required(ErrorMessage = "Digite o Nome do Usuário.")]
  public string name { get; set; }

  [Display(Name = "Senha")]
  [Required(ErrorMessage = "Digite a Senha.")]
  public string password { get; set; }
}

和编辑

 public class EditViewModel
    {
      public int ID { get; set; }

      [Display(Name = "Nome do Usuário")]
      [Required(ErrorMessage = "Digite o Nome do Usuário.")]
      public string name { get; set; }

      //perhaps you don't need the password at all in the edit view
    }

将这些类传递给您的视图,而不是您的域模型(User),然后,在控制器中,将视图模型属性映射回模型,然后再持久化到您的数据源.

Pass these classes to your view(s), not your domain model(User), then, in the controller, map the view model properties back to the model before persisting to your data source.

这篇关于数据注释 MVC3 必需属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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