你可以做這樣的事情,如果你的目的是傳遞一個信息給客戶端,並顯示一個對話框:
在您看來,添加以下內容:
@using (Html.BeginForm("ShowBox","Home",FormMethod.Post, new { enctype = "multipart/form-data" })){
@Html.Hidden("message", "Your file has uploaded successfully.");
<input id="upload-button" type="submit" value="Click Me" />
<input id="file" name="file" type="file" value="Browse"/>
}
然後在你的控制器:
[HttpPost]
public ActionResult ShowBox(string message, HttpPostedFileBase file)
{
if (file == null || file.ContentLength == 0)
{
//override the message sent from the view
message = "You did not specify a valid file to upload";
}
else
{
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/App_Data/Uploads"));
file.SaveAs(path);
}
System.Text.StringBuilder response = new System.Text.StringBuilder();
response.Append("<script language='javascript' type='text/javascript'>");
response.Append(string.Format(" alert('{0}');", message));
response.Append(" var uploader = document.getElementById('upload-button'); ");
response.Append(" window.location.href = '/Home/Index/';");
response.Append("</script>");
return Content(response.ToString());
}
注: 我覺得這種做法是不太理想。我非常確定,從控制器直接返回JavaScript可能是某種反模式。至少,它感覺不對,甚至認爲它工作得很好。
MessageBox.Show不適用於asp.net –
不太確定答案,但是我馬上就會知道,如果你所做的是正確的,那麼你需要將「s」改爲「。 @ ModController.ShowBox(「Message」); ** to ** @ ModController.ShowBox('Message'); ** – Jieqin
啊,我明白了,然而這是一個測試,就像我在click事件上這樣運行一個方法,它是否正常工作? –