2013-11-15 25 views
0

我有我的asp.net mvc的視圖中下面的代碼: -如何創建一個空的Html.DropDownlistFor及如何禁用Html.TextBox

@Html.DropDownListFor(model => model.FirewallCustomer.CustomerVLANSID,null, null, null) 
@Html.ValidationMessageFor(model => model.FirewallCustomer.CustomerName) 
<div><span class="f">VLAN IP</span> @Html.TextBox("VLANIP", new { disabled = "disabled"})</div> 

,但上面的代碼將提高對DropDownListFor錯誤: -

沒有類型爲「IEnumerable」的ViewData項目 具有密鑰「FirewallCustomer.CustomerVLANSID」。

對於TexBox,它將在文本框體內顯示disabled = "disabled",而不是禁用它。任何人都可以adivce如何解決我的下拉列表&我的文本框問題? Thanks`

+1

在DropDownListFor試圖傳遞一個空值列表,但文本框不爲空 – LINQ2Vodka

+1

:讀其conctructor簽名,你把HTML attribtes錯了地方 – LINQ2Vodka

回答

3
  • 對於文本框

@Html.TextBox("VLANIP", new { disabled = "disabled"})變化

@Html.TextBox("VLANIP","Your Value", new { disabled = "disabled"})。我想建議你改變disabled =「disabled」爲@readonly =「readonly」。

  • 下拉列表中,如果你想創建一個空的下拉列表,你必須轉換你FirewallCustomer.CustomerVLANSID到SelectListItem併爲它添加空值。請張貼您對此觀點的動作,我可以幫助您。應儘量

    Html.DropDownListFor(model => model.FirewallCustomer.CustomerVLANSID,Enumerable.Empty<SelectListItem>(),null)

相關問題