【问题标题】:Strange problem with ASP.NET MVC DropDownForASP.NET MVC DropDownFor 的奇怪问题
【发布时间】:2011-10-18 13:33:36
【问题描述】:

嗨,

我的观点如下:

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

然后我提交表单并在控制操作中将 model.LS.L1 设置为 3。我还在操作结束时检查了这是否为真(“return View(data);”)。

问题是在下拉控件中没有将值为 3 的选项设置为选中?即使在动作中设置为 3,model.LS.L1 似乎也为空?

可能是什么问题?

致以最诚挚的问候

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

}

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

【问题讨论】:

  • 您能添加您的控制器操作代码吗?
  • 你能发布你的模型和控制器代码吗?
  • 我已经添加了控制器动作代码和模型的一部分。代码很复杂,所以真的没什么可看的。重要的部分是 L1 设置正确,但 DropDown 助手没有设置更正选择。
  • Location1List 和 Location2List 的选择项填写了什么?在 LocationSelect 构造函数中,选择列表是空的(您传递的是一个空列表)。您确定您在列表中获得了值为 3 的条目吗?
  • 那么,在这一行:locationSelect.L1 = selectedLocationList[0].ParentId;,ParentId 是 3,是吗?

标签: c# html asp.net-mvc html-helper


【解决方案1】:

您的问题是 ModelState。您发布到您的操作并返回相同的视图。第二次呈现视图时,它将查看 ModelState 并使用这些值来填充您的控件。这用于验证失败时:

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

在这种情况下,用户输入的值是否再次被选中。您的情况很容易解决,只需使用:

ModelState.Clear();

在您返回视图之前。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多