ASP的奇怪问题.NET MVC DropDownFor

本文关键字:NET MVC DropDownFor 问题 ASP | 更新日期: 2023-09-27 18:04:12

嗨,

我的观点如下:

<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>

模型。LS.L1是int?模型LS.Location1List是SelectList(没有选择,只有列表(

视图上的第一次访问将看起来很粗糙,LocationDropDown将包含正确的值和模型。LS.L1将被设置为null。

然后我提交表格,在控制操作中我将设置模型。LS.L1至3。我还在操作结束时检查了这一点("return View(data(;"(。

问题是值为3的选项没有在下拉控件中设置为选中?模型。即使在操作中将LS.L1设置为3,它似乎也是null?

问题出在哪里?

BestRegards

第1版:

行动:

    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);
        }
    }

第2版:

如果我放置

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

在视图中,它将像这样呈现:

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

此处的值应为3

第3版:

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);
        }
    }
}

第一次加载页面时,其他部分将在SetDefaultSettingsLocation方法中运行。第二次将运行第一个if部分。在这种情况下,中间部分(else-if(永远不会被触发。我已经检查了在调试模式下这是真的。

ASP的奇怪问题.NET MVC DropDownFor

这里的问题是ModelState。您POST到您的Action并返回相同的视图。第二次渲染视图时,它将查看ModelState并使用这些值填充控件。这用于验证失败时:

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

在这种情况下,是否会再次选择用户输入的值。您的案例很容易修复,只需使用:

ModelState.Clear();

然后返回视图。