【问题标题】:How can i add and submit the "Any" value in my drop down list which is used for searching objects in my asp.net MVC如何在我的下拉列表中添加和提交“任何”值,该下拉列表用于在我的 asp.net MVC 中搜索对象
【发布时间】:2012-03-12 01:30:49
【问题描述】:

我有以下搜索表单,其中包含:文本框和两个下拉列表:-

@using (Ajax.BeginForm("Search", "Question", 
    new AjaxOptions
{
    HttpMethod = "GET",
    InsertionMode = InsertionMode.Replace,
    UpdateTargetId = "re",
    LoadingElementId = "progress"
}))
{   <table >
 <tr>
        <th>
         Description:-
        </th>
        <th>
          <input type="text" name="q" data-autocomplete-source="@Url.Action("QuickSearch", "Question")" />
        </th>
 </tr>
       <tr>
        <th>
         Difficulty Level:-
        </th>
        <th>
        @Html.DropDownList("q2")        
        </th>
 </tr>
  <tr>

        <th>
         Create By:-
        </th>
        <th>
         @Html.DropDownList("q3")
                 </th>
 </tr>
  </table>


     <input type="submit" value="Search Questions" />
}

所以我如何在我的两个下拉列表中定义“任何”,以便我可以在我的搜索操作方法中编写如下内容:-

    public IQueryable<Question> searchquestions3(string q, int? q2 , string)
            {

   return from u in entities1.Questions
   where (u.Description.Contains(q) && (u.DifficultyID == q2 || q2 == "Any" )  && ( u.CreatedBy == q3 || q3 == "Any"))                        
select u;}

【问题讨论】:

    标签: linq asp.net-mvc-3 search


    【解决方案1】:

    您应该将您的下拉菜单作为 DropDownListFor 并填充并绑定到视图模型中。

    然后你可以做类似的事情

    @Html.DropDownListFor(m => m.DifficultyID, Model.Difficulties, "Any")
    

    【讨论】:

    • 感谢您的回复,但为什么我应该使用 @Html.DropDownListFor ,我可以在我的原始代码中做同样的事情,例如 @Html.DropDownList("q2","Any") 。 ..
    【解决方案2】:

    我用了一些东西

    var list = elearningrepository.FindAllLevels().ToList() 
    list.Insert(0, new TypeOfYourObject() {ValueProperty = "Any"});
    

    【讨论】:

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