2010-06-10 97 views
33

我有一個典型的ADO.NET EF驅動窗體,允許用戶輸入日期。我在上面放了一個jQuery日期選擇器,但是當用戶選擇一個日期時,瀏覽器會在下拉列表中顯示一些其他條目。如何關閉該下拉菜單?在傳統的ASP.NET中,我會把autocomplete =「off」。不確定MVC的等價物。如何在MVC Html Helper中禁用自動完成功能

<div class="editor-field"> 
    <%= Html.TextBoxFor(model => model.date, new { @class = "aDatePicker" })%> 
    <%= Html.ValidationMessageFor(model => model.date) %> 
</div> 

回答

61

試試這個:

<%= Html.TextBoxFor(
    model => model.date, 
    new { @class = "aDatePicker", autocomplete = "off" } 
)%> 

它會生成標記接近於以下內容:

<input type="text" id="date" name="date" class="aDatePicker" autocomplete="off" /> 
0

我用Darin的上方,併成功地應用它來演示:

@model RequestAQuote.Models.RequestAQuoteModel 
    @{ 
     ViewBag.Title = "Request a Free Quote"; 
     List<string> ProjectTypes = new List<string>(); 
     ProjectTypes.Add("Electrical Wiring"); 
     ProjectTypes.Add("Install Breakers"); 
     ProjectTypes.Add("Ceiling Fan"); 
     ProjectTypes.Add("Replace Light"); 
     ProjectTypes.Add("Replace Outlet");  
    } 

    <h2>Request A Quote</h2> 
    @using (Html.BeginForm()) 
    { 
     @Html.ValidationSummary() 
     <fieldset> 
      <legend>Request a Quote Form</legend> 
      <ol> 
       <li> 
        @Html.LabelFor(m => m.ProjectType) 
        @Html.DropDownListFor(m => m.ProjectType, new MultiSelectList(ProjectTypes)) 
       </li> 
       <li> 
        @Html.LabelFor(m => m.ContactName) 
        @Html.EditorFor(m => m.ContactName, new { autocomplete = "off" }) 
       </li> 
       <li> 
        @Html.LabelFor(m => m.DaTimePhone) 
        @Html.EditorFor(m => m.DaTimePhone, new { autocomplete = "off" }) 
       </li> 
       <li> 
        @Html.LabelFor(m => m.Email) 
        @Html.EditorFor(m => m.Email, new { autocomplete = "off" }) 
       </li> 
       <li> 
        @Html.LabelFor(m => m.ProjectDescription) 
        @Html.EditorFor(m => m.ProjectDescription, new { autocomplete = "off" }) 
       </li> 
      </ol> 
      <input type="submit" value="Request Quote" /> 
     </fieldset> 
    } 

    @section scripts 
    { 
     @Scripts.Render("~/bundles/jqueryval") 
    } 

    } 
13

幾點

  1. 如果你或多或少已經寫好的網站,你不想回去修改所有CSHTML禁用自動完成(我們將不得不回去換幾百行代碼的整個網站),你可以通過JavaScript提供了現成的處理程序禁用它,就像這樣:

    //Disable autocomplete throughout the site 
    $(document).ready(function() { 
        $("input:text,form").attr("autocomplete","off"); 
    }) 
    
  2. 從我讀過你需要的形式和它在順序文本框中水平來禁用它在所有版本的Firefox和IE上工作。

+0

您的腳本很棒! – Kbdavis07 2014-05-14 03:50:40