i have a form that contains a few textboxes and an upload entry. On submit, i do some server side validation that hits the database to make sure the one field of the fields is not a duplicate. The problem i am running into is if the user uploads a file and
enters an invalid entry for the other fields the upload disappears on postback. is there an easy use to retain that information? otherwise the user will have to go an upload the file again.
rmcclain
Member
48 Points
30 Posts
asp.net mvc form validationHttpPostedFileBase
Mar 06, 2011 08:17 PM|LINK
i have a form that contains a few textboxes and an upload entry. On submit, i do some server side validation that hits the database to make sure the one field of the fields is not a duplicate. The problem i am running into is if the user uploads a file and enters an invalid entry for the other fields the upload disappears on postback. is there an easy use to retain that information? otherwise the user will have to go an upload the file again.
SreejithGopi...
Participant
922 Points
148 Posts
Re: asp.net mvc form validationHttpPostedFileBase
Mar 07, 2011 12:48 AM|LINK
You can keep the file in TempData
public ActionResult Index(HttpPostedFileBase fileUpload) { TempData["fileUpload"] = fileUpload; }In subsequent post you can access like
raduenuca
All-Star
24675 Points
4250 Posts
Re: asp.net mvc form validationHttpPostedFileBase
Mar 07, 2011 04:39 AM|LINK
If you're using MVC3 try to use RemoteValidation:
http://www.aaronstannard.com/post/2010/12/07/remote-validation-asp-net-mvc3.aspx
Radu Enuca | Blog
rmcclain
Member
48 Points
30 Posts
Re: asp.net mvc form validationHttpPostedFileBase
Mar 07, 2011 02:27 PM|LINK
thanks for the response. i was thinking about storing it into a session but after reading the link below i felt this wasn't the way to go.
http://stackoverflow.com/questions/4747833/cannot-save-httppostedfilebase-to-session-variable-and-use-twice
rmcclain
Member
48 Points
30 Posts
Re: asp.net mvc form validationHttpPostedFileBase
Mar 07, 2011 02:29 PM|LINK
thanks for the response.
wow remotevalidation looks like it would work but i'm not using MVC3. We are looking into upgrading to this version for future projects.
raduenuca
All-Star
24675 Points
4250 Posts
Re: asp.net mvc form validationHttpPostedFileBase
Mar 07, 2011 02:33 PM|LINK
Just download the source code for MVC3 and look at the RemoteAttribute implementation. See if you can adapt the code to work in MVC2
Radu Enuca | Blog
counsellorbe...
Member
540 Points
112 Posts
Re: asp.net mvc form validationHttpPostedFileBase
Mar 07, 2011 03:17 PM|LINK
Here is a link to an article from Brad Wilson showing how to add remote validation to MVC 2: http://bradwilson.typepad.com/blog/2010/01/remote-validation-with-aspnet-mvc-2.html.
counsellorben