2012-05-15 100 views
0

FINAL編輯:問題是,ViewBag.PropName == Model.PropName; FIX:ViewBag.PropName ===> ViewBag.PropName2驗證問題隱藏DropDownListFor的ASP.NET MVC3

這是我第一次發佈;我是新的,並認爲我搞砸了佈局......很抱歉! 我遇到了一些隱藏dropdownlistfor的客戶端驗證問題。 我正在使用ASP.NET MVC3,並且在此頁面上有很多隱藏ddl。 然而,驗證不適用於某些ddl。 以前有沒有人有過這個問題?

這些是2屬性; 1個工作,1失敗

//VALIDATION FAILS 
[Required] 
[Range(0, 4)] 
public int TiresBack { get; set; } 

//VALIDATION WORKS 
[Required] 
[Range(0, 4)] 
public int TechnicalState { get; set; } 

這是一塊剃刀:

<tr>//THIS DOESN'T WORK 
    <td style="width: 38%;"> 
     @txtFor("tiresBack") 
    </td> 
//This is JQuery Star-Rating. 
    <td align="center" id="starsTiresBack"> 
    </td> 
    <td>        
     @Html.DropDownListFor(model => model.TiresBack, ViewBag.TiresBack as IEnumerable<SelectListItem>, new { style = "display:none;" }) 
     <input class="blueButton" type="button" id="btnBrandRearTires" value="@txtFor("brandTiresBack")" /> 
     <span>@Html.ValidationMessageFor(model => model.TiresBack)</span> <span>@Html.ValidationMessageFor(model => model.BrandRearTires)</span> 
    </td> 
</tr> 
//THIS WORKS 
<tr> 
    <td style="width: 38%;"> 
     @txtFor("technicalState")        
    </td> 
    <td id="starsTechnicalState" align="center"> 
    </td> 
    <td> 
     @Html.DropDownListFor(model => model.TechnicalState, ViewBag.TechState as IEnumerable<SelectListItem>, new { style = "display:none;" }) 
     <input class="blueButton" type="button" id="btnTechnicalStateDescription" value="@txtFor("technicalStateDescriptionButton")" style="display:none;"/> 
     <span>@Html.ValidationMessageFor(model => model.TechnicalState)</span> <span>@Html.ValidationMessageFor(model => model.TechnicalStateDescription)</span> 
    </td> 
</tr> 

EDIT1:使用該方法在控制器初始化:

public static List<SelectListItem> CreateDefaultStateList(int? selected = -1) 
    { 
     List<SelectListItem> sl = new List<SelectListItem>(); 
     for (int i = -1; i < 5; i++) 
     { 
      SelectListItem sli = new SelectListItem(); 
      if (i == 0 || i == -1) 
       sli.Text = ""; 
      else 
       sli.Text = i.ToString(); 
      sli.Value = i.ToString(); 
      if (i == selected) 
       sli.Selected = true; 
      sl.Add(sli); 
     } 
     return sl; 
    } 

EDIT2:所述創建控制器。 defaultstatelist是從上面發佈的方法返回的內容。

List<SelectListItem> defaultStateList = CreateDefaultStateList(); 

[Authorize] 
public ActionResult Create() 
{ 
    FillViewBag(); 
    ViewBag.PropX = defaultStateList; 
    ViewBag.TiresFront = defaultStateList; 
    ViewBag.TechState = defaultStateList; 
    ViewBag.PropY= defaultStateList; 
    ViewBag.PropZ= defaultStateList; 
    ViewBag.PropA= defaultStateList; 
    ViewBag.PropB= defaultStateList; 
    ViewBag.PropC= defaultStateList; 
    ViewBag.PropD= defaultStateList; 

    return View(); 
} 

鍵入此我注意到方法中的默認值。這可能是... ==>試過了,不是。現在將代碼更新爲'-1'作爲默認值。

+0

如何在控制器中初始化這些屬性? – Andrei

+0

@Andrei來自編輯代碼的列表被放入視圖包 – sander

+0

您能否顯示控制器的所有代碼?沒有任何一種方法,bu實際分配ViewBag屬性?我懷疑這些下拉菜單具有不同的初始狀態 - 第一個下拉菜單選擇了空值,第二個下拉菜單選擇了非空值。因此他們的行爲有所不同。但要確認需要實際的控制器代碼。 – Andrei

回答

0

問題是ViewBag.PropName == Model.PropName; 修復:將ViewBag.PropName重命名爲ViewBag.PropName2之類的東西。