asp.net mvc3里如何弹出javascript对话框?
/// <summary>
/// 从web服务器上下载文件
/// </summary>
/// <returns></returns>
public ActionResult DownFromDisk(string dataId)
{
try
{
if (dataId!="")
{
//这里有一些下载的代码
return new EmptyResult();
}
else
{
return Content("<script>alert('未找到文件ID!');</script>");
}
}
catch
{
return JavaScript("alert('数据下载时发生异常!');");
}
}
[解决办法]
使用JavaScriptResult,貌似只能在Ajax调用的时候使用,然后在页面上使用eval()来执行。
如果你是在A页面上的一个link,指向某一个Action,然后点击这个link就会下载文件。因为你不想在点击链接后刷新页面,当然你可以使用ajax和JavascriptResult, 也可以不使用ajax,使用JasonResult
public ActionResult DoSomething(){
return Json(new {isok=true, message="Your Message", data=...});
//True / False Json Return
//return UserObj == null ?
//Json(true, JsonRequestBehavior.AllowGet) :
//Json(string.Format("YourObject '{0}' to String", YourObject),
//JsonRequestBehavior.AllowGet);
}
//view
$.ajax
{
//code
success :function(returnvalue)
{
if(!returnvalue.isok)
{
window.alert("Error:" + returnvalue.message);
}
else
{
//do the stuff with returnvalue.data
}
} var script = String.Format("<script>alert('修改成功!');location.href='{0}'</script>", Url.Action("Index"));
return Content(script, "text/html");return Content("<script>alert('添加失败!');</script>");