Form Post with enctype = "multipart/form-data" causing parameters to not get passed
Solution 1
Since you're already posting data to the server I'd put the required parameter information in the form as hidden fields.
Asking the question "Can I send this collection of information to this URL without needing to populate other collections?"
With the querystring method the answer is no. I'd put it as a hidden field.
@Html.Hidden("SomeParameter", SomeValue);
Solution 2
Digging a little bit deeper i discovered that i can do:
@using (@Html.BeginForm(new{parameter1= Request["parameter1"]}, new {enctype = "multipart/form-data" }))
{
<text>Select a file </text>
<input type="file" name="file" />
<input type="submit" value="Upload" />
}
Notice how the Html.BeginForm differs from the initial.
Savvas Sopiadis
Updated on June 05, 2022Comments
-
Savvas Sopiadis almost 2 years
In an ASP.NET MVC 3 (Razor) project i 'm trying to upload a picture: the relevant part of the view:
@using (@Html.BeginForm( new {enctype = "multipart/form-data" })) { <text>Select a file </text> <input type="file" name="file" /> <input type="submit" value="Upload" /> }
stating explicitly the enctype parameter is "responsible" for stripping off the parameter part. For example, if the URL (opening the view) was the following:
mydomain/Controller/Action/id?parameter1=somevalue1
the BeginForm-statement in the form above would give (posting back) the following:
mydomain/Controller/Action/id
thus stripping off the part: ?parameter1=somevalue1 which is needed!
How do i take care of this?
-
Savvas Sopiadis almost 12 yearsWhat should be preferred: your method or the way i 'm suggesting?
-
Jamie Dixon almost 12 yearsPersonally I prefer this method since you can take a collection of data and post it to your action method knowing that you'll get an expected result. With the querystring method, you can't be confident that your server post will be enough because a value in the querystring collection is also required. If this was a RESTful service you'd also want all the required data in the post.