【发布时间】:2015-08-11 19:33:44
【问题描述】:
我有一个带有以下 div 的 .cshtml Razor 视图
<div id="software_updates" class="htmlCode tab-pane fade">
@using (Html.BeginForm(new { @class = "form-horizontal", role = "form" }))
{
@Html.AntiForgeryToken()
<div id="update_text_content" class="active fade in">
<h2>Software Update Notification Service</h2>
<h4>
This function will send all registered users
a notification of a revision/update to the existing software. The message
they receive will be the following:
</h4>
@{
var updateMessage =
String.IsNullOrEmpty(Model.ExampleUpdateMessage) ?
String.Empty :
Model.ExampleUpdateMessage;
}
<pre>@updateMessage</pre>
@Ajax.ActionLink(
"Send Update Notification",
"SendUpdateNotifications",
"Tools",
null,
new AjaxOptions
{ HttpMethod = "GET", OnSuccess = "onSuccess" },
new
{
onclick = String.Format(
"return confirm('Are you sure you want to send a " +
"revision notification to the {0:N0} registered {1}?');",
@Model.UserList.Count,
"user".Pluralize(@Model.UserList.Count)),
@class = "btn btn-lg btn-danger",
role = "button",
onSuccess = "onCompletion"
})
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<span id="notify_failure_message" class="label label-danger"></span>
<span id="notify_success_message" class="label label-success"></span>
</div>
</div>
</div>
}
</div>
然后我的控制器中有一些 C# 代码返回 ActionResult/JsonResult
[AllowAnonymous]
public async Task<JsonResult> SendUpdateNotifications()
{
string result = String.Empty;
try
{
... // Some stuff.
return Json(new
{
notify_success = result.ConvertToHtmlString(),
notify_failure = String.Empty
}, JsonRequestBehavior.AllowGet);
}
catch (Exception e)
{
result = String.Format(
"DrGroup Revision/Update notifications failed to send\n" +
"See the invite Log for more information.\n" +
"Error: {0}",
e.Message);
return Json(new
{
notify_success = String.Empty,
notify_failure = result.ConvertToHtmlString()
}, JsonRequestBehavior.AllowGet);
}
}
我有以下 javascript 函数来更新我的 <span id="notify_failure_message" ... 中的文本。
@section scripts {
<script>
function onSuccess(result) {
$('#notify_failure_message').html(result.notify_failure);
$('#notify_success_message').html(result.notify_success);
}
</script>
}
当我点击我的按钮时,我的方法会触发并返回 Json 对象
return Json(new
{
notify_success = result.ConvertToHtmlString(),
notify_failure = String.Empty
}, JsonRequestBehavior.AllowGet);
但是,我的 java 脚本函数 onSuccess 似乎从未被使用过,我想知道原因。我做错了什么,我该如何解决?
感谢您的宝贵时间。
【问题讨论】:
-
onSuccess不属于AjaxOptions而不属于 HtmlAttributes 吗? -
天啊。这是漫长的一天...非常感谢您的宝贵时间。
-
虽然这是朝着正确方向迈出的一大步,但我仍然没有让我的
<span>s 更新?
标签: c# asp.net html asp.net-mvc-4 razor