2013-04-25 60 views
5

以下是查看:如何獲得選擇從控制器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> 

以下是控制器: -

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(); 
     } 

以下是我的Jquery對我使用Casceding下拉

$(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>"); 
     }); 

    }); 
    }); 

雖然提交數據我無法取回下拉值如果錯誤發生cr eation

+2

好吧,我的第一個注意:因爲你分開了視圖和JavaScript代碼,我懷疑JS是在一個單獨的文件中。在這種情況下,這是行不通的:'(「@ Url.Action(」GetCategoryByGameType「,」GameCombination「)' 你不能在JS文件中使用MVC幫助器方法,你必須明確指定URL。 – 2013-04-25 12:02:08

回答

0

安德拉斯回聲說,如果發生這種情況,您不能在.js文件中使用剃鬚刀語法。

另外,您應該熟悉您的調試工具,您使用的是哪種瀏覽器?大多數人都有一套很好的開發者工具(有些內置了他們),在那裏你可以檢查頁面和提交表單時發出的請求。

你可以在瀏覽器的控制檯中交互地使用jquery來環顧四周。

您不顯示您的文章創建方法,也許它與該代碼有關?

0

這應該讓你知道你可以做什麼。

我建議你將ajax移入它自己的函數中,並將$.each移入它自己的函數中,並從主函數中將ajax函數和ajax函數調出$.each函數。

$(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

你必須創建一個對象在View.cs dropdownlist然後分配值。之後,當你嘗試獲得dropdown值時,必須使用ViewBag標題對象名稱,然後使用下拉列表值。

下面的代碼指定了單選按鈕。您可以在模型改變它的下拉

@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> 

然後,你可以通過以下方式獲得的單選按鈕值:

userProgram.Language 

其中userProgram是ViewBag標題。

相關問題