使用自定义html帮助器为模型属性赋值

时间:2013-07-19 13:40:56

标签: c# asp.net-mvc razor

我的模特是

public class FileSelectControl
{
    //property for selected file path value
    public string FilePath { get; set; }
}

查看

@Html.FileBoxFor(x => x.FilePath)

其中FileBoxFor是一个自定义HTML帮助程序类,它返回html(带有输入标记)

问题陈述: 无法将fileselect文本框中的值分配给属性FilePath。假设我单击文件上传控件的浏览按钮,并选择一个文件,文件路径值不会更新到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);
    }

0 个答案:

没有答案