简体   繁体   English

ASP.NET MVC - 如何将所选项目从 DropDownListFor 传输到 Controller

[英]ASP.NET MVC - How to transfer the selected item from DropDownListFor to the Controller

I'm making a DropDownListFor and pulling its items with the model.我正在制作一个DropDownListFor并使用 model 拉动它的项目。

But I cannot transfer the selected item from DropDownListFor to the controller.但我无法将所选项目从DropDownListFor转移到 controller。

Index.cshtml:索引.cshtml:

@using (Html.BeginForm("Index", "StudentTable", FormMethod.Get))
{
    <p>
        <p>Class:</p>
        <div class="form-group">
            <select class="form-control" id="SelectedId">
                @foreach (var item in Model)
                {

                    <option value="@item.ClassId">@item.Class.ClassName</option>
                }
            </select>
        </div>
        <input type="submit" value="Ara">
    </p>
}

Controller: Controller:

StudentDatabaseContext db = new StudentDatabaseContext();
public ActionResult Index(string SelectedId)
{
    var studentList = db.StudentTables.Include(x => x.Class).ToList();
    var degerler = from d in db.StudentTables select d;
    if (!string.IsNullOrEmpty(SelectedId))
    {
        degerler = degerler.Where(m => m.ClassId.ToString().Contains(SelectedId));
    }
    return View(degerler.ToList());
}

What is the problem?问题是什么? How can I solve it?我该如何解决? Thanks for the help.谢谢您的帮助。

Provide the name attribute to the select element, so the value will be passed as SelectedId to the controller.select元素提供name属性,因此该值将作为SelectedId传递给 controller。

<select class="form-control" id="SelectedId" name="SelectedId">
    ...
</select>

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM