Shawn Mclean
Shawn Mclean

Reputation: 57469

How do you give user feedback after an action was made?

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

Answers (5)

Ryan Van Der Wal
Ryan Van Der Wal

Reputation: 165

I would just use

TempData["StatusMessage"] = "Your messsaga"

You can use tempdata for stuff only needed till next request.

Upvotes: 0

ataddeini
ataddeini

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

Peyton Crow
Peyton Crow

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

Xavier Poinas
Xavier Poinas

Reputation: 19733

You can:

  • redirect to a different view that contains an 'update successful' message
  • redirect to the 'Details' view (they will understand and 'see' that their changes have been saved)
  • pass a message to the view through the ViewData/ViewBag, then the view can display this.
    You could even have that logic in your layout page so that any view can display a message at the top of the screen, or something like that.

Upvotes: 1

Leons
Leons

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

Related Questions