2017-05-29 86 views
-2

我有一些DropdownLists在我看來通過AJAX成功更新<select>

這裏是代碼

@Html.DropDownList("Question1", null, "Вопрос 1", htmlAttributes: new {@class = "form-control", @style = "height:40px;margin-bottom: 20px;"}) 

我有AJAX調用補充問題

這裏是代碼

<script> 
$(document).ready(function() { 
    $('#save_quest').click(function() { 
     savequestion(); 
     }); 
}); 

// Сохранение вопроса в модальном окне 
function savequestion() { 
    $.ajax({ 
     type: 'Post', 
     dataType: 'Json', 
     data: { 
      Question_new: $('#question').val(), 
      Answer: $('#answer').val(), 
      Preparing: $('#prepare').val(), 
      Retries: $('#retries').val(), 
     }, 
     url: '@Url.Action("CreateNewQuestion", "Questions")', 
     success: function (da) { 
      if (da.Result === "Success") { 
       $('#myModal').hide(); 
       emails_update(); 
       } else { 
       alert('Error' + da.Message); 
      } 
     }, 
     error: function (da) { 
      alert('Error'); 
     } 
    }); 
} 

如何在成功更新DropDownList值?

感謝對幫助

+0

Update DropDown to what? – User3250

+0

值DropdownList @ User3250 –

+0

成功後您是否獲得新的列表數據? – User3250

回答

0

我附上顯示控制器,視圖和jQuery代碼的樣本。

查看代碼

@model IC.Models.ReportViewModel 

@using (Html.BeginForm(actionName: "GetReportFilter", controllerName: "Report", method: FormMethod.Post, htmlAttributes: new { @class = "edit_form", @id = "frmStatisticsReport" })) 
{ 
    @Html.AntiForgeryToken() 
    <div class="box box-frame" style="padding-bottom: 10px;"> 
     <div class="box-inner" style="padding-left: 0px"> 
      <div class="box-caption">Search Statistical Reports</div> 
      <div style="padding: 2px;"> 
       @Html.ValidationSummary() 
      </div> 
      <ul class="piped font-weight-normal"> 
       <li> 
        <b>Company: </b> 

          @Html.DropDownListFor(m => Model.CompanyId, 
                 Model.CompanyLookupValues.Select(
                  c => new SelectListItem() { Value = c.CompanyId.ToString(), Text = c.CompanyName } 
                 ), 
                 "-- select --", 
                 new { @class = "width-4" }) 
          @Html.ValidationMessageFor(m => Model.CompanyId) 
          <text>&nbsp;&nbsp;</text> 

        <b> Report Type: </b> 
        @Html.DropDownListFor(m => Model.ReportId, 
           Model.ReportLookupValues.Select(c => new SelectListItem() { Value = c.ReportId.ToString(), Text = c.ReportName }), 
           "-- select --", 
          new { @class = "width-4" }) 
         @Html.ValidationMessageFor(m => Model.ReportId) 
         &nbsp;&nbsp; 
         <input class="button-primary" type="button" id="btnGetReportFilters" value="&nbsp;&nbsp;Get Report Filters&nbsp;&nbsp;" 
           onclick="GetFilters();" /> 
       </li> 
      </ul> 

     </div> 
    </div> 


} 

視圖模型使用

public class ReportViewModel 
    { 
     [Required(ErrorMessage = "The Company is a required field")] 
     [Display(Name = "Company")] 
     public int CompanyId { get; set; } 

     public IEnumerable<ReportCompanyViewModel> CompanyLookupValues { get; set; } 

     [Required(ErrorMessage = "The Report Type is a required field")] 
     [Display(Name = "Report Type")] 
     public int ReportId { get; set; } 

     public IEnumerable<Report> ReportLookupValues { get; set; } 


    } 

    public class Report 
    { 
     public int ReportId { get; set; } 
     public string ReportName { get; set; } 
    } 

    public class ReportCompanyViewModel 
    { 
     public int CompanyId { get; set; } 
     public string CompanyName { get; set; } 
    } 

jQuery函數用於填充所述第二下拉

function GetReportTypes() { 
    var companyId = $("#CompanyId").val(); 
    if (companyId != '') { 
     var url = '@Url.Action("GetReportTypes", "Report")'; 
     $.getJSON(url + '?companyId=' + companyId, function (data) { 
      var select = $("#ReportId"); 
      select.empty(); 
      select.append($('<option/>', { 
       value: '', 
       text: "-- select --" 
      })); 

      $.each(data, function (index, itemData) { 
       select.append($('<option/>', { 
        value: itemData.ReportId, 
        text: itemData.ReportName 
       })); 
      }); 
     }); 
    } 
} 

控制器代碼

public ActionResult Index() 
     { 
      var companies = ReferenceDataService.GetFacilitiesForReport().Select(r => new ReportCompanyViewModel { CompanyId = r.Id, CompanyName = r.Name }); 

      ReportViewModel model = new ReportViewModel 
      { 
       CompanyLookupValues = companies,     
       ReportLookupValues = new List<Report>() 
      }; 
      return View(model); 
     } 


public JsonResult GetReportTypes(int companyId) 
     { 
      List<Report> reportTypes = new List<Report>(); 
      if (companyId > 0) 
      { 

        reportTypes = ReferenceDataService.GetReportTypes().Select(r => new Report { ReportId = r.ReportId, ReportName = r.ReportName }).ToList(); 


      } 

      return Json(reportTypes, JsonRequestBehavior.AllowGet); ; 

     } 
+0

它不填充到下拉列表 我只看到 - 選擇 - 並且沒有值 –