已保存的搜索加载选项1(而不是Select2下拉列表中的选项0)

时间:2019-03-25 12:35:09

标签: asp.net-mvc razor jquery-select2

我有一个控制器,它返回带有已保存搜索参数的模型,而select2应该加载到局部视图中。

当某些参数的id = 0时,这意味着select2在保存时为空,将在窗体的正确下拉列表中显示为第一个值,而不是空白(0)。

[HttpGet]
        public ActionResult GetSavedAdvancedSearchForm(int SavedSearchId, int SelectedClientId)
        {
            if (SavedSearchId != 0)
            {
                var savedSearch = _fanService.GetSavedSearchById(SavedSearchId);
                if (savedSearch != null)
                {
                    var uid = User.Identity.GetUserId();

                    var model = new FansAdvancedSearchViewModel
                    {
                        SelectedLeadLinkedInProfileId = (int)savedSearch.SelectedLeadLinkedInProfileId,
                        SelectedBatchId = (int)savedSearch.SelectedBatchId,
                        SelectedTagId = (int)savedSearch.SelectedTagId,
                        SelectedStatusId = (int)savedSearch.SelectedStatusId,
                        SelectedEmailResponseStatusId = (int)savedSearch.SelectedEmailResponseStatusId,
                        SelectedLastPitch121ActivitiyId = (int)savedSearch.SelectedLastPitch121ActivitiyId,
                        ActivityThatIsAassignedId = savedSearch.SelectedFanActivityAssignedId.Split(',').Select(Int32.Parse).ToList(),
                        ActivityThatIsNotAssignedId = savedSearch.SelectedFanActivityNotAssignedId.Split(',').Select(Int32.Parse).ToList(),
                        DeliveryActionThatIsAssignedId = savedSearch.SelectedDeliveryActionAssignedId.Split(',').Select(Int32.Parse).ToList(),
                        DeliveryActionThatIsNotAssignedId = savedSearch.SelectedDeliveryActionNotAssignedId.Split(',').Select(Int32.Parse).ToList(),
                        ActivityDateRangeStart = savedSearch.ActivityDateRangeStart,
                        ActivityDateRangeEnd = savedSearch.ActivityDateRangeEnd,
                        ScoreRangeMin = (int)savedSearch.ScoreRangeMin,
                        ScoreRangeMax = (int)savedSearch.ScoreRangeMax,
                        SelectedSavedSearchId = SavedSearchId,                       
                        TwitterHandlePresent = (bool)savedSearch.TwiterHandlePresent,
                        JobTitlekeywords = savedSearch.JobTitleKeywords,
                        ConnectedToOtherProfilesId = (int)savedSearch.ConnectedToOtherProfiles,
                        ConnectedToOtherProfiles = new SelectList(_linkedInProfileService.GetLinkedInProfiles(SelectedClientId), "Id", "Name"),
                        Batches = new SelectList(_linkedInProfileService.GetAllBatches(), "Id", "Name"),
                        LeadLinkedInProfiles = new SelectList(_linkedInProfileService.GetLinkedInProfiles(SelectedClientId), "Id", "Name"),
                        Tags = new SelectList(_linkedInProfileService.GetAllTags(), "Id", "Title"),
                        LastPitch121Activities = new SelectList(_linkedInProfileService.GetAllDeliveryActions(), "Id", "Name"),
                        FanActivities = new SelectList(_linkedInProfileService.GetAllFanActivities(), "Id", "Name"),
                        SavedSearches = new SelectList(_fanService.GetAllSavedSeardces(uid), "Id", "SavedSearchName"),
                        ActivityThatIsAssigned = new SelectList(_linkedInProfileService.GetAllFanActivities(), "Id", "Name"),
                        ActivityThatIsNotAssigned = new SelectList(_linkedInProfileService.GetAllFanActivities(), "Id", "Name"),
                        DeliveryActionThatIsAssigned = new SelectList(_linkedInProfileService.GetAllDeliveryActions(), "Id", "Name"),
                        DeliveryActionThatIsNotAssigned = new SelectList(_linkedInProfileService.GetAllDeliveryActions(), "Id", "Name"),

                    };

                    return PartialView("_AdvancedSearch", model);
                }
            }
            return null;
        }

<div class="row mg-t-20">
                        @Html.LabelFor(m => m.Batches, new { @class = "col-sm-4 form-control-label" })
                        <div class="col-sm-8 mg-t-10 mg-sm-t-0">
                            @Html.DropDownListFor(m => m.SelectedBatchId, new SelectList(Model.Batches, "Value", "Text"), new { @class = "dropdown js-selectbatch", @style = "width: 100%;" })
                            @Html.HiddenFor(m => m.SelectedBatchId)
                        </div>
                    </div>

 function ReloadAdvancedSearch(SavedSearchId) {
        if (SavedSearchId != 0) {
            ShowLoader();
            $.ajax({
                type: "GET",
                url: "@Url.Action("GetSavedAdvancedSearchForm", "Fans")?SavedSearchId=" + SavedSearchId + "&SelectedClientId="+ @Model.SelectedClientId,
                success: function (data) {
                    $('#fansTable').DataTable().clear().destroy();
                    if (data != null) {
                        $("#fan-advanced-search-form").html("");
                        $("#fan-advanced-search-form").html(data);

                        $("#fan-advanced-search-form").ready(function () {

                            $(".js-selectfandeliveryactionAssigned, .js-selectfandeliveryactionNotAsigned, .js-selectlinkedinprofile, .js-selectbatch, .js-selecttag, .js-selectfanactivity, .js-selectfandeliveryaction, .js-ActivityThatIsAssigned-dropdown,.js-ActivityThatIsNotAssigned-dropdown, .js-ConnectedToOtherProfiles").prepend('<option value="0"></option>');

                            $(" .dropdown.js-selectfandeliveryactionAssigned, .dropdown.js-selectfandeliveryactionNotAsigned, .dropdown.js-selectlinkedinprofile, .dropdown.js-selectbatch, .dropdown.js-selecttag, .dropdown.js-selectfanactivity, .dropdown.js-selectfandeliveryaction, .dropdown.js-selectstatus-dropdown, .dropdown.js-selectemailresponsstatus-dropdown, .dropdown.js-select-saved-search, .dropdown.js-ActivityThatIsAssigned-dropdown, .dropdown.js-ActivityThatIsNotAssigned-dropdown, .dropdown.js-ConnectedToOtherProfiles").select2();

                           // $(".js-selectlinkedinprofile, .js-selectbatch, .js-selecttag, .js-selectfanactivity, .js-selectfandeliveryaction, .js-ActivityThatIsNotAssigned-dropdown, .js-ConnectedToOtherProfiles").val("0").trigger("change.select2");
                        });
                        $("#fan-advanced-search-form").html(data);
                    }
                    loadFans();
                    HideLoader();
                },
                error: function(data) {
                    HideLoader();
                }
            });
        }
    }

enter image description here Actual Output 期望的输出 Desired Output

0 个答案:

没有答案