0

I can't figure out how to return a result from my controller to my view without completely overriding the content on the page.

Here is what I am doing:

View

@using (Html.BeginForm("FileUpload", "Test", FormMethod.Post, 
                            new { enctype = "multipart/form-data" }))
{  
    <label for="file">Upload Image:</label> 
    <input type="file" name="file" id="file" style="width: 100%;" /> 
    <input type="submit" value="Upload" class="submit" /> 
}

<div id="result"></div>


<script type="text/javascript">
    $(document).ready(function () {

    });
</script>

Controller:

[HttpPost]
        public JsonResult FileUpload(HttpPostedFileBase file)
        {
            if (file != null)
            {
                using (Image image = Image.FromStream(file.InputStream, true, true))
                {
                    if (image.Width >= 100 && image.Width <= 160 && image.Height >= 100 && image.Height <= 160)
                    {
                        string fileName = Path.GetFileName(file.FileName);
                        string path = Path.Combine(Server.MapPath(Url.Content("~/img/profile/")), fileName);
                        file.SaveAs(path);
                        ViewBag.Message = "success";
                        return Json("success");
                    }
                }
            }

            ViewBag.Message = "failed";
            return Json("failed");
        }

http://www.domainname.com/Test/Index

enter image description here

It changes to: http://www.domainname.com/Test/FileUpload and I get this:

enter image description here

or this:

enter image description here

I know how to return Json to a View when called via jquery such as

$("#btn1").click(function () {
            $.ajax({
                url: '@Url.Action("IndexPost", "Test")',
                type: "POST",
                data: {
                    str1: $("#txt1").val(),
                    str2: $("#txt2").val(),
                    str3: $("#txt3").val()
                },
                success: function (data) {
                    $("#txt4").val(data);
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert(XMLHttpRequest.responseText);
                },
                complete: function () { }
            });
        });

but I am lost when it comes to having to use @using (Html.BeginForm(...))

I can successfully upload an image, but the problem I need help with is how do I maintain the page as it is, but have it look like this instead?:

enter image description here

or

enter image description here

Edit:

View 2 (using same Controller action and switching JsonResult and ActionResult file is still always null regardless):

<form>
<label for="file">Upload Image:</label> 
    <input type="file" name="file" id="file" style="width: 100%;" /> 
    <input type="submit" value="Upload" id="btnSubmit" class="submit" /> 
</form>
<div id="result"></div>


<script type="text/javascript">
    $(document).ready(function () {
        $("#btnSubmit").click(function () {
            var formData = new FormData($("form").get(0));

            $.ajax({
                url: '@Url.Action("FileUpload", "Test")',
                type: "POST",
                data: formData,
                processData: false,
                contentType: false
            });

            return false;
        });
    });
</script>

Answer from Stephan Muecke: I was missing tags.

His answer: how to append whole set of model to formdata and obtain it in MVC

Community
  • 1
  • 1
Khaltazar
  • 296
  • 1
  • 7
  • `Html.BeginForm()` makes a normal post, leaves the page and renders the result of your method on a new page. If you want to stay on the same page then you nee to use ajax to submit –  Nov 13 '16 at 05:44
  • And if you want to upload an file input using ajax, refer [this answer](http://stackoverflow.com/questions/29293637/how-to-append-whole-set-of-model-to-formdata-and-obtain-it-in-mvc/29293681#29293681) –  Nov 13 '16 at 05:45
  • @StephenMuecke - I tried that answer before posting this, but it always passes null as HttpPostedFileBase file (which is why I have had to use @ using (Html.BeginForm()) because nothing else I did would get me away from endless nulls. – Khaltazar Nov 13 '16 at 06:28
  • Then you did not use the code in that answer :). Perhaps a new question showing your code and we will correct it. –  Nov 13 '16 at 06:29
  • @StephenMuecke - I'm not sure what code you need. I gave everything from the View to the Controller, that is all I have. I'll edit this question with what I tried from your answer. – Khaltazar Nov 13 '16 at 06:33
  • After the `$.ajax({..})` you need to add `return false;` (your button is a submit button and you need to cancel it or your code makes a normal submit and an ajax submit). And then you need to do something in the success callback with the result you returned. –  Nov 13 '16 at 06:37
  • @StephenMuecke - Added the return false; and it is still null when I get to the controller. file is always null. – Khaltazar Nov 13 '16 at 06:39
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/127976/discussion-between-stephen-muecke-and-khaltazar). –  Nov 13 '16 at 06:40

0 Answers0