Mvc文件上载功能在模型级验证上失败 [英] Mvc file upload functionaity is failing on model level validation

查看:61
本文介绍了Mvc文件上载功能在模型级验证上失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的项目中使用mvc,我已经完成了模型级验证的样本注册表格



在我的表格中我有名字,姓氏,手机号码和会员文档的详细信息,除了文档详细信息之外都是必填字段



输入会员详细信息后我有一个按钮(添加文档),一旦我点击它,它将会显示一个文本框,fileupload按钮我已经使用jquery完成了这个功能,之后我将在文本框中输入文档名称并使用fileupload控件浏览文档,到目前为止功能正常。



但是在post post后如果在模型级别对所需字段进行任何验证,则fileupload控件正在消失,所以每次我都要通过单击按钮上传文件(添加新文档) ,有什么替代方法可以保留表格上传的文件吗?



我尝试了什么:



我的jquery文件上传f功能性



< table id =docclass =table table-striped table-hover table-bordered>



< tr>

< th style =width:200px>

文件名

< ; / th>



< th style =width:500px>

要上传的文件

< / th>





< th style =width:6px>



< / th>





< / tr>



@if(莫del!= null)

{

if(Model.table_dtls_doc!= null)

{

for( int i = 0;我< 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 =hiddenname =table_dtls_doc .Indexvalue =@ i/>

< / td>





< / tr>

}

}

}



< / table>

< button type =buttonname =Nid =Nvalue =Nclass =>添加新的Doc< / button>

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>

推荐答案

您需要使用ajax请求保存表单并获得响应。如果模型侧验证成功执行,则保存数据并刷新页面,否则保持形式为其并仅显示错误消息。



您可以在视图上使用Ajax.BeginForm()或使用j-query执行此操作的简单ajax请求。
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.


出于安全原因,您无法在帖子之间保留文件上载控件。如果要上传文件,请将流程拆分为两步。首先允许人们输入他们的所有细节,一旦完成,就会向他们展示一个他们可以上传文件的表格。
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.


这篇关于Mvc文件上载功能在模型级验证上失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆