级联下拉列表中的选定值在帖子上重置

时间:2013-04-28 18:14:23

标签: c# asp.net-mvc-4 cascadingdropdown

我有MVC4网络应用程序,带有2个级联下拉列表(父级和子级)和带有后期操作的按钮,用于根据下拉列表中的选定值过滤网格中显示的数据。层叠下拉列表我用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”,新的SelectList(ViewBag.Sections,“Id”,“Name”),“请选择父类型”,new {id =“Sections”})             
            @ Html.DropDownList(“Types”,新的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);
    }
 }

0 个答案:

没有答案