2013-04-25 36 views
5

Sau đây là xem:Làm thế nào để có được lựa chọn thả xuống giá trị từ Controller MVC

<div class="editor-label"> 
     Select Currency : 
     </div> 
     <div class="editor-field"> 
      @Html.DropDownList("CurrencyId", new SelectList(ViewBag.CurrencyId, "Value", "Text")) 
</div><div class="editor-label"> 
     Select GameType : 
     </div> 
     <div class="editor-field"> 
      @Html.DropDownList("GameTypeId", new SelectList(ViewBag.GameTypeId, "Value", "Text"), new { style = "width:100px" }) 
      @Html.ValidationMessageFor(model => model.GameTypeId) 
     </div> 

     <div class="editor-label"> 
      Select Category : 
     </div> 
     <div class="editor-field"> 
      @Html.DropDownList("CategoryByGameType", Enumerable.Empty<SelectListItem>(), "Select Category") 
      @Html.ValidationMessageFor(model => model.CategoryId) 
     </div> 

Sau đây là Bộ điều khiển: -

public ActionResult Create() 
     { 
      List<Currency> objCurrency = new List<Currency>(); 
      objCurrency = db.Currencies.ToList(); 
      List<SelectListItem> listItems = new List<SelectListItem>(); 
      listItems.Add(new SelectListItem() 
      { 
       Value = "0", 
       Text = "Select Currency" 
      }); 
      foreach (Currency item_Currency in objCurrency) 
      { 
       listItems.Add(new SelectListItem() 
       { 
        Value = item_Currency.CurrencyId.ToString(), 
        Text = item_Currency.CurrencyName 
       }); 
      } 
      ViewBag.CurrencyId = new SelectList(listItems, "Value", "Text"); 

      List<GameType> objgametype = objGameByGameType.GetDistinctGameTypeID(); 
      List<SelectListItem> listItems_1 = new List<SelectListItem>(); 
      listItems_1.Add(new SelectListItem() 
      { 
       Value = "0", 
       Text = "Select Game Type" 
      }); 
      foreach (GameType item_GameType in objgametype) 
      { 
       listItems_1.Add(new SelectListItem() 
       { 
        Value = item_GameType.GameTypeId.ToString(), 
        Text = item_GameType.GameTypeName 
       }); 
      } 
      ViewBag.GameTypeId = new SelectList(listItems_1, "Value", "Text"); 


      return View(); 
     } 

Sau đây là Jquery của tôi cho tôi đang sử dụng Casceding Dropdown

$(function() { 
      $("#GameTypeId").change(function() { 
       var theatres = ""; 
       var gametype = ""; 
       var mytestvar = ""; 
       var gametypeid = $(this).val(); 

      mytestvar += "<option value= -1 >Select Category</option>"; 
      $.getJSON("@Url.Action("GetCategoryByGameType", "GameCombination")?gametypeid=" + gametypeid, function (data) { 
       $.each(data, function (index, gametype) { 
        // alert("<option value='" + gametype.Value + "'>" + gametype.Text + "</option>"); 
        mytestvar += "<option value='" + gametype.Value + "'>" + gametype.Text + "</option>"; 
       }); 
       //alert(mytestvar); 
       $("#CategoryByGameType").html(mytestvar); 
       $("#GamebyCategory").html("<option value=0>Select Game</option>"); 
       $("#LimitVariantByGameByGameType").html("<option value=0>Select Limit Variant</option>"); 
       $("#StakeCategory").html("<option value=0>Select Stake Category</option>"); 
       $("#StakeBuyInByStakeCategory").html("<option value=0>Select Stake Buy In By Stake Category</option>"); 
     }); 

    }); 
    }); 

Trong khi gửi dữ liệu tôi không thể lấy lại giá trị thả xuống nếu Lỗi xuất hiện trên cr eation

+2

Ok, lưu ý đầu tiên của tôi: vì bạn đã tách chế độ xem và mã javascript, tôi nghi ngờ rằng JS nằm trong một tệp riêng biệt. Trong trường hợp này, thao tác này không hoạt động: '(" @ Url.Action ("GetCategoryByGameType", "GameCombination") ' Bạn không thể sử dụng các phương thức trợ giúp MVC trong tệp JS, bạn phải chỉ định URL một cách rõ ràng. –

Trả lời

0

Echo về những gì Andras nói, bạn không thể sử dụng cú pháp dao cạo trong tệp .js nếu điều đó xảy ra.

Bạn cũng nên làm quen với các công cụ gỡ lỗi của mình, bạn đang sử dụng trình duyệt nào? Hầu hết có một bộ công cụ nhà phát triển tốt (một số công cụ được tích hợp sẵn), nơi bạn có thể kiểm tra trang và yêu cầu được gửi khi bạn gửi biểu mẫu.

Bạn cũng có thể sử dụng jquery tương tác trong giao diện điều khiển của trình duyệt để xem xung quanh.

Bạn không hiển thị phương pháp tạo bài đăng của mình, có lẽ nó có liên quan đến mã đó?

0

Điều này sẽ cung cấp cho bạn ý tưởng về những việc bạn có thể làm.

Tôi khuyên bạn nên di chuyển ajax vào chức năng riêng của nó và $.each vào chức năng riêng của nó và gọi hàm $.each trong hàm ajax và hàm ajax trong hàm chính.

$(function() { 
    $("#GameTypeId").change(function() { 
     var theatres = ""; 
     var gametype = ""; 
     var mytestvar = ""; 
     var gametypeid = $(this).val(); 

     mytestvar += "<option value= -1 >Select Category</option>"; 

     $.ajax({ 
      url: '/GameCombination/GetCategoryByGameType', 
      type: 'GET', 
      data: { "gametypeid": gametypeid}, 
      dataType: 'json', 
      success: function (data) { 
       $.each(data, function (index, gametype) { 
        // alert("<option value='" + gametype.Value + "'>" + gametype.Text + "</option>"); 
        mytestvar += "<option value='" + gametype.Value + "'>" + gametype.Text + "</option>"; 
       }); 
       //alert(mytestvar); 
       $("#CategoryByGameType").html(mytestvar); 
       $("#GamebyCategory").html("<option value=0>Select Game</option>"); 
       $("#LimitVariantByGameByGameType").html("<option value=0>Select Limit Variant</option>"); 
       $("#StakeCategory").html("<option value=0>Select Stake Category</option>"); 
       $("#StakeBuyInByStakeCategory").html("<option value=0>Select Stake Buy In By Stake Category</option>"); 
      }, 
      error: function (error) { 
       alert(error.toString()); 
      } 
     }); 

    }); 
}); 
0

Bạn phải tạo đối tượng cho dropdownlist trong View.cs và sau đó gán giá trị. Sau đó khi u cố gắng để có được các giá trị dropdown u phải sử dụng tên đối tượng tiêu đề ViewBag và sau đó giá trị danh sách thả xuống.

Mã dưới đây chỉ định cho radio. Bạn có thể thay đổi nó cho thả xuống

@model MVC_Compiler.Models.UserProgram 
@{ 
ViewBag.Title = "UserProgram"; 
} 
<table style="background-color: #FBF9EF;"> 
      <colgroup> 
       <col style="width: 20%;"> 
       <col style="width: 80%;"> 
      </colgroup> 
      <tr> 
       <td style="vertical-align: text-top"> 
        @Html.RadioButtonFor(up => up.Language, "C")C<br /> 
        @Html.RadioButtonFor(up => up.Language, "C++")C++<br /> 
        @Html.RadioButtonFor(up => up.Language, "C#")C#<br /> 
        @Html.RadioButtonFor(up => up.Language, "VB")VB<br /> 
        @Html.RadioButtonFor(up => up.Language, "Java")Java<br /> 
        @Html.RadioButtonFor(up => up.Language, "Perl")Perl<br /> 
        @Html.HiddenFor(up => up.Language, new { @id = "Select_Language" }) 
        @Html.HiddenFor(up => up.UserName, new { @id = "UserName" }) 
       </td> 

Sau đó, trong mô hình bạn có thể nhận được giá trị nút radio bằng cách sau:

userProgram.Language 

nơi userProgram là ViewBag Tiêu đề.

Các vấn đề liên quan