Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7585661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:13:50+00:00 2026-05-30T19:13:50+00:00

I am using MVC-Viewmodel EF Model First, I am trying to make 2 forms

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T19:13:51+00:00Added an answer on May 30, 2026 at 7:13 pm

    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 CreateSubjectType action or the DeleteSubjectType action? Or even maybe some other action which you haven’t shown?

    If we consider that it is the CreateSubjectType.cshtml then you have a problem because in this view you are attempting to create a dropdownlist in the delete form for the Sname property. Except that in your CreateSubjectType you never initialized the SubjectTypes property of your view model to a SelectList as you did in the DeleteSubjectType action.

    So make sure that you properly initialize this property:

    public ActionResult CreateSubjectType()
    {
        CreateViewModel model = new CreateViewModel();
        List<SubjectType> subjectypes = Arep.getallS();
        model.SubjectTypes = new SelectList(subjectypes, "SID", "Sname");
        return View(model);
    }
    

    Also your delete form seems to be posting to the DeleteSubjectType action:

    @using (Html.BeginForm("DeleteSubjectType","Admin",FormMethod.Post))
    

    except that on your controller I see:

    [HttpPost]
    public ActionResult DeleteConfirmed2(int id)
    

    instead of:

    [HttpPost]
    public ActionResult DeleteSubjectType(int id)
    

    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:

    [HttpPost]
    public ActionResult DeleteSubjectType(int id, FormCollection fc)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using MVC-Viewmodel and EF model first for my projekt These are my
I'm using ASP.NET MVC 2 and here's the issue. My View Model looks something
I am using ASP.NET MVC 2, and am using a view-model per view approach.
I am using EF model first and using viewmodels in MVC, I have problems
Have you tried using MVC or any other UI pattern for GWT client code.
2 questions: Does using MVC make it any easier to build 508/WAI compliant sites?
I am using asp.net mvc 2.0(default binding model) and I have this problem. I
I'm using the module-level validator: 'PropertiesMustMatch' on my view-model, like so: [PropertiesMustMatch(Password, PasswordConfirm)] public
Using the EditorFor templates is a really nice feature of ASP.Net MVC 3, but
In ASP.NET MVC there is Model, View and Controller. MODEL represents entities which are

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.