繁体   English   中英

MVC 3 Razor DropdownListFor-选择值后如何“过滤”视图

[英]MVC 3 Razor DropdownListFor - How to “filter” the view after value is selected

所以我建议在视图中创建一个DropDownListFor元素,这意味着创建一个名为

模型
class StudentModelView

我有两个属性。

StudentID{get;set;}
IEnumerable<SelectListItem> SelectPeopleList { get;set;}

我得到的列表用此代码很好地填充了

视图

    @Html.DropDownListFor(x => x.StudentID, Model.SelectPeopleList,"--Select Students--", new Dictionary<string,object>{ {"class","dropdowns"},{"id","selectPeopleDDL"}})  

//NEED A BUTTON TO SUBMIT 

控制器

 public ActionResult Index()
        {


            return View(new StudentModelView());
        }  
  1. 我需要什么样的按钮才能张贴
  2. 发布后,如何更新视图以显示新的过滤列表? 我是否缺少POST控制器或只是ajax? 最好的方法是什么?

您可以使用jquery来完成所有操作,但这是几个指针:

您应该将过滤器包装为以下形式:

@using (Html.BeginForm("Home", "Index", FormMethod.Get))
{
  @Html.DropDownListFor(x => x.StudentID, Model.SelectPeopleList,"--Select Students--", new Dictionary<string,object>{ {"class","dropdowns"},{"id","selectPeopleDDL"}})
  <input type="submit" value="Go" />
}

提交按钮不是必须的,只要下拉列表的值发生变化,您都可以使用jquery进行提交

在您的控制器中:

public ActionMethod Index(int? studentID)
{
  var model = new StudentModelView 
  {
    StudentId = studentID, 
    SelectPeopleList=GetListFiltered(studentId)  
  }

  return(model);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM