C# 使用自定义Html帮助程序时,HttpPostedFileBase始终为空
我为file(file和FileFor)创建了一个HTML帮助程序,如下所示:C# 使用自定义Html帮助程序时,HttpPostedFileBase始终为空,c#,asp.net-mvc,file-upload,C#,Asp.net Mvc,File Upload,我为file(file和FileFor)创建了一个HTML帮助程序,如下所示: public static IHtmlString File(this HtmlHelper html, String text, String name, IDictionary<String, Object> attributes) { // Code snip TagBuilder input = new TagBuilder("input"); input.MergeAtt
public static IHtmlString File(this HtmlHelper html, String text, String name, IDictionary<String, Object> attributes)
{
// Code snip
TagBuilder input = new TagBuilder("input");
input.MergeAttribute("type", "file");
input.MergeAttribute("name", name, true);
input.GenerateId(name);
return // ...
}
public static IHtmlString FileFor<TModel, TProperty>(this HtmlHelper<TModel> html, Expression<Func<TModel, TProperty>> expression, String text)
where TModel : class
{
return FileFor(html, expression, text, null);
}
问题在于,在这种情况下,ASP.NET将输入呈现为:
<input type="file" name="Advert.Image" id="Advert_Image">
我的Html助手将其呈现为:
<input type="file" name="Image" id="Image">
如果我不上儿童课,那也可以
我怎样才能解决这个问题
更新
我的FileFor代码:
public static IHtmlString FileFor<TModel, TProperty>(this HtmlHelper<TModel> html, Expression<Func<TModel, TProperty>> expression, String text, IDictionary<String, Object> attributes) where TModel : class {
ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
String field = ExpressionHelper.GetExpressionText(expression);
String name = html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(field);
return File(html, text, metadata.DisplayName ?? metadata.PropertyName ?? field, attributes == null ? new RouteValueDictionary() : attributes);
}
公共静态IHtmlString文件(此HtmlHelper html、表达式、字符串文本、IDictionary属性),其中TModel:class{
ModelMetadata=ModelMetadata.FromLambdaExpression(表达式,html.ViewData);
字符串字段=ExpressionHelper.GetExpressionText(表达式);
字符串名称=html.ViewContext.ViewData.TemplateInfo.GetFullHtmlField(字段);
返回文件(html、文本、metadata.DisplayName??metadata.PropertyName??字段,属性==null?新RouteValueDictionary():属性);
}
更改
metadata.DisplayName ?? metadata.PropertyName ?? field
到
我认为它选择的是DisplayName而不是PropertyName。请发布
FileFor
如何从表达式中读取名称是的,我刚刚更新以包含该代码。基本上我使用的是:String name=html.ViewContext.ViewData.TemplateInfo.getFullHtmlField(字段);
metadata.DisplayName ?? metadata.PropertyName ?? field
metadata.PropertyName