Javascript 在MVC5中使用Razor呈现自定义控件

Javascript 在MVC5中使用Razor呈现自定义控件,javascript,c#,jquery,asp.net-mvc,html-helper,Javascript,C#,Jquery,Asp.net Mvc,Html Helper,我有一个包含两个属性的视图模型 public class ControlToRender { public int IsText { get; set; } public string LabelString { get; set; } public string TextValue { get; set; } } 这将填充到控制器中,以便: IsText = True; LabelString = "please enter your name"; TextV

我有一个包含两个属性的视图模型

public class ControlToRender
{
     public int IsText { get; set; }
     public string LabelString { get; set; } 
     public string TextValue { get; set; }
}
这将填充到控制器中,以便:

IsText = True;
LabelString = "please enter your name";
TextValue = "";
在我看来,我正在测试上述内容,如下所示:

if (Model.IsText)
{
    @Html.CtrlHelper().ClearableTextBoxFor(model => model.TextValue, 
          new
          {
                placeholder = Html.Encode(placeHolder),
                @class = "js-text-option-text",
                @onchange = "javascript:alert('excellent!!!')"})
}
最后,我的HTML助手代码:

static void CreateClearableTextBoxFor(object htmlAttributes, 
            decimal? additionalCost, out IDictionary<string, object> attrs,  
            out string anchorHtml)
    {
        attrs = new RouteValueDictionary(htmlAttributes);
        if (attrs.ContainsKey("class"))
            attrs["class"] = String.Concat("js-text-option-clear ", attrs["class"]);
        else
            attrs.Add("class", "js-text-option-clear");

        var anchor = new TagBuilder("a");
        anchor.AddCssClass("js-text-option-clear-button text-option-clear js-toolTip tool-tip");
        anchor.Attributes.Add("title", "Clear");
        if (additionalCost != null && additionalCost > 0)
        {
            anchor.Attributes.Add("data-additionalcost", additionalCost.ToString());
        }
        anchorHtml = anchor.ToString();
    }
static void createClearTableTextBoxfor(对象htmlAttributes,
十进制?额外成本,不含字典属性,
输出字符串(HTML)
{
attrs=新的RouteValueDictionary(htmlAttributes);
if(属性容器(“类”))
attrs[“class”]=String.Concat(“js文本选项清除”,attrs[“class”]);
其他的
属性添加(“类”,“js文本选项清除”);
var anchor=新标记生成器(“a”);
AddCssClass(“js文本选项清除按钮文本选项清除js工具提示工具提示”);
添加(“标题”、“清晰”);
如果(additionalCost!=null&&additionalCost>0)
{
Add(“data additionalcost”,additionalcost.ToString());
}
anchorHtml=anchor.ToString();
}
此代码编译为ok,并在屏幕上呈现,但当我更改控件中的文本时,不会触发onchange事件

我已经查看了生成的hmtl输出,以确定onchange事件根本没有呈现


有人能指出正确的方向吗。

我想我已经解决了

我所做的代码更改如下。在HMTL帮助程序中,我添加了:

attrs = new RouteValueDictionary(htmlAttributes);
if (attrs.ContainsKey("class"))
    attrs["class"] = String.Concat("js-text-option-clear ", attrs["class"]);
else
    attrs.Add("class", "js-text-option-clear"); 

 /* NEW LINE BELOW */
 attrs.Add("onchange", "javascript:alert('Booooooom');");
这将导致onchange事件正确触发