Reputation: 1850
I am not sure if and how one can do this but in my controller I have:
[HttpGet]
public ActionResult Detail(int userId)
{
UserDetailViewModel user = new UserDetailViewModel();
user.UserId = userId;
user.Email = Email;
return View(user);
}
My UserDetailViewModel:
namespace Zinc.Web.Areas.Admin.ViewModels.User
public class UserDetailViewModel
{
[LocalizedRequired]
[DisplayName("UserId")]
public int UserId { get; set; }
[LocalizedRequired]
[DisplayName("Email")]
public string Email { get; set; }
}
I have the Id of the user but need to get the rest of the details like email, name, surname etc.. I have just put email down for now.. Must I pass into the view another model that has those details?
Upvotes: 0
Views: 199
Reputation: 103428
You would need to add all those properties into your view model (i.e. UserDetailViewModel
).
Then create a method which can get the values and set them to the properties using the id
as a parameter:
[HttpGet]
public ActionResult Detail(int userId)
{
UserDetailModel user = GetUser(userid);
UserDetailViewModel model = new UserDetailViewModel;
model.UserId = user.UserId;
model.Email = user.Email;
model.Name = user.Name;
//etc...
return View(model);
}
Example of GetUser
:
public UserDetailModel GetUser(userId){
//Do some data access here
UserDetailModel user = new UserDetailModel;
user.UserId = //Set from data accessed
//etc..
return user;
}
The advantage of applying this to a general Model rather than your View Model is that this can be applied to other View Models without having to make multiple methods for data access of the same data.
Upvotes: 1