0

我有带有 2 个级联下拉列表(父级和子级)的 MVC4 Web 应用程序和带有发布操作的按钮,用于根据下拉列表中的选定值过滤网格中显示的数据。我使用 Microsoft AJAX(Ajax.BeginForm 助手)实现的级联下拉列表,几乎如此处所述:http ://weblogs.asp.net/raduenuca/archive/2011/03/20/asp-net-mvc-cascading-dropdown-lists -tutorial-part-3-cascading-using-microsoft-ajax-ajax-beginform-helper.aspx。顺便说一句,下拉列表位于部分视图中。问题是当我单击按钮时,执行回发并将级联下拉列表中的选定值重置为原始值,即“请选择值”。有谁知道如何解决这个问题?

提前感谢所有给出答案的人!

这是我的带有级联下拉列表的部分视图的代码:

<script type="text/javascript">
$(function () {
    $('#Sections').change(function () {
        var sectionId = $("#Sections :selected").val();
        if (sectionId != "") {
            $.ajax({
                type: "GET",
                contentType: "application/json; charset=utf-8",
                url: '@Url.Action("GetDocumentTypesList", "DocumentTypes")',
                data: { "id": sectionId },
                dataType: "json",
                success: function (data) {
                    var items = "";
                    $.each(data, function (i, documentType) {
                        items += "<option value='" + documentType.Value + "'>" + documentType.Text + "</option>";
                    });
                    $('#Types').html(items);
                },
                error: function (result) {
                    alert('Service call failed: ' + result.status + ' Type :' + result.statusText);
                }
            });
        }
        else {
            var items = '<option value="">Select</option>';
            $('#Types').html(items);
        }
    });
});    

@Html.DropDownList("Sections", new SelectList(ViewBag.Sections, "Id", "Name"), "请选择父类型", new { id = "Sections" })
@Html.DropDownList("Types", new SelectList(ViewBag.Types, "Id", "Name"), "请选择子类型", new { id = "Types" })

这是部分视图的控制器代码:

public class DocumentTypesController : Controller
{
    static List<DocumentType> documentTypes = DocumentsDAL.GetDocumentTypes(true, true, false);

    // GET: /DocumentTypes/
    public ActionResult Index()
    {
        var root = documentTypes.Where(d => d.ParentId == null).ToList();

        ViewBag.Sections = root;
        ViewBag.Types = new List<DocumentType> { new DocumentType { Id = -1, Name = "Select" } };

        return PartialView("~/Views/Shared/_DocumentTypes.cshtml", root);
    }

    // Get values for parent dropdownlist.
    [AcceptVerbs(HttpVerbs.Get)]
    public JsonResult GetDocumentTypesList(string id)
    {
        var items = GetDocumentTypes(Convert.ToInt32(id)).Select(a => new SelectListItem
        {
            Text = a.Name,
            Value = a.Id.ToString(CultureInfo.InvariantCulture)
        });

        return Json(items, JsonRequestBehavior.AllowGet);
    }

    // Get values for child dropdownlist.
    [AcceptVerbs(HttpVerbs.Get)]
    public JsonResult GetDocumentTypeData(string sectionId, string typeId)
    {
        var documentTypeData = GetDocumentTypes(Convert.ToInt32(sectionId))
            .First(d => d.Id == Convert.ToInt32(typeId));

        return Json(documentTypeData, JsonRequestBehavior.AllowGet);
    }

    private static IEnumerable<DocumentType> GetDocumentTypes(int id)
    {
        return documentTypes.First(d => d.Id == id).DocumentTypes;
    }
}

这是一个基本视图,其中使用了部分视图:

@using (Ajax.BeginForm("Index", null, new AjaxOptions
{
    InsertionMode = InsertionMode.Replace,
    UpdateTargetId = "documentsGrid"
}, new { @class = "form-inline" }))
{
    <div>
        @{ Html.RenderAction("Index", "DocumentTypes", new { area = "" }); }
    </div>
    <p class="form-inline">
        @Html.LabelForModel(@Resources.LabelPeriodFrom)
        @Html.Raw("&nbsp;")
        @Html.TextBox("periodFrom", "", new { @class = "input-small" })
        @Html.Raw("&nbsp;")
        @Html.LabelForModel(@Resources.LabelPeriodTo)
        @Html.Raw("&nbsp;")
        @Html.TextBox("periodTo", "", new { @class = "input-small" })
        @Html.Raw("&nbsp;")
        <input type="submit" class="btn" value="Filter" />
    </p>
}

以及带有后操作索引的基本视图控制器,当用户按下按钮时触发:

public class IssuerDocumentsController : Controller
{
    static readonly IEnumerable<IssuerDocument> Documents = DocumentsDAL.GetDocuments(1, 1).AsEnumerable();

    [HttpPost]
    public ActionResult Index(FormCollection collection)
    {
        var documents = Documents.AsEnumerable();

        // Check if document section filter is applied.
        if (!string.IsNullOrEmpty(collection.Get("Sections")))
        {
            var documentSectionId = Convert.ToInt32(collection.Get("Sections"));
            documents = documents.Where(d => d.SectionId == documentSectionId);
        }

        // Check if document type filter is applied.
        if (!string.IsNullOrEmpty(collection.Get("Types")))
        {
            var documentTypeId = Convert.ToInt32(collection.Get("Types"));
            documents = documents.Where(d => d.TypeId == documentTypeId);
        }

        return View(documents);
    }
 }
4

0 回答 0