文件输入MVC 3客户端验证所需 [英] file input MVC 3 Client-side validation for required

查看:117
本文介绍了文件输入MVC 3客户端验证所需的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单的问题...是否有可能对文件类型的输入使用客户端MVC 3验证?

Simple question... Is it possible to use client side MVC 3 validation on inputs of type file?

要解释一下:MVC 3使用带有IClientValidatable和非侵入式JavaScript的模型验证,让你写在服务器端验证,并将它使用微软的插件使用jQuery验证渲染客户端。为了使一个属性需要添加以下

To explain: MVC 3 uses its model validation with IClientValidatable and unobtrusive javascript to allow you to write validation on the server side and have it render the client side using jquery validate using Microsoft's plugins. To make a property required you add the attribute below

[Required]
public HttpPostedFileBase CvFile {get; set;}

只要客户端VAL和不显眼的JavaScript是在配置本应在客户端上的所有火灾。

As long as client side val and unobtrusive javascript is on in the config this should all fire on the client.

HttpPostedFileBase (即<输入类型=文件名称=Model.CvFile/>)将不会运行需要在客户端。

However HttpPostedFileBase (i.e. <input type="file" name="Model.CvFile" />) will not run required on the client side.

任何想法如何可以做到这一点保持与服务器端验证的关系

Any ideas how this can be achieved keeping the relationship with the server side validation

推荐答案

答案很简单:A HttpPostedFileBase 渲染文件输入类型,它是一个安全问题,并且据我所知,是不是编写脚本。有此不支持开箱即用。

Simple answer: A HttpPostedFileBase renders "file" input type which is a security issue and, AFAIK, is not scriptable. There's no support for this "out of the box".

编辑:这似乎是一个很热门的话题在网上。
<一href=\"http://www.hanselman.com/blog/ABackToBasicsCaseStudyImplementingHTTPFileUploadWithASPNETMVCIncludingTestsAndMocks.aspx\" rel=\"nofollow\">http://www.hanselman.com/blog/ABackToBasicsCaseStudyImplementingHTTPFileUploadWithASPNETMVCIncludingTestsAndMocks.aspx

this seems to be a very popular topic online. http://www.hanselman.com/blog/ABackToBasicsCaseStudyImplementingHTTPFileUploadWithASPNETMVCIncludingTestsAndMocks.aspx

这篇关于文件输入MVC 3客户端验证所需的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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