我如何传递一个选定的DropDownListFor项目到我的控制器?MVC3 c#

本文关键字:项目 DropDownListFor 我的 控制器 MVC3 何传递 一个 | 更新日期: 2023-09-27 18:05:46

我这里有一个页面,显示了两个classgroup -学生组/列表-您可以选择其中的一些=>单击按钮=>选择的学生更改classgroup。

默认情况下,第一个classgroup在另一个页面中被选中,第二个学生列表是尚未拥有classgroup的学生。

现在我想添加一个包含数据库中所有classgroup的下拉列表,然后用户(教师)可以选择一个,第二个类列表更改为所选的classgroup。

问题是,我似乎无法在我的控制器中获得选定的类组(我对mvc3相当不熟悉,我是应用信息学的第二年,我们没有关于这方面的书籍/课程,但我似乎无法让它工作)

Controlleraction (DocentController):

public ViewResult ToonStudenten(string id) //id = klasnaam
        {
            var GeselecteerdeKlas = StudentRepo.getStudentenByKlas(id);
            var TweedeKlas = StudentRepo.getStudentenZonderKlas();
            var AlleKlassen = KlasRepo.FindAllKlasNaam();
            var KlasNaam = id;
            ViewBag.EersteKlas = id;
            ViewBag.TweedeKlas = "Deze studenten kregen nog geen klas toegewezen, gelieve de studenten die u naar klas" + id + " wil verzetten te selecteren";
            return View(new ToonStudentenModel(GeselecteerdeKlas, TweedeKlas, AlleKlassen, KlasNaam));
        }
        [HttpPost, ActionName("ToonStudenten")]
        public ActionResult VeranderStudentenKlas(string[] deleteInputsKlas1, string[] deleteInputsKlas2, string id, string id2)//, string klasnaam1, string opdrachtID)
        {
            //TODO: Lijsten worden goed mee gegeven, klasnaam nog NIET (= null)
            if (deleteInputsKlas2 == null && deleteInputsKlas1 == null)
            {
                ModelState.AddModelError("", "Er werden geen Studenten geselecteerd");
                return RedirectToAction("ToonStudenten", "Docent", new { id = id });
            }
            foreach (var item in deleteInputsKlas1)
            {
                try
                {
                    StudentRepo.VeranderKlas(item, null);
                }
                catch (Exception er)
                {
                    ModelState.AddModelError("", "Failed on ID " + item + " : " + er.Message);
                    return View("error");
                }
            }
            foreach (var item in deleteInputsKlas2)
            {
                try
                {
                    StudentRepo.VeranderKlas(item, id);
                }
                catch (Exception er)
                {
                    ModelState.AddModelError("", "Failed on ID " + item + " : " + er.Message);
                    return View("error");
                }
            }
            StudentRepo.SaveChanges();
            return RedirectToAction("ToonStudenten", "Docent", new { id = id });
        }

    }
}

视图模型

{
    public class ToonStudentenModel
    {
        public IEnumerable<Student> EersteKlas { get; set; }
        public IEnumerable<Student> TweedeKlas { get; set; }
        public IEnumerable<Student> GeselecteerdUitEersteKlas { get; set; }
        public IEnumerable<Student> GeselecteerdUitTweedeKlas { get; set; }
        public IEnumerable<string> KlassenDropDown { get; set; }
        public string KlasNaam { get; set; }
        public string geselecteerdeKlasUitDropDown { get; set; }

        public ToonStudentenModel(IEnumerable<Student> GeselecteerdeKlas,
            IEnumerable<Student> AndereKlasOfStudZonderKlas, IEnumerable<string> KlassenDropDown, string KlasNaam)
        {         
            EersteKlas = GeselecteerdeKlas;
            TweedeKlas = AndereKlasOfStudZonderKlas;
            this.KlassenDropDown = KlassenDropDown;
            this.KlasNaam=KlasNaam;
        }
    }
}

视图:

@using (Html.BeginForm(new { id = Model.KlasNaam } ))
{
<div>
@Html.ValidationSummary()
</div>
<div style="float:right;">
    <input type="submit" value="Delete Selected Values" />
</div>
<h2>Index</h2>
<div>Studenten @ViewBag.EersteKlas</div>
<table>
<tr>
    <th></th>
    <th></th>
    <th>
        Studentennummer
    </th>
    <th>
        Voornaam
    </th>
    <th>
        Naam
    </th>
</tr>
@foreach (var item in Model.EersteKlas)
{
<tr>
    <td>
        <input type="checkbox" name="deleteInputsKlas1" value="@item.StudentID" />
    </td>
    <td>
        @Html.ActionLink("Details", "Details", new { id = item.StudentID }) |
    </td>
    <td>
        @item.StudentID
    </td>
    <td>
        @item.Voornaam
    </td>
    <td>
        @item.Naam
    </td>
</tr>
}
</table>
@ViewBag.TweedeKlas
<table>
<tr>
    <th></th>
    <th></th>
    <th>
        Studentennummer
    </th>
    <th>
        Voornaam
    </th>
    <th>
        Naam
    </th>
</tr>
@foreach (var item in Model.TweedeKlas)
{
    <tr>
        <td>
            <input type="checkbox" name="deleteInputsKlas2" value="@item.StudentID" />
        </td>
        <td>
            @Html.ActionLink("Details", "Details", new { id = item.StudentID }) |
        </td>
        <td>
            @item.StudentID
        </td>
        <td>
            @item.Voornaam
        </td>
        <td>
            @item.Naam
        </td>
    </tr>
}
</table>
<input type="hidden" name="id" value = @Model.KlasNaam />
    <input type="hidden" name="id1" value = @Model.opdrachtID />
  <div>OF: Kies een andere klas om studenten in klas @Model.KlasNaam te steken:
  @Html.DropDownListFor(model => Model.KlassenDropDown, new SelectList(Model.KlassenDropDown))


  </div>
}
<div>
    @Html.ActionLink("Terug naar uw klassen", "ToonKlassen", new{id = Model.opdrachtID });
</div>

我如何传递一个选定的DropDownListFor项目到我的控制器?MVC3 c#

我会尝试使用ajax。你必须通过javascript来实现,但在你的控制器中,它看起来像这样:

[HttpPost]
public JsonResult lookup(int groupNumber)

然后控制器可以返回新的学生列表来填充第二个下拉列表。希望对大家有所帮助

最终没有使用Ajax而是通过表单将对象传递给post方法并使用一些if

视图

@Html.DropDownListFor(model => Model.geselecteerdeKlasUitDropDown, new SelectList(Model.KlassenDropDown), "--Zonder klas--")

控制器:

[HttpPost, ActionName("ToonStudenten")]
        public ActionResult VeranderStudentenKlas(string[] deleteInputsKlas1, string[] deleteInputsKlas2, string id, int id1, string geselecteerdeKlasUitDropDown, string searchpara1, string searchpara2)// id = klasnaam1, id1 = opdrachtID, id2 = klasgroep2)
        {
            if (deleteInputsKlas2 == null && deleteInputsKlas1 == null)
            {
                    ModelState.AddModelError("", "Er werden geen Studenten geselecteerd");
                    return RedirectToAction("ToonStudenten", "Docent", new { id = id, id1 = id1, geselecteerdeKlasUitDropDown = geselecteerdeKlasUitDropDown, searchpara1 = searchpara1, searchpara2 = searchpara2 });
            }
                if (deleteInputsKlas1 != null)
                    foreach (var item in deleteInputsKlas1)
                    {
                        try
                        {
                            if (geselecteerdeKlasUitDropDown == "")
                                StudentRepo.VeranderKlas(item, null);
                            else
                                StudentRepo.VeranderKlas(item, geselecteerdeKlasUitDropDown);
                        }
                        catch (Exception er)
                        {
                            ModelState.AddModelError("", "Fout bij Student met ID " + item + " : " + er.Message);
                            return View("error");
                        }
                    }
                if (deleteInputsKlas2 != null)
                    foreach (var item in deleteInputsKlas2)
                    {
                        try
                        {
                            StudentRepo.VeranderKlas(item, id);
                        }
                        catch (Exception er)
                        {
                            ModelState.AddModelError("", "Failed on ID " + item + " : " + er.Message);
                            return View("error");
                        }
                    }
                StudentRepo.SaveChanges();
                return RedirectToAction("ToonStudenten", "Docent", new { id = id, id1 = id1, geselecteerdeKlasUitDropDown = geselecteerdeKlasUitDropDown, searchpara1 = searchpara1, searchpara2 = searchpara2 });