ASP.NET MVC DropDownFor奇怪的问题 [英] Strange problem with ASP.NET MVC DropDownFor

查看:114
本文介绍了ASP.NET MVC DropDownFor奇怪的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的观点如下:

 < form id =list_admethod =getclass =adListFilteraction =<%= 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.我也检查过,在action(return View(data);)。

问题在于下拉控件中的值3选项未设置为选中状态? model.LS.L1在动作中设置为3时似乎为空?



可能是什么问题?



BestResards

编辑1:

/ p>

  public ActionResult List(AdList data)
{
AdModel adModel = new AdModel();
AccountModel accountModel = new AccountModel();
FilterModel filterModel = new FilterModel();
列表< 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;
//在传入数据上提交参数时将被设置,如果设置了这个参数,那么
//data.LS.L1将被设置为3.我已经尝试过data.LS.L1返回提交时设置为3。
return View(data);

型号:

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

public LocationSelect LS {get;组; }
}

public class LocationSelect
{
public int? L1 {get;组; }
public int? L2 {get;组; }
///< summary>
///有多种选择可能时使用
///< / summary>
public List< int> L3 {get;组; }

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

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


$ / code $ / pre
$ b $编辑2:

如果我放置

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

在这个视图中,它会渲染成这样:

 < input id =LS_L1type =hiddenvalue =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();
列表< ModelViewLocation> mvLocationList = new List< ModelViewLocation>();

列表<位置> selectedLocationList = new List< Location>(); (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(字符串locationId在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))。
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 , 名称); //,locationModel.GetLocation(locationSelect.L1.Value));

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

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



$ b

该页面第一次将else部分在SetDefaultSettingsOnLocations方法中运行。第一次if部分将被第二次运行。在这种情况下,中间部分(如果)不会被触发。我已经在调试模式下检查了这一点。

解决方案

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

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

此场景是用户输入的值再次被选中。您的情况很容易解决,只需使用:

  ModelState.Clear(); 

,然后再返回视图。


Hi,

I have the following in my view :

<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 is int? Model.LS.Location1List is SelectList (no selection set only the list)

First visit on the view will look grate, the LocationDropDown will contain correct values and the model.LS.L1 will be set to null.

Then I submit the form and in the control action I will set the model.LS.L1 to 3. I have also checked that this is true at the end of the action ("return View(data);").

The problem is that the option with value 3 is not set as selected in the dropdown control? The model.LS.L1 seems to be null even when it was set to 3 in the action?

What can be the problem?

BestRegards

Edit1:

Action :

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

Model :

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:

If I place a

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

in the view, it will be renderd like this :

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

The value should be 3 here

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

}

When loading the page the first time the else part will be runned in the SetDefaultSettingsOnLocations method. The second time the first if section will be runned. The middle part(else if) will never be triggered in this scenario. I have checked that this is true in debug mode.

解决方案

Your problem here is ModelState. You POST to your Action and return the same view. The second time the view is rendered it will look at the ModelState and use those values to fill your controls. This is used for when validation fails:

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

Is this scenario the values that the user entered are again selected. Your case is simple to fix, just use:

ModelState.Clear();

before you return the View.

这篇关于ASP.NET MVC DropDownFor奇怪的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆