开发者

How do I pass a selected DropDownListFor item to my Controller? MVC3 c#

I have a page here that shows two classgroups - groups/lists of students - of which you can select some => click the button => selected students change classgroups.

By default the first classgroup is selected in another page, and the second list of students are students who don't have a classgroup yet.

Now I want to add a dropdownlist with all the classgroups in the database, and then the user (teacher) can select one and the second list of classes changes to the selected classgroup.

The problem is that I can't seem to get the selected classgroup in my controller (I'm fairly new to mvc3, I'm in my second year of applied informatics atm and we don't get books/a course on this but I can't seem to get it to work)

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 });

        }


    }
}

ViewModel

{
    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 { 开发者_如何学Goget; 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;
        }
    }
}

View:

@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>


I'd try using ajax. You'd have to do this via javascript, but in your controller it would look something like this:

[HttpPost]
public JsonResult lookup(int groupNumber)

Then the controller can return the new list of students to populate the second drop-down. Hope this helps


ended up not using Ajax but just passing the object through the form to the post method and use some if's there

View

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

Controller:

[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 });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜