在MVC控制器里控制前台弹出对话框和基于Page的Web页面有些少出入,下面几种方法简单描述下:
单纯弹出:
return Content(@"<script>alert('xxxxxxxx');</script>");
弹出&转向:
var script = String.Format("<script>alert('修改成功!');location.href='{0}'</script>", Url.Action("Index"));
return Content(script, "text/html");
上个方法:
public ActionResult ShowAlertAndHref(string msg, string actionName, object[] obj=null)
{
var script = String.Format("<script>alert('{0}');location.href='{1}'</script>", msg,
Url.Action(actionName, obj));
return Content(script, "text/html");
}
调用:
public ActionResult Delete(Guid id)
{
if(2>1) return ShowAlertAndHref("删除成功!", "Index");
}
确认框 :
@Html.ActionLink("删除", "Delete", new { id = item.ID }, new { οnclick="return confirm('确定要删除?')"})