Question

For now I am able to upload the file successfully... What I'm trying to do right now is show an alert box if the file was successfully uploaded or show an alert for the error/exception if not...

Here is my view:

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

Here is my 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 + "\");" };
    }
}

What i would like is that my view would still remain... no redirection of page... just merely showing of that alert box for the message...Thank you! Any ideas is greatly appreciated coz i know this way of mine is not recommended... :)

Was it helpful?

Solution 2

I was able to find the right solution here.

The link above was for showing a confirmation box before submitting the form and showing an alert for the message that was a json object returned by my controller...

return Json(new { isok = true, message = "Successfully uploaded the calendar XML file!" }, "application/octet-stream");

OTHER TIPS

This is what i've done to get the Alert from the controller

Here is the View Code:

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

Here is Controller Code:

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

Even there is no need of script, it directly shows alert when the get alert button is clicked

Hope it helps

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top