【问题标题】:How do I get the value of my second dropdownlist in MVC4?如何获取 MVC4 中第二个下拉列表的值?
【发布时间】:2014-01-24 07:51:11
【问题描述】:

我有这两个级联下拉列表,它们填充得很好。但是当我点击提交时,我总是得到 NULL 作为第二个的值。我的猜测是我必须对 javascript 做点什么,但我在那个部门的技能严重缺乏。请帮忙!

删除代码,添加整个视图

我什至没有第二个下拉菜单的 HtmlHelper,它在视图中看起来像这样:

删除代码,添加整个视图

工作得很好,我的意思是,它填充得很好,具体取决于第一个下拉列表中选择的内容。也许这是需要改变的部分?问题是我一无所知。

编辑:

这是从表单中读取信息并将其提交到数据库的代码。

[HttpPost]
        public ActionResult Returer(ReturerDB retur)
        {
            if (ModelState.IsValid)
            {
                db2.ReturerDB.AddObject(retur);
                db2.SaveChanges();

                return RedirectToAction("Returer");
            }

            return View("Returer");
        }

EDIT2

整个视图代码:

@model Fakturabolag.Models.ReturerDB

@{
    ViewBag.Title = "Returer";
}

<script type="text/javascript" src="../../Scripts/jquery-1.7.1.js"></script>
<script type="text/javascript">

    $(document).ready(function () {
        $("#bolag").prop("disabled", true);
        $("#Kund").change(function () {
            if ($("#Kund").val() != "- Välj bolag -") {
                var options = {};
                options.url = "/companies/getbolag";
                options.type = "POST";
                options.data = JSON.stringify({ country: $("#Kund").val() });
                options.dataType = "json";
                options.contentType = "application/json";
                options.success = function (bolag) {
                    $("#bolag").empty();
                    for (var i = 0; i < bolag.length; i++) {
                        $("#bolag").append("<option>" + bolag[i] + "</option>");
                    }
                    $("#bolag").prop("disabled", false);
                };
                options.error = function () { alert("Fel vid bolagshämtning!"); };
                $.ajax(options);
            }
            else {
                $("#bolag").empty();
                $("#bolag").prop("disabled", true);
            }
        });
    });

</script>



    <h2>Returer</h2>

    @using (Html.BeginForm())
    {

    @Html.ValidationSummary(true)

    <fieldset>
        <legend>Returer</legend>
        <br /><br />


        <div class="editor-label">
            <b>Kund</b>
        </div>

        <div class="editor-field">
            @Html.DropDownListFor(model => model.Kund, ViewData["kundLista"] as SelectList)
            @Html.ValidationMessageFor(model => model.Kund)
        </div>

        <div class="editor-label">
            <b>Bolag</b>
        </div>

        <select id="bolag"></select>

        <div class="editor-label">
            <b>Pris</b>
        </div>
        <div class="editor-field">
            @Html.TextBox("pris", null, new { style = "width: 150px" })
            @Html.ValidationMessageFor(model => model.Pris)
        </div>

        <div class="editor-label">
            <b>Datum</b>
        </div>
        <div class="editor-field">
            @Html.TextBox("datum", ViewData["datum"] as String, new { style = "width: 150px" })
            @Html.ValidationMessageFor(model => model.Datum)
        </div>

        <p>
            <input type="submit" value="Lägg till" />
        </p>
    </fieldset>
    }

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

【问题讨论】:

  • 你是如何读取值的,贴出相关的服务器端代码。
  • 您可以在 Firebug / Chrome Dev Tools 的 Network 标签中使用 Chrome 和 FF 检查提交的 POST 数据
  • @Esa 我想我发布了你提到的内容。
  • 但是你是打ajax调用还是正常提交​​?您使用默认模型绑定器吗?因为我想知道你是如何传递它的——首先没有分配“名称”属性,其次它不是命名约定的一部分,当你使用强类型视图和 Html 扩展方法时,默认情况下它存在,这使默认模型绑定器能够适当地解释表单集合元素。也许您可以粘贴整个视图代码?
  • 应该是正常提交​​。我是 MVC 的新手,从未使用过它,但在工作中得到了几个 MVC 项目,我必须继续开发。我边走边学,没有官方知识。所以请耐心等待我尝试回答您的问题:)

标签: c# javascript asp.net-mvc-4 cascadingdropdown


【解决方案1】:

目前,您没有在操作中检索附加值“bolag”。我猜你的模型没有名为“bolag”的属性。您可以添加它,也可以向您的操作添加其他参数,如下所示:

    [HttpPost]
    public ActionResult Returer(ReturerDB retur, string bolag)
    {
        if (ModelState.IsValid)
        {
            db2.ReturerDB.AddObject(retur);
            db2.SaveChanges();

            return RedirectToAction("Returer");
        }

        return View("Returer");
    }

之后,从下拉列表中选择的值应该自动在 borag-parameter 中。

编辑。

您还需要在您的选择中添加名称属性:

  <select id="bolag" name="bolag"/>

【讨论】:

  • 模型包含“bolag”的属性。我刚刚将 name 属性添加到我的选择中,它起作用了!对我来说是一个非常愚蠢的错误,但是发生了一些事情:P 感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 2020-05-24
  • 1970-01-01
  • 1970-01-01
  • 2020-01-07
  • 2022-08-15
  • 2013-12-06
  • 1970-01-01
  • 2021-09-07
相关资源
最近更新 更多