我的模型是
public class FileSelectControl
{
//property for selected file path value
public string FilePath { get; set; }
}
看法
@Html.FileBoxFor(x => x.FilePath)
其中 FileBoxFor 是一个自定义 HTML 帮助器类,它返回 html(带有输入标签)
问题陈述:无法将文件选择文本框中的值分配给属性 FilePath。假设我单击文件上传控件的浏览按钮,然后选择一个文件,文件路径值不会更新为文件路径属性。
这是我的自定义 HTML 助手
public static MvcHtmlString FileBoxFor<TModel, TValue>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TValue>> expression)
{
return htmlHelper.FileBoxFor<TModel, TValue>(expression, (object)null);
}
/// <summary>
/// Returns a file input element by using the specified HTML helper and the name of the form field as an expression.
/// </summary>
/// <typeparam name="TModel">The type of the model.</typeparam>
/// <typeparam name="TValue">The type of the value.</typeparam>
/// <param name="htmlHelper">The HTML helper instance that this method extends.</param>
/// <param name="expression">The expression that resolves to the name of the form field and the <see cref="System.Web.Mvc.ViewDataDictionary" /> key that is used to look up the validation errors.</param>
/// <param name="htmlAttributes">An object that contains the HTML attributes for the element. The attributes are retrieved through reflection by examining the properties of the object. The object is typically created by using object initializer syntax.</param>
/// <returns>
/// An input element that has its type attribute set to "file".
/// </returns>
public static MvcHtmlString FileBoxFor<TModel, TValue>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TValue>> expression, object htmlAttributes)
{
return htmlHelper.FileBoxFor<TModel, TValue>(expression, new RouteValueDictionary(htmlAttributes));
}
/// <summary>
/// Returns a file input element by using the specified HTML helper and the name of the form field as an expression.
/// </summary>
/// <typeparam name="TModel">The type of the model.</typeparam>
/// <typeparam name="TValue">The type of the value.</typeparam>
/// <param name="htmlHelper">The HTML helper instance that this method extends.</param>
/// <param name="expression">The expression that resolves to the name of the form field and the <see cref="System.Web.Mvc.ViewDataDictionary" /> key that is used to look up the validation errors.</param>
/// <param name="htmlAttributes">An object that contains the HTML attributes for the element. The attributes are retrieved through reflection by examining the properties of the object. The object is typically created by using object initializer syntax.</param>
/// <returns>
/// An input element that has its type attribute set to "file".
/// </returns>
public static MvcHtmlString FileBoxFor<TModel, TValue>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TValue>> expression, IDictionary<String, Object> htmlAttributes)
{
var name = ExpressionHelper.GetExpressionText(expression);
return htmlHelper.FileBox(name, htmlAttributes);
}