简体   繁体   English

如何使用ASp.net MVC3插入数据库后显示“成功插入”等警报消息

[英]How to show Alert Message like “successfully Inserted” after inserting to DB using ASp.net MVC3

How to write a code for displaying the alert message: "Successfully registered", after user data is stored in database, using MVC 如何使用MVC编写用于显示警报消息的代码:“成功注册”,用户数据存储在数据库中之后

I am using Asp.Net MVC3, C#, Entity Model. 我使用的是Asp.Net MVC3,C#,Entity Model。

Try using TempData : 尝试使用TempData

public ActionResult Create(FormCollection collection) {
  ...
  TempData["notice"] = "Successfully registered";
  return RedirectToAction("Index");
  ...
}

Then, in your Index view, or master page, etc., you can do this: 然后,在索引视图或母版页等中,您可以执行以下操作:

<% if (TempData["notice"] != null) { %>
  <p><%= Html.Encode(TempData["notice"]) %></p>
<% } %>

Or, in a Razor view: 或者,在Razor视图中:

@if (TempData["notice"] != null) {
  <p>@TempData["notice"]</p>
}

Quote from MSDN (page no longer exists as of 2014, archived copy here ): 从MSDN引用(截至2014年页面已不存在, 此处存档副本):

An action method can store data in the controller's TempDataDictionary object before it calls the controller's RedirectToAction method to invoke the next action. action方法可以在调用控制器的RedirectToAction方法调用下一个操作之前将数据存储在控制器的TempDataDictionary对象中。 The TempData property value is stored in session state. TempData属性值存储在会话状态中。 Any action method that is called after the TempDataDictionary value is set can get values from the object and then process or display them. 在设置TempDataDictionary值之后调用的任何操作方法都可以从对象获取值,然后处理或显示它们。 The value of TempData persists until it is read or until the session times out. TempData的值一直存在,直到读取或会话超时为止。 Persisting TempData in this way enables scenarios such as redirection, because the values in TempData are available beyond a single request. 以这种方式保持TempData可以启用重定向等方案,因为TempData中的值可以在单个请求之外使用。

The 'best' way to do this would be to set a property on a view object once the update is successful. 执行此操作的“最佳”方法是在更新成功后在视图对象上设置属性。 You can then access this property in the view and inform the user accordingly. 然后,您可以在视图中访问此属性并相应地通知用户。

Having said that it would be possible to trigger an alert from the controller code by doing something like this - 已经说过可以通过做这样的事情从控制器代码触发警报 -

public ActionResult ActionName(PostBackData postbackdata)
{
    //your DB code
    return new JavascriptResult { Script = "alert('Successfully registered');" };
}

You can find further info in this question - How to display "Message box" using MVC3 controller 您可以在此问题中找到更多信息 - 如何使用MVC3控制器显示“消息框”

Personally I'd go with AJAX. 就个人而言,我会选择AJAX。

If you cannot switch to @Ajax... helpers, I suggest you to add a couple of properties in your model 如果您无法切换到@Ajax...助手,我建议您在模型中添加几个属性

public bool TriggerOnLoad { get; set; }
public string TriggerOnLoadMessage { get; set: }

Change your view to a strongly typed Model via 将您的视图更改为强类型模型

@using MyModel

Before returning the View, in case of successfull creation do something like 在返回View之前,如果成功创建,请执行类似的操作

MyModel model = new MyModel();
model.TriggerOnLoad = true;
model.TriggerOnLoadMessage = "Object successfully created!";
return View ("Add", model);

then in your view, add this 然后在您的视图中,添加此项

@{
   if (model.TriggerOnLoad) {
   <text>
   <script type="text/javascript">
     alert('@Model.TriggerOnLoadMessage');
   </script>
   </text>
   }
}

Of course inside the tag you can choose to do anything you want, event declare a jQuery ready function: 当然在标签内你可以选择做任何你想做的事情,事件声明一个jQuery就绪函数:

$(document).ready(function () {
   alert('@Model.TriggerOnLoadMessage');
});

Please remember to reset the Model properties upon successfully alert emission. 请记住在成功发出警报后重置模型属性。

Another nice thing about MVC is that you can actually define an EditorTemplate for all this, and then use it in your view via: 关于MVC的另一个好处是你可以为所有这些定义一个EditorTemplate,然后通过以下方式在你的视图中使用它:

@Html.EditorFor (m => m.TriggerOnLoadMessage)

But in case you want to build up such a thing, maybe it's better to define your own C# class: 但是如果你想构建这样的东西,也许最好定义你自己的C#类:

class ClientMessageNotification {
    public bool TriggerOnLoad { get; set; }
    public string TriggerOnLoadMessage { get; set: }
}

and add a ClientMessageNotification property in your model. 并在模型中添加ClientMessageNotification属性。 Then write EditorTemplate / DisplayTemplate for the ClientMessageNotification class and you're done. 然后为ClientMessageNotification类编写EditorTemplate / DisplayTemplate,您就完成了。 Nice, clean, and reusable. 很好,干净,可重复使用。

Little Edit 小编辑

Try adding 尝试添加

return new JavascriptResult() { Script = "alert('Successfully registered');" };

in place of 代替

return RedirectToAction("Index");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM