如何将所选项目传递给控制器​​MVC5

时间:2016-12-22 14:38:11

标签: jquery asp.net-mvc

控制器:

public ActionResult searchGroup(int groep)
        {
            var returnValue = convertWerkvorm(db.Werkvormens.Where(f => f.GroepenWerkvormID == groep).ToList());
            return View(returnValue);
        }

Partialview:

@model alina1617.Models.DropDownModel

<h2>Groepen</h2>
<div>
        <div><select id="@Html.IdFor(m => m.selectedItem)" name="@Html.NameFor(m => m.selectedItem)">
            @foreach (var groepModel in ViewBag.groepen)
            {
                <option value="@groepModel.id" title="@groepModel.Beschrijving">@groepModel.Naam</option>
            }
        </select>
        <input type="button" id="zoekgroep" value="Zoeken" onclick="location.href='@Url.Action("Create", "WerkvormController")'" />
        </div>
    </div>

我想在单击按钮时将所选组的ID传递给控制器​​。我应该使用Jquery还是可以使用与MVC相关的东西?

2 个答案:

答案 0 :(得分:0)

function(event)

使用dropdownfor并在Model中添加Selectedvalue属性,并将所有这些放在表单中并使按钮输入类型提交

答案 1 :(得分:0)

你是否应该使用jQuery或MVC是主观的,所以我把它留给你。你可以这样做。谷歌搜索它,有很多例子。

至于“MVC相关”,请看一下这样的事情(只是一个例子):

@Html.DropDownList("groep", (SelectList)ViewBag.Groepen)

您需要在表单标记中包装列表和按钮,并更改您的按钮类型以提交。如果控制器中的方法不存在,则可能需要[HttpPost] atttribute。

您还可以查看类似的DropDownListFor对象。

我相信你可能会问一些已经得到很多回答的事情。请查看this postthis one

相关问题