我为 HTML 自定义扩展添加了一个类:
using System;
using System.Linq.Expressions;
using System.Text;
using System.Web.Mvc;
using System.Web.Mvc.Html;
namespace App.MvcHtmlHelpers
{
public static class HtmlHelperExtensions
{
public static MvcHtmlString ComboBox(HtmlHelper html, string name, SelectList items, string selectedValue)
{
var sb = new StringBuilder();
sb.Append(html.DropDownList(name + "_hidden", items, new { @style = "width: 200px;", @onchange = "$('input#" + name + "').val($(this).val());" }));
sb.Append(html.TextBox(name, selectedValue, new { @style = "margin-left: -199px; width: 179px; height: 1.2em; border: 0;" }));
return MvcHtmlString.Create(sb.ToString());
}
public static MvcHtmlString ComboBoxFor<TModel, TProperty>(HtmlHelper<TModel> html, Expression<Func<TModel, TProperty>> expression, SelectList items)
{
var me = (MemberExpression)expression.Body;
var name = me.Member.Name;
var sb = new StringBuilder();
sb.Append(html.DropDownList(name + "_hidden", items, new { @style = "width: 200px;", @onchange = "$('input#" + name + "').val($(this).val());" }));
sb.Append(html.TextBoxFor(expression, new { @style = "margin-left: -199px; width: 179px; height: 1.2em; border: 0;" }));
return MvcHtmlString.Create(sb.ToString());
}
我还在我的网站网络配置中注册了它:
<namespaces>
<add namespace="System.Web.Helpers" />
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<add namespace="System.Web.WebPages" />
<add namespace="App.MvcHtmlHelpers"/>
</namespaces>
在我看来,我导入了命名空间:
<%@ Import Namespace="RSPWebApp.MvcHtmlHelpers" %>
但是当我在视图中调用它时,它无法识别自定义扩展。有人可以告诉我我可能错过了什么来帮助我吗?提前非常感谢!<%:Html.ComboBoxFor(a => a.Street2, streetAddressListItems) %>