【问题标题】:How to get selected drop down values from Controller MVC如何从 Controller MVC 中获取选定的下拉值
【发布时间】:2023-03-04 10:15:01
【问题描述】:

以下是视图:

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

以下是Controller:-

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

    });
    });

在提交数据时,如果创建时出现错误,我将无法取回下拉值

【问题讨论】:

  • 好的,我的第一个注意事项:因为您将视图和 javascript 代码分开,我怀疑 JS 在单独的文件中。在这种情况下,这不起作用:("@Url.Action("GetCategoryByGameType", "GameCombination") 您不能在 JS 文件中使用 MVC 辅助方法,您必须明确指定 URL。

标签: c# jquery asp.net-mvc razor


【解决方案1】:

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

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

您也可以在浏览器的控制台中以交互方式使用 jquery 进行查看。

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

【讨论】:

    【解决方案2】:

    这应该让你知道你可以做什么。

    我建议你将ajax移到它自己的函数中,将$.each移到它自己的函数中,并将$.each函数从ajax函数中调用,将ajax函数从main函数中调用。

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

    【讨论】:

      【解决方案3】:

      您必须在 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 标题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-10-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多