Click here to Skip to main content
15,888,121 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am using mvc in my project, I have done sample registration form with model level validations

In my form I have first name, last name, mobile number and member document details, all are required fields except document details

After Entering the member details I have a button(add document), once I click on it, it will display a textbox, fileupload button i have done this functionality using jquery,after that i will enter the document name in textbox and browse the document using fileupload control, up to here the functionality is working fine.

But after this on post action if any validation occurs in model level for the required fields, the fileupload control is disappearing,so each time again i have to upload the file by clicking the button(add new doc) , is there any alternate to remain the uploaded file on the form?

What I have tried:

My jquery file upload functionality

<table id="doc" class="table table-striped table-hover table-bordered">

<tr>
<th style="width:200px">
Document Name
</th>

<th style="width:500px">
File to Upload
</th>


<th style="width:6px">

</th>


</tr>

@if (Model != null)
{
if (Model.table_dtls_doc != null)
{
for (int i = 0; i < Model.table_dtls_doc .Count; i++)
{

<tr>
<td>
@Html.HiddenFor(model => Model.table_dtls_doc [i].detail_id);
@Html.HiddenFor(model => table_dtls_doc[i].id);
@Html.TextBoxFor(model => model.table_dtls_doc [i].document_name)</td>
<td>
@Html.TextBoxFor(model => model.table_dtls_doc [i].UpFile, new { type = "file" })
<input type="hidden" name="table_dtls_doc .Index" value="@i" />
</td>


</tr>
}
}
}

</table>
<button type="button" name="N" id="N" value="N" class="">Add New Doc </button>
Posted
Updated 23-Mar-17 23:12pm
Comments
j snooze 23-Mar-17 17:16pm    
Either move your validation to javascript or ajax so that a postback on your page isn't done, or upload your file no matter what on postback are a couple options.

You have to need save form using ajax request and get response. If model side validations successfully performed then save data and refresh the page otherwise keep form as its and show error messages only.

you can use Ajax.BeginForm() on view or simple ajax request using j-query to do this.
 
Share this answer
 
You can't persist the file upload control between posts for security reasons. If you want to upload a file then split your process into a two-step one. First allow people to enter all their details, and once that is done show them a form where they can upload their file.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900