2017-07-13 67 views
0

几天前我问了一个类似的问题,但没有提及我的页面上有第二个下拉列表。第一个列表使用Html.DropDownList,我的第二个列表使用select语句。未在下拉列表中设置选定的值

我可以使用我提供的解决方案将转发中的选定项目呈现到第一个下拉列表(SelectedItem)。但是,当我尝试将相同的技术应用到我的第二个下拉列表(SortSelect)时,它失败了。

我不明白为什么,因为它们都是由id标识的,而且我使用相同的ViewBag语句将我想要显示的数据发回给视图。我在做什么错我的第二个下拉列表导致选定的项目不呈现?

我很抱歉无法建立连接并解决本质上相同问题的第二次迭代。我对.Net非常陌生,仍然在学习基础知识。

我查看

// This is the first dropdown list 
@Html.DropDownList("SelectedItem", Model.ReverseMonthsLists(), 
    new { @onchange = "CallChangeFunction(this.value)" }) 

// This is the second dropdown list 
<select id="SortSelect" onchange="CallChangeFunction(value)"> 
    <option value="default">Default</option> 
    <option value="make">Make</option> 
    <option value="model">Model</option> 
    <option value="year">Year</option> 
</select> 

// This is my javascript 
@section scripts 
{ 
    <script> 
     function CallChangeFunction() { 
      var dateItem = document.getElementById("SelectedItem").value; 
      var sortItem = document.getElementById("SortSelect").value; 
      window.location.href = "/dashboard/Report_Performance?dateItem=" + dateItem + "&sortItem=" + sortItem; 
      } 
    </script> 
} 

我的控制器

public ActionResult Report_Performance(string dateItem, string sortItem) 
{ 
    DateTime newDate = DateTime.Now.Date.AddMonths(-1); 
    if (dateItem != null && dateItem != "") 
     newDate = DateTime.Parse(dateItem); 
    var aVar = Models.Reporting.ListingStatsReportingViewModel.GetStats(userCurrentService.CompanyId.Value, Models.Reporting.DateTimePeriod.Monthly, newDate); 

    if (sortItem == "make") 
     aVar.CurrentListingStats = aVar.CurrentListingStats.OrderBy(i => i.Make).ToList(); 
    else if (sortItem == "model") 
     aVar.CurrentListingStats = aVar.CurrentListingStats.OrderBy(i => i.Model).ToList(); 
    else if (sortItem == "year") 
     aVar.CurrentListingStats = aVar.CurrentListingStats.OrderBy(i => i.Year).ToList(); 

    ViewBag.SelectedItem = dateItem; //<- THIS ONE WORKS FINE 
    ViewBag.SortSelect = sortItem; //<- THIS IS NOT WORKING, IT'S IGNORED BY THE VIEW 

    return this.View(aVar); 
} 

回答

1

OK,你的第二个下拉列表是标准的HTML,所以你得写多一点的代码为“设置“选定的值。忽略第一个列表和JS方法(它们可以保持原样),你可以尝试此:

查看

@{ 
    string _sortItem = ViewBag.SortSelect == null ? "" : ViewBag.SortSelect; 
} 
... 

<select id="SortSelect" onchange="CallChangeFunction(value)"> 
    <option value="default" @(_sortItem=="default" ? Html.Raw("selected") : Html.Raw(""))>Default</option> 
    <option value="make" @(_sortItem=="make" ? Html.Raw("selected") : Html.Raw(""))>Make</option> 
    <option value="model" @(_sortItem=="model" ? Html.Raw("selected") : Html.Raw(""))>Model</option> 
    <option value="year" @(_sortItem=="year" ? Html.Raw("selected") : Html.Raw(""))>Year</option> 
</select> 

...

老实说,虽然,我会做它更具动态性(与其他列表类似)并从控制器提供列表值(通过ViewBag),然后在视图中使用@Html.DropDownList(...

+0

你的建议非常好!更重要的是,你对事物如何运作的解释更有价值。我实现了解决方案,但必须将“_sortItem”更改为“ViewBag.SortSelect”。否则,它完美地工作。谢谢。 – csharpMind

+0

@csharpMind很高兴它有帮助。我用'_sortItem'回答的上半部分实际上验证了'ViewBag.SortSelect'的有效存在。在事件为null的情况下,您可以通过使用'raw'ViewBag作为比较来在视图中获得异常。 – scgough

+0

很高兴知道。如果有的话,我将不得不审查我的方法并进行适当的调整。再次感谢。 – csharpMind

相关问题