asp.net mvc基于下拉列表选择的动态搜索条件

时间:2014-09-26 04:10:06

标签: asp.net asp.net-mvc

早上好,我是asp.net mvc的新手。我想开发一个小应用程序,用户可以根据下拉选择使用标准进行搜索。这意味着当他们选择一个选项时,将创建并初始化动态表单元素。

这是应该是主屏幕

enter image description here

例如,当用户选择公司然后页面显示如下

enter image description here

这是我的剧本:

我的控制器

 public class RequesterController : Controller
    {
        private mEntities db = new mEntities();
        public List<SelectListItem>  LoadSelectionList(){
            List<SelectListItem> li = new List<SelectListItem>();
            li.Add(new SelectListItem { Text = "--- Select ---", Value = "0" });
            li.Add(new SelectListItem { Text = "Available Sticker", Value = "1" });
            li.Add(new SelectListItem { Text = "Company", Value = "2" });
            li.Add(new SelectListItem { Text = "Interval Date", Value = "3" });
            li.Add(new SelectListItem { Text = "Name", Value = "4" });
            li.Add(new SelectListItem { Text = "Reference Number", Value = "5" });
            li.Add(new SelectListItem { Text = "Interval Request Date", Value = "6" });
            li.Add(new SelectListItem { Text = "Valide Start Date", Value = "7" });

            return li;           
        }

        public ActionResult Index(string dllSearchCategory, string ddlCategory, string ddlCompany, string searchString){
            ViewData["selectionlist"] = LoadSelectionList();
             if (!string.IsNullOrEmpty(searchString)){
             users = users.Where(s => s.Name.Contains(searchString));
            }            
             return View(users);
        }

        public PartialViewResult LoadPartialGui(int id){
            IEnumerable<SelectListItem> iCompanies = null;

            switch (id) {
                case 1: { //Available Sticker

                    break;
                }
                case 2:
                    { //Company
                         iCompanies = db.Companies
                                        .ToList()
                                        .OrderBy(c => c.CompanyName)
                                        .Select(c => new SelectListItem
                                        {
                                            Value = c.CompanyID.ToString(),
                                            Text = c.CompanyName
                                        });

                        ViewBag.Companies = iCompanies;

                        break;
                    }
                case 3:
                    { //Interval Date

                        break;
                    }
                case 4:
                    { //Name

                        break;
                    }
                case 5:
                    { //Reference Number

                        break;
                    }
                case 6:
                    { //Interval Request Date

                        break;
                    }
                case 7:
                    { //Valide Start Date

                        break;
                    }
            }
            return PartialView("LoadPartialGuiView", iCompanies);
        }     
    }

**我的部分视图 - LoadPartialGuiView **

@if (ViewBag.Companies != null) { 
    <fieldset>
        @Html.Label("Value: ")
        @Html.DropDownList("ddlCompany", (IEnumerable<SelectListItem>)ViewBag.Companies, "--- Select ---")<br />
    </fieldset>
}

和我的Index.cshtml

   @using (Html.BeginForm("Index", "Requester", FormMethod.Get))
    {
    <p>
        Search By
        @Html.DropDownList("dllSearchCategory", ViewData["selectionlist"] as List<SelectListItem>)
        <div id="partialPlaceHolder"> </div>
        <input type="submit" value="Search" />
    </p>
    }

</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.IDCode)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.FamilyName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Gender)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Company.CompanyName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.DocumentPath)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.PhotoPath)
        </th>
        <th>
            &nbsp;
        </th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.IDCode)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.FamilyName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Gender)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Company.CompanyName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.DocumentPath)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.PhotoPath)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.UserID }) |
            @Html.ActionLink("Details", "Details", new { id=item.UserID }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.UserID })
        </td>
    </tr>
}

</table>
<script type="text/javascript">    
    $('#dllSearchCategory').change(function () {       
        var selectedID = $(this).val();       
        $.get('/Requester/LoadPartialGui/' + selectedID, function (data) {       
            $('#partialPlaceHolder').html(data);            
            $('#partialPlaceHolder').fadeIn('fast');
        });       
    });    
</script>

当我点击“搜索”按钮时,动态块创建的下拉列表选择基础也会重置。我不知道如何让事情发挥作用。

非常感谢您的帮助,

此致

Veasna

0 个答案:

没有答案