2016-11-11 83 views
-1

我有一個基本的密碼強度指示器。如何在剃鬚刀中設置數據顯示器

<input type="password" id="myElement1" size="40" class="left" data-display="myDisplayElement1" /> 
<div class="left" id="myDisplayElement1"></div> 

我有JS Script照顧所有的邏輯。並且data-display div顯示消息密碼是弱還是強。但我想將上面的代碼轉換成Razor。下面是我到現在爲止的內容。

<div class="form-group"> 
    @Html.LabelFor(model => model.NewPassword, htmlAttributes: new { @class = "control-label col-md-2" }) 
    <div class="col-md-10"> 
     @Html.EditorFor(model => model.NewPassword, new { htmlAttributes = new { @class = "form-control", id = "myElement1" } }) 
     <div class="left" id="myDisplayElement1"></div> 
     @Html.ValidationMessageFor(model => model.NewPassword, "", new { @class = "text-danger" }) 
    </div> 
</div> 

問題:一切正常,但我無法將數據顯示屬性放入剃鬚刀中。我需要把它放在哪裏。但是由於數據顯示沒有設置,我無法以div的形式顯示用戶的密碼強度。

我希望能夠做一些類似下面

@Html.EditorFor(model => model.NewPassword, new { htmlAttributes = new { @class = "form-control", id = "myElement1" , data-display = "myDisplayElement1"} }) 

data-display給我的錯誤。

錯誤

無法解析符號data錯誤來在那裏我已經加入data-display的地方。

+2

請閱讀[問],分享你的研究成果,並在下次提到確切的錯誤。 – CodeCaster

+0

我已添加錯誤消息。我也把代碼片段。如果您滿意,請拿出反對票。謝謝! – Unbreakable

回答

1

連字在匿名對象中無效。但是,知道有時需要使用連字符添加屬性,Razor助手將自動用屬性名稱中的連字符替換下劃線。因此,您可以使用data_display而不是data-display來完成您所需的操作。

+2

這是在[重複](http://stackoverflow.com/questions/2897733/hyphenated-html-attributes-with-asp-net-mvc),處理這個問題的許多問題之一。你確定它是否需要在網站上存在很多次的答案的另一個版本? :) – CodeCaster

+0

@Chris:非常感謝你的回答。 CodeCaster:感謝您指導我如何正確提問。 – Unbreakable