Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# MVC下拉筛选器不会回发选定的值_C#_Asp.net Mvc - Fatal编程技术网

C# MVC下拉筛选器不会回发选定的值

C# MVC下拉筛选器不会回发选定的值,c#,asp.net-mvc,C#,Asp.net Mvc,我正在创建我的第一个基于MVC的示例应用程序 我有一个模型:- public class SampleClass { public int ID { get; set; } [Required] public string Description { get; set; } [Range(1,100)] public int Age { get; set; } [Display(Nam

我正在创建我的第一个基于MVC的示例应用程序

我有一个模型:-

 public class SampleClass
    {
        public int ID { get; set; }

        [Required]
        public string Description { get; set; }

        [Range(1,100)]
        public int Age { get; set; }

        [Display(Name="Created On")]
        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
        public DateTime CreatedDate { get; set; }

        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
        public DateTime? LastUpdated { get; set; }

        public string itemGenre { get; set; }

    }
显示以下内容的Index.cshtml页面:-

<p>
    @Html.ActionLink("Create New", "Create")
    @using (Html.BeginForm("Index", "Sample", FormMethod.Get))
    {
        <p style="margin-bottom:30px;">

            <div style="display:inline-block; float:left; border:1px dotted black; padding:20px;">
                Created Date: @Html.TextBox("fromDate")
                <input type="submit" value="Filter" />
            </div>

            <div style="display: inline-block; border: 1px dotted black; padding: 20px; margin-left:170px;">
                Genre: @Html.DropDownList("itemGenre", "All")
            </div>

            <div style="display: inline-block; float: right; border: 1px dotted black; padding: 20px;">
                Search: @Html.TextBox("searchString")
                <input type="submit" value="Filter" />
            </div>
        </p>
    }

</p>
我已使CreatedDate和SearchString的筛选方法正常工作。但我似乎无法让我的下拉菜单过滤器正常工作。虽然我的下拉菜单中填充了存储在数据库中的正确值/字符串,但每次我回发时,ItemGene都会在我的控制器类中作为空值检索:-

        public ActionResult Index(DateTime? fromDate, string itemGenere, string searchString)
        {

            var GenreList = new List<string>();
            var GenreQuery = from d in db.Sample
                             orderby d.itemGenre
                             select d.itemGenre;
            GenreList.AddRange(GenreQuery.Distinct());
            ViewBag.itemGenre = new SelectList(GenreList);

            var items = from i in db.Sample
                        select i;

            if (!String.IsNullOrEmpty(searchString))
            {
                items = items.Where(o => o.Description.Contains(searchString));
            }

            if (!String.IsNullOrEmpty(Convert.ToString(fromDate)))
            {
                items = items.Where(p => p.CreatedDate >= fromDate);
            }

            if(!String.IsNullOrEmpty(itemGenere))
            {
                items = items.Where(q => q.itemGenre == itemGenere);
            }

            return View(items);

        }

我想知道我哪里出了问题

因为itemGenre dropdownlist名称不是itemGenere方法参数。使用视图模型和html helperswow强类型。拼写错误…..谢谢斯蒂芬。@StephenMuecke:有没有办法使下拉菜单过滤器动态化,一旦有选择,控制器中的搜索就会启动,您需要javascript/jquery来处理dropdownlist的.change事件并提交表单,而不是执行手动筛选回发操作,但这不是正常的预期行为,因此我不推荐这样做。如果使用ajax发布值并更新DOM而不刷新整个页面,那么您将获得更好的性能