2011-08-02 98 views
0

嗨,奇怪的問題,ASP.NET MVC DropDownFor

我有以下我認爲:

<form id="list_ad" method="get" class="adListFilter" action="<%=Url.Action("List", "Ad") %>"> 
    <%: Html.DropDownListFor(model => model.LS.L1, Model.LS.Location1List, "-- Place --", new { @class = "dd1" })%> 
    ... 
</form> 

model.LS.L1爲int? Model.LS.Location1List是SelectList(沒有選擇只設置列表)

對視圖的第一次訪問看起來是格柵,LocationDropDown將包含正確的值,並且model.LS.L1將被設置爲null。

然後我提交表單,並在控制操作中將模型.LS.L1設置爲3.我還在動作結束時檢查了這一點(「返回視圖(數據);」) 。

問題是具有值3的選項未在下拉控件中設置爲選中狀態? model.LS.L1即使在動作中設置爲3時,它似乎也是空的?

可能是什麼問題?

BestRegards

EDIT1:

操作:

public ActionResult List(AdList data) 
    { 
     AdModel adModel = new AdModel(); 
     AccountModel accountModel = new AccountModel(); 
     FilterModel filterModel = new FilterModel(); 
     List<Ad> adList; 

     AdCategoryPreset adCategoryPreset = null; 

     int adCount; 


     if (data == null) 
      data = new AdList(); 

     adCategoryPreset = this.setDefaultPresets(data); 

     this.setDefaultViewData(data, adCategoryPreset); 
     this.SetDefaultSettingsOnListAdListSettings1(data.ALS); 

     data.ALC.MVA = new List<AdListItem>(); 

     FillLocationOfList(data.ALC.MVA); 
     FillCategoriesOfList(data.ALC.MVA); 

     FilterHandler.Instance.SetFilter(data.F, data.CS.LastSelectedCategory.Value, FilterType.Display, adCategoryPreset.ToFilterValues()); 

     adList = adModel.SearchAds(data, DateTime.Now.AddMonths(-int.Parse(ConfigurationManager.AppSettings["ShowAdsThatIsEqualOrLessThenMonth"])), out adCount); 

     data.ALC.MVA.AddRange(Mapper.Map<IList<Ad>, IList<AdListItem>>(adList)); 
     data.ALS.TC = adCount; 
//When submitting a parameter on the incoming data will be set and if this is set then the 
//data.LS.L1 will be set to 3. I have cheked that data.LS.L1 is set to 3 when returning a submit. 
     return View(data); 
    } 

型號:

public class AdList 
    { 
     public AdList() 
     { 
      this.LS = new LocationSelect(); 
this.P = new AdListPresets(); 
     } 

public LocationSelect LS { get; set; } 
} 

    public class LocationSelect 
    { 
     public int? L1 { get; set; } 
     public int? L2 { get; set; } 
     /// <summary> 
     /// Used when multiple choise is possible 
     /// </summary> 
     public List<int> L3 { get; set; } 

     public SelectList Location1List { get; set; } 
     public SelectList Location2List { get; set; } 

     public LocationSelect() 
     { 
      L3 = new List<int>(); 
      Location1List = new SelectList(new List<SelectListItem>(), "Value", "Text", -1); 
      Location2List = new SelectList(new List<SelectListItem>(), "Value", "Text", 0); 
     } 
    } 

EDIT2:

如果我在視圖中放置一個

<%: Html.HiddenFor(c => c.LS.L1) %> 

,將renderd這樣的:

<input id="LS_L1" type="hidden" value="" name="LS.L1"> 

值應該是3這裏

EDIT3:

private void setDefaultViewData(AdList adList, AdCategoryPreset adCategoryPreset) 
{ 
    this.SetDefaultSettingsOnCategories(adList.CS, adCategoryPreset); 
    SetDefaultSettingsOnLocations(adList.LS, adCategoryPreset); 
} 

public static void SetDefaultSettingsOnLocations(LocationSelect locationSelect, AdCategoryPreset adCategoryPreset) 
{ 
    LocationModel locationModel = new LocationModel(); 
    List<ModelViewLocation> mvLocationList = new List<ModelViewLocation>(); 

    List<Location> selectedLocationList = new List<Location>(); 

    if (locationSelect != null) 
    { 
     mvLocationList = Mapper.Map<IList<Location>, IList<ModelViewLocation>>(locationModel.GetLocationsByParentId(null)).ToList(); 

     if (adCategoryPreset != null && adCategoryPreset.LocationIdList.Length > 0) 
     { 
      selectedLocationList = new List<Location>(); 
      foreach (string locationId in adCategoryPreset.LocationIdList.Split(',')) 
       selectedLocationList.Add(locationModel.GetLocation(int.Parse(locationId))); 

      locationSelect.Location1List = new SelectList(mvLocationList, "Id", "Name", selectedLocationList[0].ParentId); 
      locationSelect.L1 = selectedLocationList[0].ParentId; 

      mvLocationList = Mapper.Map<IList<Location>, IList<ModelViewLocation>>(locationModel.GetLocationsByParentId(selectedLocationList[0].ParentId)).ToList(); 
      locationSelect.Location2List = new SelectList(mvLocationList, "Id", "Name"); 

      locationSelect.L3 = selectedLocationList.Select(c => c.Id).ToList(); 
     } 
     else if (locationSelect.L1.HasValue && locationSelect.L1.Value > 0) 
     { 
      locationSelect.Location1List = new SelectList(mvLocationList, "Id", "Name"); //, locationModel.GetLocation(locationSelect.L1.Value)); 

      mvLocationList = Mapper.Map<IList<Location>, IList<ModelViewLocation>>(locationModel.GetLocationsByParentId(locationSelect.L1)).ToList(); 
      locationSelect.Location2List = new SelectList(mvLocationList, "Id", "Name"); 
     } 
     else 
     { 
      mvLocationList = Mapper.Map<IList<Location>, IList<ModelViewLocation>>(locationModel.GetLocationsByParentId(null)).ToList(); 

      locationSelect.Location1List = new SelectList(mvLocationList, "Id", "Name", 0); 
      locationSelect.Location2List = new SelectList(new List<object>(), null); 
     } 
    } 

} 

當在第一次在SetDefaultSettingsOnLocations方法中運行else部分時加載頁面。第一次if部分將被第二次運行。在這種情況下,中間部分(如果)不會被觸發。我已經在調試模式下檢查過這種情況。

+0

您可以添加您的控制器操作代碼嗎? –

+0

你可以發佈你的模型和控制器代碼嗎? –

+0

我已添加控制器操作代碼和模型的一部分。代碼很複雜,所以實際上看不出太多。重要的部分是L1設置正確,但DropDown助手不設置校正選擇。 – Banshee

回答

1

您的問題在於ModelState。您發佈到您的操作並返回相同的視圖。第二次渲染視圖時,它將查看ModelState並使用這些值來填充控件。這用於驗證失敗時:

if(!ModelState.IsValid) 
    return View(model); 

此方案是用戶輸入的值再次被選中。您的情況很容易修復,只需使用:

ModelState.Clear(); 

在返回視圖之前。

+0

你是對的,這對我來說是新東西。非常感謝!! – Banshee