0

I already tried all solutions that I found on StackOverflow but they are not working for me. I also tried Tetsuya Yamamoto solution but still its always returning null when I am using it with model.

Update

When I am inspecting it then my file type input hold data but in inspected element its value is ""

enter image description here

In both ways my posted file is like this

 @using (Html.BeginForm("AddLocation", "MasterData", FormMethod.Post, new { encytype = "multipart/form-data"}))
{
 <div class="file-upload">
          <input type="file" name="postedFile" />
   </div>

//passing model value when using it
}

without model working perfact

 public ActionResult AddLocation(HttpPostedFileBase file)
        {
            try
            {

              if (file != null) //Working Perfact
               {

               }              
                return View(model);
            }
            catch (Exception ex)
            {
                return View(model);
                throw;
            }

        }

with model always rerunning null

public ActionResult AddLocation(LocationModel model, HttpPostedFileBase file)
        {
            try
            {
                if (ModelState.IsValid)
                {                    
                    if (file != null) //Always return null when passing with model
                    {

                    }
                }
                return View(model);
            }
            catch (Exception ex)
            {
                return View(model);
                throw;
            }

        }
piet.t
  • 11,718
  • 21
  • 43
  • 52
ArunPratap
  • 4,816
  • 7
  • 25
  • 43

3 Answers3

3

Just add HttpPostedFileBase property inside the viewmodel class which has same name as in <input type="file" /> element:

public class LocationModel
{
    // other properties
    public HttpPostedFileBase PostedFile { get; set; }
}

Then remove second parameter from controller action:

[HttpPost]
public ActionResult AddLocation(LocationModel model)
{
    // do something
}

And replace the <input> file element with strongly-typed binding:

@Html.TextBoxFor(model => model.PostedFile, new { type = "file" })

Afterwards, your file should be available on corresponding viewmodel property.

Side note:

There is a typo on the form definition, which should use enctype instead of encytype:

@model LocationModel

@using (Html.BeginForm("AddLocation", "MasterData", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <div class="file-upload">
        @Html.TextBoxFor(model => model.PostedFile, new { type = "file" })
    </div>
}

Reference:

mvc upload file with model - second parameter posted file is null

Tetsuya Yamamoto
  • 24,297
  • 8
  • 39
  • 61
  • i already tried that and also tried again but still if (model.PostedFile != null) returning null – ArunPratap Dec 13 '18 at 08:25
  • Did you follow those steps above, is that `model.PostedFile.FileName` also returns null? If you have nested forms with parent form not set to `enctype = "multipart/form-data"`, the same problem occurs. – Tetsuya Yamamoto Dec 13 '18 at 08:36
  • i tried as u told and also removed enctype = "multipart/form-data" but still if (model.PostedFile.FileName != null) geting null – ArunPratap Dec 13 '18 at 09:31
0

You can add HttpPostedFileBase file in side the model and it should get the file.

public class LocationModel 
{
    ....
    public HttpPostedFileBase File{ get; set; }
}

Reason is multiple post parameters are not supported in asp.net mvc

cdev
  • 5,043
  • 2
  • 33
  • 32
0

@ArunPratap this issue is quite famous in asp.net mvc, actually the POST method acquires for a file named the same as the given Id name in the view, as the form data is sent through the headers, and by default it accepts anything coming from the form but as you send more than one data the corresponding POST method requires the same name given as it doesn't identify the data coming from the form.

Example in this case:

@using(Html.BeginForm("AddLocation", "MasterData", FormMethod.Post, new {enctype = "multipart/form-data" }))
{
    <div class="file-upload">
        @Html.TextBoxFor(model => model.PostedFile, new { type = "file" })
    </div>
}

whatever you name the parameter of the POST method it identifies it as the file but when extend the form/model with other fields it lacks the capability to detect the file around. For best practices use strongly typed ViewModels as @TetsuyaYamamoto has recommended.