开发者

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>();
      开发者_Python百科  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?


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.


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.


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.


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();


I would just use

TempData["StatusMessage"] = "Your messsaga"

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜