保存数据后向用户显示消息

时间:2019-04-24 13:56:48

标签: c# asp.net-mvc razor

在保存数据后,我想向用户显示一条成功消息。我在控制器中创建一个Viewbag并在视图中调用它。单击保存按钮后,将保存数据,但不会出现成功消息。它立即重定向到索引页面。下面是我的代码,谢谢

控制器

  [HttpPost]
  [ValidateAntiForgeryToken]
  public ActionResult Create(ParamNoRekeningSumberDana model)
  {
     ViewBag.SuccessCreate = "";
     if (ModelState.IsValid)
     {
          ParamNoRekeningSumberDana newrecord = new ParamNoRekeningSumberDana();
          newrecord.ID = Guid.NewGuid();
          newrecord.AccountNo = model.AccountNo;
          newrecord.CreatedBy = Session["UserName"].ToString();

          db.ParamNoRekeningSumberDanas.Add(newrecord);
          db.SaveChanges();
          ViewBag.SuccessCreate = "Saved successfully";

          return RedirectToAction("Index");
       }
       else{
          return View(model);
       }
  }

查看

@{
  if (ViewBag.SuccessCreate != null){
    <script type="text/javascript">
        var temp = @Html.Raw(Json.Encode(ViewBag.SuccessCreate));
        alert(temp);
    </script>
  }
}
@using (Html.BeginForm())
{
    <div class="form-group">
        @Html.LabelFor(model => model.AccountNo, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownListFor(model => model.AccountNo, (List<SelectListItem>)ViewBag.AccountNos, new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.AccountNo, "", new { @class = "text-danger" })
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create" class="btn btn-default" />
        </div>
    </div>
}

2 个答案:

答案 0 :(得分:0)

控制器

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(ParamNoRekeningSumberDana model)
{
    if (ModelState.IsValid)
    {
        ParamNoRekeningSumberDana newrecord = new 
        ParamNoRekeningSumberDana();
        newrecord.ID = Guid.NewGuid();
        newrecord.AccountNo = model.AccountNo;
        newrecord.CreatedBy = Session["UserName"].ToString();

        db.ParamNoRekeningSumberDanas.Add(newrecord);
        db.SaveChanges();
        TempData["SuccessCreate"] = "Saved successfully";

        return RedirectToAction("Index");
   }
   else
   {
         return View(model);
   }

}

查看

  @{
     if (ViewBag.SuccessCreate != null){
       <script type="text/javascript">
        var temp = '@TempData["SuccessCreate"]';
        alert(temp);
       </script>
     }
  }
  @using (Html.BeginForm())
  {
    <div class="form-group">
        @Html.LabelFor(model => model.AccountNo, htmlAttributes: new { @class 
         = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownListFor(model => model.AccountNo, 
   (List<SelectListItem>)ViewBag.AccountNos, new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.AccountNo, "", new { 
     @class = "text-danger" })
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create" class="btn btn-default" />
        </div>
    </div>
   }

答案 1 :(得分:0)

看看这个问题(Set Viewbag before Redirect),他们解释说在这种情况下您应该在Viewbag上使用TempData。

返回View时,Viewbag stil适用于View上下文。但是,当您使用RedirectToAction时,它实际上是HTTP重定向,因此.NET在创建另一个要在此新页面上使用的实例时“清除”了ViewBag。

您可以在此处了解更多信息:https://www.c-sharpcorner.com/blogs/viewdata-vs-viewbag-vs-tempdata-in-mvc1