2015-12-17 41 views
1

我想用我的自定義Textbox更改輸入值。我嘗試添加類文本 - 大寫,但默認情況下該值仍爲小寫。如何使用我的customTextbox將輸入值更改爲大寫?

我HtmlHelper類是

public static class CustomTextBoxHelper 
{ 
    public static MvcHtmlString Custom_TextBox<TModel, TValue>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TValue>> expression, object htmlAttributes) 
    { 
     var dict = new RouteValueDictionary(htmlAttributes); 
     return helper.Custom_TextBox(expression, dict); 
    } 
    public static MvcHtmlString Custom_TextBox<TModel, TValue>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TValue>> expression) 
    { 
     var htmlAttributes = new Dictionary<string, object>(); 
     return helper.Custom_TextBox(expression, htmlAttributes); 
    } 


    public static MvcHtmlString Custom_TextBox<TModel, TValue>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TValue>> expression, IDictionary<string, object> htmlAttributes) 
    { 

     ModelMetadata oModelMetadata = ModelMetadata.FromLambdaExpression(expression, helper.ViewData); 

     if (htmlAttributes == null) 
     { 
      htmlAttributes = new Dictionary<string, object>(); 
     } 

     htmlAttributes.Add("type", "text"); 
     htmlAttributes.Add("name", oModelMetadata.DisplayName); 
     htmlAttributes.Add("id", oModelMetadata.DisplayName); 


     if (htmlAttributes.ContainsKey("class")) 
     { 
      htmlAttributes["class"] += " text-uppercase"; 
     } 
     else 
     { 
      htmlAttributes.Add("class", "text-uppercase"); 
     } 
     if (htmlAttributes.ContainsKey("value")) 
     { 
      htmlAttributes["value"] = htmlAttributes["value"].ToString().ToUpper(); 
     } 
     return helper.TextBoxFor(expression, htmlAttributes); 
    } 
} 

text-uppercase並沒有解決我的疑難問題。知道這是CSS財產。 我也嘗試添加的onchange event.But我需要增加我的腳本:

<script> 
function myFunction() { 
    $("#myTextbox").val($("#myTextbox").val().toUpperCase()); 
} 
</script> 

如何我此腳本添加到我的助手類?還是其他想法?

+0

嘗試添加樣式爲htmlAttributes.Add的htmlAttributes(「transform」,「uppercase」) –

+0

@AnupamSingh謝謝你的回答,但它不起作用。當我用小寫輸入文本時,值仍然是小寫。 – tayfunozturk3561

+0

嘗試檢查相同的html生成 –

回答

0

我解決了這個問題,但可能會有更有效的解決方案。 我在Custom_Textbox函數而不是使用htmlattribute「值」,這樣的底部增加了腳本的字符串:

if (htmlAttributes.ContainsKey("onchange")) 
     { 
      htmlAttributes["onchange"] = "myFunctionThatMustNotMergeAnyOtherFunctionName()"; 
     } 
     else htmlAttributes.Add("onchange", "myFunctionThatMustNotMergeAnyOtherFunctionName()"); 
     return MvcHtmlString.Create(helper.TextBoxFor(expression, htmlAttributes).ToString() + @"<script> 
function myFunctionThatMustNotMergeAnyOtherFunctionName() { 
    $('#"+oModelMetadata.DisplayName+"').val($('#"[email protected]"').val().toUpperCase()); 
} 
</script>"); 

現在,我的價值始終是大寫。 感謝您的關注。

相關問題