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 7518791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:48:02+00:00 2026-05-30T01:48:02+00:00

I am using MVC-Viewmodel with repository pattern with EF on my project. I have

  • 0

I am using MVC-Viewmodel with repository pattern with EF on my project.

I have 3 tables, Question, CoreValue, SubjectType.
SubjectType and CoreValue are many to many associated with Question and these two tables are not suppose to get any new values, but users can create questions so Question table will get new data when a user creates it. I use two dropdownlists for CoreValue and SubjectType so that the user can choose a CoreValue and a SubjectType when they create a Question.

Here is my HTTPGET controller action:

    // GET: /Admin/Create

    public ActionResult Create()
    {

    CoreValueRepository Crep = new CoreValueRepository();
    SubjectTypeRepository Srep = new SubjectTypeRepository();

        CreateViewModel model = new CreateViewModel();
        List<SubjectType> subjectypes = Srep.getall();
        List<CoreValue> corevalues = Crep.getall();
        model.SubjectTypes = new SelectList(subjectypes, "SID", "Sname");
        model.CoreValues = new SelectList(corevalues, "CID", "Cname");

        return View(model);

    }  

And here is my Viewmodel:

public class CreateViewModel
{

public string QuestionText { get; set; }
public string Sname { get; set; }
public string Cname { get; set; }

public SelectList SubjectTypes { get; set; }
public SelectList CoreValues { get; set; }

}

I use Repository for CRUD operations and viewmodels for handling data in UI.

Now I have to code the HTTPPOST Action Create in my controller for inserting Question data to my database, and the questions need to be tagged with CoreValue ID and SubjectType ID. So I was thinkin about to start coding the HTTPOST action Create, and I was wondering if someone could help me out with this.

Thanks in advance!

Best Regards!

  • 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-30T01:48:02+00:00Added an answer on May 30, 2026 at 1:48 am

    This is how i would handle it :

    In your ViewModel, replace :

    public class CreateViewModel {
    
    public string QuestionText { get; set; }
    public string Sname { get; set; }
    public string Cname { get; set; }
    
    public int SubjectTypesID { get; set; }
    public int CoreValuesID { get; set; }
    }
    

    In your HTTPGET put your list in Viewbags :

    public ActionResult Create()
    {
    
        CoreValueRepository Crep = new CoreValueRepository();
        SubjectTypeRepository Srep = new SubjectTypeRepository();
    
        CreateViewModel model = new CreateViewModel();
        ViewBag.SubjectTypes = Srep.getall();
        ViewBag.CoreValues = Crep.getall();
    
        return View(model);
    
    }  
    

    To use the viewbag in your view you can use this :

    @Html.DropDownList("SubjectTypesID ", new SelectList(ViewBag.SubjectTypes as  System.Collections.IEnumerable, "SID", "Sname", Model.SubjectTypesID ))
    
    @Html.DropDownList("CoreValuesID ", new SelectList(ViewBag.CoreValues as  System.Collections.IEnumerable, "CID", "Cname", Model.CoreValuesID ))
    

    Your HTTPOST :

    [HTTPOST]
    public ActionResult Create(CreateViewModel model)
    {
        //Now with your model you have the Id of CoreValue and SubjectType
        //You could do
     if (ModelState.IsValid)
     {
       QuestionRep.Add(model);
       return RedirectToAction("Index");
     }
    
    
       return View(model);
    }  
    

    Hope this can help you 🙂

    Edit :

    in my repository I do :

     public void Add(Model.Models.LabExam.Examen entity)
        {
            using (var context = new PDSIDataContext())
            {
                var exam = BindModelExamenToRepExamen(entity);
                context.Examen.InsertOnSubmit(exam);
                context.SubmitChanges();
            }
        }
    

    Binding methods (Repository.Examen represents my table, Repository is my project where I have a .dbml to represent my DB):

         private static Repository.Examen BindModelExamenToRepExamen(Model.Models.LabExam.Examen modelExamen)
        {
            return new Repository.Examen
            {
                ID_Examen = modelExamen.ID,
                ID_Examen_Type = modelExamen.ID_Examen_Type,
                Date_Prescription = modelExamen.Date_Prescription,
                Realise_Le = modelExamen.Realise_Le,
                Statut = modelExamen.Statut,
                Fait = modelExamen.Fait,
                ID_Examen_Sous_Type = modelExamen.ID_Examen_Sous_Type,
                ID_Examen_Sous_Sous_Type = modelExamen.ID_Examen_Sous_Sous_Type,
                ID_Patient = modelExamen.ID_Patient,
                Commentaires = modelExamen.Commentaires
            };
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have you tried using MVC or any other UI pattern for GWT client code.
Say you have an ASP.NET MVC project and are using a service layer, such
I am new to MVC and using the repository pattern in an attempt to
Good evening everyone. I am currently using MVC 3 and I have a viewmodel
I have an ASP.NET MVC project in C# using Forms Authentication and Active Directory
I am using MVC-Viewmodel and EF model first for my projekt These are my
Using MVC with an observer pattern, if a user action requires polling a device
Just getting started using MVC in ASP.NET, I'm going to have it so users
I'm trying to get started with the repository pattern and ASP.NET MVC, and I
As I familiarize myself with Asp.Net MVC, I am using MVC 2, I have

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.