Obtain various properties of the object

2

Hi, I would like to know what solutions there are to reduce an object. I have a model which is used as a return in the controller but I'm only interested in certain properties of that model. The main idea I came up with was to create an anonymous object with only those properties, but I would like to know what options I have.

namespace aplication {
    class UserModel{
        public string Car{ get; set; }
        public string Dir { get; set; }
        public string Name { get; set; }
        public string Surname { get; set; }
        public string Email { get; set; }
    }
}

public ActionResult Login() {
            UserModel user = new UserModel();
            //nuevo objeto con las propiedades Name y email
            return View(nuevoObjeto);
}
    
asked by user2742460 12.07.2016 в 10:02
source

1 answer

2

What is usually done in these cases is to use a view model (or view model): a class created specifically to be used as a model in the view:

class LoginViewModel{
    public string Name { get; set; }
    public string Email { get; set; }
}

public ActionResult Login() {
    var user = new UserModel();
    // Código en el que cargas las propiedades de user
    var model = new LoginViewModel() { Name = user.Name, Email = user.Email };
    return View(model);
}
    
answered by 12.07.2016 / 10:11
source