2015-10-13 56 views
1

我想在mvc中使用級聯下拉列表。一些它如何工作,但有一些問題。實際上當第一個視圖呈現我有一個下拉帳戶類型的基礎上,這個下拉我必須填充組下拉。但是當第一個下拉菜單被渲染時,它必須選擇房地產經紀人和贊助商,默認情況下,房地產經紀人是第一個選擇的選項,然後所有與它相關的團隊被渲染。但無法在第一時間獲得價值。什麼是任何人都可以建議我的問題。級聯下拉不起作用

這裏是我的代碼

public class ViewModel 
{ 
[Required] 
     public SelectList AccountType { get; set; } 
     public string SelectedAccountType { get; set; } 
     public IEnumerable<SelectListItem> Group { get; set; } 
     public string SelectedGroup { get; set; } 
} 


[HttpGet] 
     public ActionResult Index(ViewModel viewModel) 
     { 



      List<SelectListItem> groups = sDbContext.Groups 
        .Where(o => o.GroupId > 1 && o.Deleted == false && o.AccountSubId == 1).OrderBy(uu => uu.GroupName) 
        .Select(o => new SelectListItem() 
        { 
         Value = o.GroupId.ToString(), 
         Text = o.GroupName 
        }).ToList(); 
      groups.Insert(0, new SelectListItem() { Value = "0", Text = "All" }); 
      viewModel.Group = groups; 
      viewModel.AccountType = new SelectList(sDbContext.AccountTypes.Where(o => o.AccountTypeId != 0), "AccountTypeId", "AccountName");       
      reportService.GetReportsListGrid(); 
      return View(viewModel);   
    } 

觀點:

@model App1.ViewModels.ViewModel 
@{ 
    List<SelectListItem> list = new List<SelectListItem>(); 
} 
@using (Html.BeginForm("Index", "routee", FormMethod.Post, new { @id = "appForm" })) 
{ 
Account Type 
           @Html.DropDownListFor(m => m.SelectedAccountType, (IEnumerable<SelectListItem>)Model.AccountType, new { @class = "form-control" }) 
@Html.DropDownListFor(model => model.SelectedGroup, list, new { @class = "form-control" }) 
           @Html.ValidationMessageFor(model => model.SelectedGroup) 

} 

,這裏是我的腳本..

<script type="text/javascript"> 
$('#SelectedAccountType').change(function() { 
     OnAccountTypeChange(); 
    }); 

    function OnAccountTypeChange() { 
     var id = $("#SelectedAccountType :selected").val(); 
     var groupId = $('#SelectedGroup').val(); 
     if (id != "") { 
      alert(id); 
      $.ajax({ 
       type: "GET", 
       contentType: "application/json; charset=utf-8", 
       url: '@Url.Action("GetGroupsByAccountSubTypeId", "Common", new { area = "" })', 
       data: { "AccountSubId": id }, 
       dataType: "json", 
       beforeSend: function() { 
        //alert(id); 
       }, 
       success: function (data) { 
        var items = "<option value='0' selected='selected'>-All-</option>"; 
        $.each(data, function (i, groups) { 
         items += "<option value='" + groups.Value + "'>" + groups.Text + "</option>"; 
        }); 
        $('#SelectedGroup').html(items); 
        if (groupId != undefined || groupId != null || groupId != "") 
         $('#SelectedGroup').val(groupId); 
       }, 
       error: function (result) { 
        console.log('Service call failed: ' + result.status + ' Type :' + result.statusText); 
       }, 
       complete: function() { 
        $('#SelectedGroup').prop('disabled', false); 
       } 
      }); 
     } 
     else { 
      $('#SelectedGroup').html(items); 
     } 
    } 
</script> 
+0

這可能會有幫助:http://stackoverflow.com/questions/22952196/cascading-dropdown-lists-in- ASP-淨-MVC-5/30945495#30945495 –

回答

1

只是一個簡單的錯誤。你爲什麼要使用列表在這裏

@Html.DropDownListFor(model => model.SelectedGroup, list, new { @class = "form-control" }) 

你的代碼應該是這樣的:

@Html.DropDownListFor(model => model.SelectedGroup, (IEnumerable<SelectListItem>)Model.Group, new { @class = "form-control" })