I am using MVC-Viewmodel EF Model First, I am trying to make 2 forms in my View, one for CREATE and the other one for DELETE.
But I cant get it to work… I get this error:
There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'Sname'.
I think this happens beacuse I have this code in my DeleteSubjectType for the dropdownlist and its suppose to be there since im using 2 forms. Somehow the action DeleteSubjectType aint working in my form.
I would appreciate if someone could take a look at my code:
This is my controller with my actions that I want to use in my view beginforms:
public ActionResult CreateSubjectType()
{
CreateViewModel model = new CreateViewModel();
return View(model);
}
[HttpPost]
public ActionResult CreateSubjectType(CreateViewModel model)
{
if (ModelState.IsValid)
{
SubjectType s = new SubjectType();
s.Sname = model.Sname2;
Arep.addsubject(s);
Arep.save();
return RedirectToAction("CreateSubjectType");
}
return View(model);
}
public ActionResult DeleteSubjectType(int id)
{
CreateViewModel model = new CreateViewModel();
List<SubjectType> subjectypes = Arep.getallS();
model.SubjectTypes = new SelectList(subjectypes, "SID", "Sname");
SubjectType SubjectTypeToDelete = Arep.getbysid(id);
return View(model);
}
[HttpPost]
public ActionResult DeleteConfirmed2(int id)
{
SubjectType SubjectTypeToDelete = Arep.getbysid(id);
Arep.DeletSubjects(SubjectTypeToDelete);
Arep.save();
return RedirectToAction("Index");
}
This is my view:
@model NKI3.ViewModels.CreateViewModel
@using (Html.BeginForm("CreateSubjectType","Admin",FormMethod.Post))
{
@Html.ValidationSummary(true)
<fieldset>
<legend>Skapa ny utvärderingsobjekt</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Sname2, "Välj ny utvärderingsobjekt")
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Sname2)
@Html.ValidationMessageFor(model => model.Sname2)
</div>
<p>
<input type="submit" value="Lägg till" />
</p>
</fieldset>
}
@using (Html.BeginForm("DeleteSubjectType","Admin",FormMethod.Post))
{
<fieldset>
<legend>Ta bort utvärderingsobjekt</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Sname, "Befintliga Utvärderingsobjekt")
</div>
<div class="editor-field">
@Html.DropDownListFor(f => f.Sname, Model.SubjectTypes)
@Html.ValidationMessageFor(model => model.Sname)
</div>
<p>
<input type="submit" value="Ta bort" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Tillbaka", "Index")
</div>
Thanks in advance!
From your question it is not clear to which of the two GET actions that you have shown is this view associated: is it the
CreateSubjectTypeaction or theDeleteSubjectTypeaction? Or even maybe some other action which you haven’t shown?If we consider that it is the
CreateSubjectType.cshtmlthen you have a problem because in this view you are attempting to create a dropdownlist in the delete form for theSnameproperty. Except that in yourCreateSubjectTypeyou never initialized theSubjectTypesproperty of your view model to a SelectList as you did in theDeleteSubjectTypeaction.So make sure that you properly initialize this property:
Also your delete form seems to be posting to the
DeleteSubjectTypeaction:except that on your controller I see:
instead of:
which I guess you were forced to do to avoid the overload resolution conflict you had with the GET action with the same name. In this case you could do this: