3

现在我能够成功上传文件......我现在要做的是在文件成功上传时显示一个警报框,如果没有,则显示错误/异常警报......

这是我的看法:

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>
        }

这是我的控制器:

[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 + "\");" };
    }
}

我想要的是我的观点仍然存在......页面没有重定向......只是显示该消息的警报框......谢谢!非常感谢任何想法,因为我知道不推荐我的这种方式...... :)

4

2 回答 2

1

这就是我为从控制器获取警报所做的工作

这是查看代码:

@using (@Html.BeginForm("DoSomething","secure"))
{
    <input type="submit" value="get alert" />
}

这是控制器代码:

[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点击按钮时直接显示警报

希望能帮助到你

于 2013-01-07T12:48:26.090 回答
1

我能够在这里找到正确的解决方案。

上面的链接用于在提交表单之前显示一个确认框,并显示一个警报,该消息是我的控制器返回的 json 对象...

return Json(new { isok = true, message = "Successfully uploaded the calendar XML file!" }, "application/octet-stream");
于 2013-01-08T09:14:43.253 回答