problemy z ASP MVC + Html.DropDownList () przy użyciu wzorca ModelView
Ostatnio wysłałem pytanie o listę rozwijaną helpera html i sprawiłem, że działało (tutaj). Ale teraz zdecydowałem, że przejście na Wzorce ModelView jest o wiele mądrzejsze, więc mam dostęp do mocno wpisanych metod w moich widokach itd. Dokonałem pewnych poprawek w kodzie w moim innym temacie w następujący sposób:
VacatureFormViewModel:
public class VacaturesFormViewModel
{
public Vacatures Vacature { get; private set; }
public SelectList EducationLevels { get; private set; }
public SelectList Branches { get; private set; }
public SelectList CareerLevels { get; private set; }
Repository repository;
// Constructor
public VacaturesFormViewModel(Vacatures vacature)
{
this.Vacature = vacature;
this.repository = new Repository();
this.EducationLevels = new SelectList(repository.GetAllEducationLevels(),"ID","Name",vacature.EducationLevels);
this.Branches = new SelectList(repository.GetAllBranches(),"ID","Name",vacature.Branches);
this.CareerLevels = new SelectList(repository.GetAllCareerLevels(), "ID", "Name", vacature.CareerLevels);
}
}
BanenController:
//
// GET: /Banen/Create
public ActionResult Create()
{
Vacatures vacature = new Vacatures();
return View(new VacaturesFormViewModel(vacature));
}
//
// POST: /Banen/Create
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(Vacatures vacatureToAdd)
{
if (ModelState.IsValid)
{
try
{
// TODO: Add insert logic here
repository.AddToVacatures(vacatureToAdd);
repository.SaveChanges();
// Return to listing page if succesful
return RedirectToAction("Index");
}
catch (Exception e)
{
return View();
}
}
}
I mój widok Create.aspx (jego część):
<% using (Html.BeginForm()) {%>
<fieldset>
<legend>Fields</legend>
<p>
<label for="Title">Title:</label>
<%= Html.TextBox("Title", Model.Vacature.Title) %>
<%= Html.ValidationMessage("Title", "*") %>
</p>
<p>
<label for="Content">Content:</label>
<%= Html.TextArea("Content", Model.Vacature.Content) %>
<%= Html.ValidationMessage("Content", "*") %>
</p>
<p>
<label for="EducationLevels">EducationLevels:</label>
<%= Html.DropDownList("EducationLevels", Model.EducationLevels)%>
<%= Html.ValidationMessage("EducationLevels", "*") %>
</p>
<p>
<label for="CareerLevels">CareerLevels:</label>
<%= Html.DropDownList("CareerLevels", Model.CareerLevels)%>
<%= Html.ValidationMessage("CareerLevels", "*")%>
</p>
<p>
<label for="Branches">Branches:</label>
<%= Html.DropDownList("Branches", Model.Branches)%>
<%= Html.ValidationMessage("Branches", "*")%>
</p>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
<% } %>
Do prowadzenia użyłemNerdDinner samouczek ScottGu i przeczytałem tutaj różne tematy.
Moje pytanie brzmi, czy można pozwolić MVC ASP ustawić mój poziom kariery, poziom wykształcenia i branche (listy rozwijane) automatycznie, ponieważ obecnie zwraca ciąg identyfikacyjny, który nie jest tym, czego chcę. Kiedy zmieniam tworzenie listy SelectList na:
this.CareerLevels = new SelectList(repository.GetAllCareerLevels(), vacature.CareerLevels);
Tak więc bez „ID” i „Name” nie zapisuje również (chyba wciąż jest zwracany jako ciąg znaków w metodzie post, a nie sam obiekt), a obok niego wyświetla w widoku jako: vacature. EducationLevels itp. Tak więc nie są wymienione nazwy, ale sam obiekt.
Ostatnie pytanie Krótko mówiąc, moje pytanie brzmi, czy możliwe jest użycie tego podejścia do ustawienia mojej branży, poziomu edukacyjnego i poziomu kariery. Więc nie automatycznie?
W takim przypadku nadal muszę korzystać z takich rzeczy jak:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(FormCollection form)
{
Vacatures vacatureToAdd = new Vacatures();
// Retrieve the education level by its ID
if (!form["EducationLevels"].Equals(""))
{
Guid educationID = new Guid(form["EducationLevels"]);
vacatureToAdd.EducationLevels = repository.GetEducationLevelByID(educationID);
}
W moim kontrolerze? A może są inne, gładsze opcje.