public static MvcHtmlString InputFile<TModel, TValue>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TValue>> expression)
{
string modelName = ExpressionHelper.GetExpressionText(expression);
//从Lambda表达式中获取模型对应属性的名称
TagBuilder tagBuilder = new TagBuilder("input");
//设置标签类型为input
tagBuilder.Attributes.Add("type", "file");
//为标签添加type属性及值
tagBuilder.Attributes.Add("name", modelName);
//为标签添加name属性及值
tagBuilder.GenerateId(modelName);
//为标签生成Id,name参数代码Id的值
//创建经过HTML加密的字符串
//TagRenderMode.SelfClosing枚举值代表当前标签是自动关闭的
ModelState modelState;
if (htmlHelper.ViewData.ModelState.TryGetValue(modelName, out modelState))
{
if (modelState.Errors.Count > 0)
{ //添加错误提示CSS
tagBuilder.AddCssClass(HtmlHelper.ValidationInputCssClassName);
}
}
return MvcHtmlString.Create(tagBuilder.ToString(TagRenderMode.SelfClosing));
}
cshtml:
@Html.InputFile(model => model.FilePath)
control:
[HttpPost]
public ActionResult UploadFileCreate(SecUploadFileModel model)问题: 为什么我取的值是model.FilePath="HttpPostedFileWrapper"
{
string modelName = ExpressionHelper.GetExpressionText(expression);
//从Lambda表达式中获取模型对应属性的名称
TagBuilder tagBuilder = new TagBuilder("input");
//设置标签类型为input
tagBuilder.Attributes.Add("type", "file");
//为标签添加type属性及值
tagBuilder.Attributes.Add("name", modelName);
//为标签添加name属性及值
tagBuilder.GenerateId(modelName);
//为标签生成Id,name参数代码Id的值
//创建经过HTML加密的字符串
//TagRenderMode.SelfClosing枚举值代表当前标签是自动关闭的
ModelState modelState;
if (htmlHelper.ViewData.ModelState.TryGetValue(modelName, out modelState))
{
if (modelState.Errors.Count > 0)
{ //添加错误提示CSS
tagBuilder.AddCssClass(HtmlHelper.ValidationInputCssClassName);
}
}
return MvcHtmlString.Create(tagBuilder.ToString(TagRenderMode.SelfClosing));
}
cshtml:
@Html.InputFile(model => model.FilePath)
control:
[HttpPost]
public ActionResult UploadFileCreate(SecUploadFileModel model)问题: 为什么我取的值是model.FilePath="HttpPostedFileWrapper"
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货