0

我有一个视图模型,它在部分视图中呈现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);
            }
4

1 回答 1

0

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

于 2013-02-07T20:09:50.600 回答