Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass other form data along with MVC File Upload?

I am trying to implement File Upload in MVC. I have the following code which works.

@using (Html.BeginForm("ActioName", "ControllerName", FormMethod.Post, new { enctype = "multipart/form-data" }))
{         
       <div>
            <input type="file" name="file" />
            <input type="submit" value="OK" class="button" />
        </div>       
}

      [HttpPost]
      public ActionResult UploadFile(HttpPostedFileBase file)
       {
        // Verify that the user selected a file
        if (file != null && file.ContentLength > 0)
        {
        //do something here...
        }
      }

Now I want to add a dropdown box (to select the file type) and send that value along with the file to my Controller. How can I do that (send other form data along with the file)?

like image 244
Gadam Avatar asked Dec 12 '22 09:12

Gadam


1 Answers

You should be able to add them to the view, include them in the POST and have MVC take care of the model binding:

@using (Html.BeginForm("ActioName", "ControllerName", FormMethod.Post, new { enctype = "multipart/form-data" }))
{         
       <div>
            <input type="file" name="file" />
            <select name="fileType">
               <option value="JPG">Photo</option>
               <option value="DOC">Word</option>
            </select>
            <input type="submit" value="OK" class="button" />
        </div>       
}

      [HttpPost]
      public ActionResult UploadFile(HttpPostedFileBase file, string fileType)
      {
        //Validate the fileType

        // Verify that the user selected a file
        if (file != null && file.ContentLength > 0)
        {
        //do something here...
        }
      }
like image 136
Cloud SME Avatar answered Dec 28 '22 12:12

Cloud SME