0

I came across the following issue: I have this model

public class Region
{
    public int RegionID { get; set; }
    public string RegionName { get; set; }
    public HttpPostedFile CustomFile { get; set; }
}

and I have a custom extension html helper method from here.

Now in the view I have the following code:

<%using (Html.BeginForm("ModifyRegion", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))

{%>

  <%: Html.TextAreaFor(x => x.RegionID, Model.RegionID)%>
  <br />
  <%: Html.TextAreaFor(x => x.RegionName, Model.RegionName)%>
  <br />

 <%: Html.FileBoxFor(x=>x.CustomFile, Model.CustomFile) %>

<input type="submit" />

When it reaches my controller action the CustomFile field is empty, but all other fields are properly set. If I change the CustomFile property to string instead of HttpPostedFile, I get correctly the file name ("dog.jpg" for example). Is there any way to get the full HttpPostedFile file correctly?

Thanks in advance, Tamash

2 Answers 2

1

Try replacing HttpPostedFile by HttpPostedFileBase in your view model:

public class Region
{
    public int RegionID { get; set; }
    public string RegionName { get; set; }
    public HttpPostedFileBase CustomFile { get; set; }
}

Also make sure that the custom FileBoxFor helper you are using generates correct name and type of the corresponding file field:

<input name="CustomFile" type="file" />
Sign up to request clarification or add additional context in comments.

4 Comments

Tried that. The CustomFile property is still null. The generated HTML is : <form action="/Home/ModifyRegion" method="post"><form action="/Home/ModifyRegion" enctype="multipart/form-data" method="post"> 0</textarea> <br /> <textarea cols="20" id="RegionName" name="RegionName"rows="2"> </textarea> <br /> <input name="CustomFile" type="file" value="Browse" /> <input type="submit" /> </form></form>
@TamasIonut, seems like you have 2 nested forms which is invalid HTML. You will have to remove the outer form and leave only the one with the enctype="multipart/form-data" attribute.
Thanks a lot. I had a partial view which declared a form itself and was embedded into my view into another form . (Don't know too much about HTML I guess ;)) )
And also if I change the type to HttpPostedFile it doesn't work. Only with HttpPostedFileBase. Good to know that too.
0

You can use something like :

< input name="CustomFile" type="file" value="Browse" />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.