我创建了一个html helper,它可以帮助我显示jquery模式对话框:我在控制器的tempdata中设置了一条消息,如果消息不为空,helper会编写jquery+html代码,以便在回发后生成popup apears。
但我需要将验证结果显示为消息(与validationsummary显示的消息相同),我不知道如何完成这一点。有人能帮我吗?我这样做对吗?
我的助手:
[...]
public static string ModalDialogNotifier(this HtmlHelper helper)
{
string message = "";
if (helper.ViewContext.TempData["message"] != null)
message = helper.ViewContext.TempData["message"].ToString();
if (!String.IsNullOrEmpty(message))
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("<script>$(document).ready(function() {$.blockUI({ message: $('#mdiag')});$('#mdiagok').click(function() {$.unblockUI();return false;});})</script>");
sb.AppendFormat("<div id='mdiag'>{0}<input type='button' id='mdiagok' value='Ok' /></div>", message);
return sb.ToString();
}
return string.Empty;
}
[...]
我的控制器:
[HttpPost]
[Authorize(Roles = "Admin")]
public ActionResult Create(CreateUserModel Model)
{
if (!ModelState.IsValid)
{
this.TempData["message"] = "Model is not valid";
}
else
{
[...]
}
return View(Model);
}
我的看法是:
[...]<%= Html.ModalDialogNotifier()%>[...]