Reputation: 57469
Using asp.net mvc, a user updates his profile. This is how my update action looks:
[HttpPost]
public ActionResult EditUser(string id, UserEditViewModel model)
{
Mapper.CreateMap<UserEditViewModel, SimpleUser>();
var user = Mapper.Map<UserEditViewModel, SimpleUser>(model);
userService.EditUser(user);
//Re-populating the countries.
var countries = geoService.GetCountries();
model.Countries = countries.Select(c => new SelectListItem { Text = c.Name, Value = c.CountryId.ToString() });
return View(model);
}
First of all, am I doing this correct? By writing the countries populating code twice (1 in GET and 1 here).
Second, how do I give the user some form of a feed back that they have successfully updated their profile?
Upvotes: 0
Views: 657
Reputation: 165
I would just use
TempData["StatusMessage"] = "Your messsaga"
You can use tempdata for stuff only needed till next request.
Upvotes: 0
Reputation: 4951
Since you've created a specific view model for the user edit page (nice approach, by the way), I would actually add the message as a property on that view model itself. After all, it exists specifically to contain data for your view, so why complicate things/add inconsistency by throwing a key/value pair into ViewData
? That's the way I see it at least.
Oh, and incidentally, if you're using Automapper, and it looks like you are (another nice approach) you may consider putting your Mapper.CreateMap
in someplace other than the action itself. There is a little overhead there. Hope that helps.
Upvotes: 2
Reputation: 882
It might be better to create a common / helper method for your model for all this stuff, something like:
private void SetupCountries() {
//Re-populating the countries.
var countries = geoService.GetCountries();
model.Countries = countries.Select(c => new SelectListItem { Text = c.Name, Value = c.CountryId.ToString() });
}
Then in all of your actions in the controller, you can just call it without typing those code again.
public ActionResult EditUser(.... {
SetupCountries();
Upvotes: 0
Reputation: 19733
You can:
ViewData
/ViewBag
, then the view can display this.Upvotes: 1
Reputation: 2674
Yes, we will normally have to query twice, first to popule the screen and the second time to re-populate. You can always cache the values on the first retrieve and use the cached values on the postback action to re-populate the screen.
Set your message in ViewData (MVC-2) or ViewBag (MVC-3) and display to the user.
Upvotes: 1