ajax调用后保留多选列表框项

时间:2013-02-07 20:00:11

标签: jquery asp.net-mvc

我有一个视图模型,它在局部视图中呈现 N 列表框的数量。每个列表框都有一个唯一的ID,称为 PartID 。这些列表框允许用户多选列表项。

我要做的是在发出ajax请求之前,查看是否存在任何列表框并存储相应的列表框选定项目索引。如果再次呈现相同的litsbox,则保留其先前选定的项目并预先选择ajax请求的成功函数。

这些列表框是通过AJAX

获取的
$.ajax({
                    type: 'POST',
                    url: serviceListPartsUrl,
                    cache: false,
                    datatype: "html",
                    data: { ServiceEntryID: $("#ServiceEntryID").val(), Parts: partTextArea },
                    success: function (result) {
                        $("#inputParts").html(result);
                    }
                });

查看模型

using System.Collections.Generic;
using RunLog.Domain.Entities;

namespace RunLog.WebUI.Models
{
    public class ServiceEntryListPartsViewModel
    {
        public IEnumerable<ServiceEntryPartDisplay> Parts { get; set; }
    }
}

部分视图

<tr>
    <td>@string.Format("{0:MM/dd/yyyy hh:mm:ss tt}", Model.PartDescription)
        @Html.HiddenFor(model => model.PartDescription)
    </td>
    <td>
        @Html.ListBoxFor(model => model.ServiceTypes, new MultiSelectList(RunLog.Domain.Lists.GlobalList.PartsServiceTypes(), "ID", "Name"), new { style = "width: 200px; height: 80px;", id =  @Model.PartID, name = "listbox" })
    </td>
    <td>@Html.TextAreaFor(model => model.Comment, new { style = "width: 200px; height: 80px;" })
    </td>
</tr>

控制器操作

  [HttpPost]
            //[OutputCacheAttribute(NoStore=true,Duration=0,VaryByParam="*")]
            public ViewResult ListParts(string ServiceEntryID, string Parts)
            {
                int id = Convert.ToInt32(ServiceEntryID);

                ServiceEntryListPartsViewModel viewModel = new ServiceEntryListPartsViewModel();

                List<ServiceEntryPartDisplay> parts = new List<ServiceEntryPartDisplay>();

                if (Parts.Length > 0)
                {
                    var partsServiceTypeResults = from rec in db.ServiceEntryPart
                                                  join ec in db.Part on rec.PartID equals ec.ID
                                                  where (rec.ServiceEntryID.Equals(id) && ec.Active == true)
                                                  orderby rec.ServiceEntryID
                                                  select new ServiceEntryPartDisplay()
                                                  {
                                                      ServiceEntryID = rec.ServiceEntryID,
                                                      PartID = rec.PartID,
                                                      PartDescription = ec.PartDescription,
                                                      ServiceTypeIDs = rec.ServiceTypeIDs,
                                                      Comment = rec.Comment
                                                  };


                    string[] splitData = Parts.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);

                    foreach (string split in splitData)
                    {
                        ServiceEntryPartDisplay part = new ServiceEntryPartDisplay();

                        part.PartDescription = split;

                        part.PartID = Convert.ToInt32(split);
                        part.PartDescription = string.Format("{0} ~ {1}", split, (from pp in db.Part where pp.ID.Equals(part.PartID) select pp.PartDescription).FirstOrDefault());

                        var results = (from pp in partsServiceTypeResults where pp.PartID.Equals(part.PartID) select new { pp.ServiceTypeIDs, pp.Comment }).FirstOrDefault();

                        if (results != null)
                        {
                            part.Comment = results.Comment;
                            part.ServiceTypes = Domain.Lists.GlobalList.GetPartsServiceTypes(results.ServiceTypeIDs);
                        }
                        else
                        {
                            part.Comment = "";
                            part.ServiceTypes = new List<Domain.Lists.PartsServiceType>();
                        }

                        parts.Add(part);
                    }

                }

                viewModel.Parts = parts;

                return View(viewModel);
            }

1 个答案:

答案 0 :(得分:0)

我认为你可以简化你的工作方式。为什么不向您的Controller传递一个自定义ViewModel的集合,其中包含ServiceTypeID和当前选定的部分ID。您可以使用正确的项目已选择正确生成部分视图。你可以用javascript客户端来做这个诡计,但我会说利用你拥有的工具。将所有必要的数据发布回服务器,让MVC帮助您以正确的状态设置视图。