如何从Controller MVC中获取选定的下拉值

ujj*_*val 5 c# asp.net-mvc jquery razor

以下是视图:

 <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>
Run Code Online (Sandbox Code Playgroud)

以下是控制器: -

 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();
        }
Run Code Online (Sandbox Code Playgroud)

以下是我使用Casceding Dropdown的Jquery

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

    });
    });
Run Code Online (Sandbox Code Playgroud)

提交数据时,如果创建错误,我无法获取下拉值

Mas*_*low 0

正如 Andras 所说,如果发生这种情况,则不能在 .js 文件中使用 razor 语法。

另外你应该熟悉你的调试工具,你使用什么浏览器?大多数都有一套很好的开发人员工具(有些是内置的),您可以在其中检查页面以及提交表单时发出的请求。

您还可以在浏览器的控制台中交互地使用 jquery 来查看。

您没有显示您的帖子创建方法,也许它与该代码有关?