【问题标题】:How to get the selected value from drop down list and submit it to my Details view? [duplicate]如何从下拉列表中获取所选值并将其提交到我的详细信息视图? [复制]
【发布时间】:2014-01-14 10:26:44
【问题描述】:

我有一个 mvc razor 表单。我想要的是从项目下拉列表中提交用户的选择并导航到详细信息视图以访问所选项目的信息。

目前,当我单击提交按钮时,我成功导航到详细信息视图,但那里绝对没有数据显示。

所选值必须来自我在视图中使用 jquery 构建的 Items 下拉列表...

如您所见,我尝试使用“ItemsID”,但在查看我的详细信息视图的刹车点时,我得到的只是空值。

你能帮帮我吗?

查看:

@using (Html.BeginForm("Details", "Bookings", FormMethod.Post))
{
<fieldset>
    <legend> Type/Item</legend>
    <label for="Items">Item Types </label>
    @Html.DropDownList("department", ViewBag.ItemTypesList as SelectList, "Select a Type", new { id = "ItemTypeID" })
    <div id="ItemsDivId">
        <label for="Items">Items </label>
        <select id="ItemsID" name="Items"></select>
    </div>
    <p>
        <input type ="submit" value="Submit" id="SubmitID" />
    </p>
 </fieldset>
}


    <script type="text/javascript">

        $('#ItemTypeID').on('change', function () {
            $.ajax({
                type: 'POST',
                url: '@Url.Action("GetItemTypeForm")',
            data: { itemTypeId: $('#ItemTypeID').val() },
            success: function (results) {
                var options = $('#ItemsID');
                options.empty();
                options.append($('<option />').val(null).text("- Select an Item -"));
                $.each(results, function () {
                    options.append($('<option />').val(this.ItemsID).text(this.Value));
                });
            }
        });
    });
</script>

控制器:

public ActionResult Details(string ItemsID)
        {
            var item = db.Items.Find(ItemsID);

            return View(item);
        }

[HttpPost]
        public JsonResult GetItemTypeForm(string itemTypeId)
        {
            //pseudo code
            var data = from s in db.Items
                       where s.ItemType.ItemTypeName == itemTypeId
                       select new { Value = s.ItemName, ItemsID = s.ItemId };


            return Json(data);
        }

【问题讨论】:

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


    【解决方案1】:

    感谢大家的关注,

    正确的答案是使用“物品”

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-02
      • 2015-12-06
      • 1970-01-01
      • 1970-01-01
      • 2016-02-08
      相关资源
      最近更新 更多