现在我可以成功上传文件了……我现在要做的是在文件上传成功时显示一个警告框,否则显示错误/异常的警告……
这是我的看法:
using (Html.BeginForm("Upload", "Home", FormMethod.Post, new { @id = "File", enctype = "multipart/form-data"}))
{
<div class="control-group">
<input type="file" id="file" name="file" />
<input type="submit" value="Upload" />
</div>
}
这是我的 Controller :
[HttpPost]
public ActionResult Upload(HttpPostedFileBase file)
{
try
{
//some code here for the upload....
/*In this part is my problem...Both Lines below is not showing the alert...*/
//return new JavaScriptResult() { Script = "alert('The calendar XML file was uploaded successfully!');" };
//return JavaScript("alert('The calendar XML file was uploaded successfully!');");
}
catch (Exception e)
{
log.Error("HomeController::Upload()", e);
return new JavaScriptResult() { Script = "alert(\"" + e.Message + "\");" };
}
}
我想要的是我的 View 仍然存在...没有页面重定向...只是显示消息的警告框...谢谢!非常感谢任何想法,因为我知道不推荐我的这种方式......:)
最佳答案
这就是我从 Controller 获取警报所做的工作
查看代码如下:
@using (@Html.BeginForm("DoSomething","secure"))
{
<input type="submit" value="get alert" />
}
Controller 代码如下:
[HttpPost]
public ActionResult DoSomething()
{
string message = "hai";
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append("<script type = 'text/javascript'>");
sb.Append("window.onload=function(){");
sb.Append("alert('");
sb.Append(message);
sb.Append("')};");
sb.Append("</script>");
//return Content("<script type='text/javascript'>alert('Hello there');</script>"); //You can get the alert with this line also
return Content(sb.ToString(), "text/javascript");
}
即使不需要脚本,点击get alert
按钮也直接显示alert
希望对你有帮助
关于javascript - 如何在 ASP.net MVC 3 中成功上传文件后显示警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14194158/