私のモデルは
public class FileSelectControl
{
//property for selected file path value
public string FilePath { get; set; }
}
意見
@Html.FileBoxFor(x => x.FilePath)
FileBoxFor は、html (入力タグ付き) を返すカスタム HTML ヘルパー クラスです。
問題文 : ファイル選択テキスト ボックスの値をプロパティ 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);
}