数据库更新后返回成功/失败消息以供查看 [英] returning success/failure message to view after database update

查看:90
本文介绍了数据库更新后返回成功/失败消息以供查看的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在用户更新个人资料页面以指示其是否成功之后向用户显示状态消息.这是我所拥有的:

I would like to display a status message to a user after they update a profile page to indicate whether it was successful or not. Here is what I have:

控制器

[HttpPost]
public ActionResult Profiles(UserProfile model)
{
    try
    {
        _userRepository.UpdateUserProfile(model);
        ViewBag.Message = "Success";
    }
    catch
    {
        ViewBag.Message = "Failure";
    }
     return View();
}

数据库调用

public void UpdateUserProfile(UserProfile user)
{
    using (var connection = new SqlConnection(SQLSettings.GetConnectionString()))
    {
        var p = new DynamicParameters();
        p.Add("@Id", user.Id);
        p.Add("@City", user.City);
        p.Add("@State", user.State);
        connection.Execute("UpdateUserProfile", p, commandType: CommandType.StoredProcedure);
     }
}

查看

@if (ViewBag.Message == "Success")
{
    <div class="alert alert-success"><strong><span class="glyphicon glyphicon-check"></span> Your profile has been updated.</strong></div>
}
@if (ViewBag.Message == "Failure")
{
    <div class="alert alert-danger"><span class="glyphicon glyphicon-alert"></span><strong> Error, please try again.</strong></div>
}

虽然这似乎对我有用,但我想还有一种更合乎逻辑的方法?

While this appears to be working for me, I'm guessing there is a more logical way?

推荐答案

在此用例中,您应该真正考虑切换到PRG模式.PRG代表 POST - REDIRECT - GET .通过这种方法,成功完成事务(例如:更新用户记录)后,您将使用新位置返回到客户端浏览器的重定向响应,浏览器将进行全新的http get调用以加载该GET操作方法.

You should really consider switching to the PRG Pattern in this use case. PRG stands for POST-REDIRECT-GET. In this approach after successfully finishing a transaction (ex :Updating user record), you return a redirect response to the client browser with the new location and browser will make a totally new http get call to load that GET action method.

您可以传递TempData来传输成功消息.如果成功完成操作时出现任何错误,则可以使用 ModelState.AddModelErrorMethod 将错误添加到模型状态字典中.

You can pass TempData to transfer the success message. If there was any error in successfully completing the operation, you may use the ModelState.AddModelErrorMethod to add an error to the model state dictionary.

[HttpPost]
public ActionResult Profiles(UserProfile model)
{
    try
    {
        _userRepository.UpdateUserProfile(model);
        TempData["Message"] = "Success";
        return RedirectToAction("Profiles",new { id= model.Id });
    }
    catch
    {
        ModelState.AddModelError(string.Empty,"Some error happened");
        return View(model);
    }
}

现在,在GET操作中(Profiles?id = someId),您基本上需要检查TempData值并根据需要显示它.

Now in your GET action(Profiles?id=someId), you basically need to check the TempData value and display it as needed.

在发生错误的情况下,可以在视图( Profiles )中使用 Html.ValidationSummary 辅助方法来显示我们添加到模型中的错误消息状态字典.

In the case of an error, in the view(Profiles), you may use the Html.ValidationSummary helper method to show the error message we added to the model state dictionary.

这篇关于数据库更新后返回成功/失败消息以供查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆