2017-03-20 14 views
0

2つのプロパティを含むビューモデルがあります。MVC5でRazorを使用してカスタムコントロールをレンダリングする

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(); 
    } 

これはコードはokをコンパイルして画面上に描画しますが、コントロール内のテキストを変更するとonchangeイベントは発生しません。

私は、生成されたhmtl出力を調べて、onchangeイベントがまったくレンダリングされていないことを確認しました。

誰かが正しい方向を指すことができますか?

答えて

0

私はそれを選別したと思います。

私が作成したコードの変更は次のとおりです。 HMTLヘルパーでは、次のように追加しました。これで、onchangeイベントが正しく発生するようになりました。

関連する問題