1

I'm using Entity Framework with MVC4. I'm not sure how to add my ViewModel to my dbcontext. Is it as simple as declaring it in my DbContext? Basically I wish to use my view model in my controller and pass it to my view. I've run into many many problems trying to accomplish this.

namespace BagInventory.Models
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;

    public partial class FreshouseSalesEntities : DbContext
    {
        public FreshouseSalesEntities()
            : base("name=FreshouseSalesEntities")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }

        public DbSet<Materials_Packer> Materials_Packer { get; set; }
        public DbSet<Materials_Product> Materials_Product { get; set; }
        public DbSet<Materials_PackerProduct> Materials_PackerProduct { get; set; }
        public DbSet<Materials_Vendor> Materials_Vendor { get; set; }
        public DbSet<Materials_Log> Materials_Log { get; set; }
        //Below is my view model I wish to add to dbcontext
        public DbSet<BigViewModel> BigViewModel{get;set;}


    }
}

is this the correct way of doing this?

4
  • 1
    Why do you want to add a view model to the data context? Normally, a view model is a model for the views and detached from the database. And you would create some code to migrate the values to and from the view model to and from a database model. Commented Apr 24, 2015 at 14:15
  • 1
    What does your viewmodel represent? Commented Apr 24, 2015 at 14:16
  • 1
    No ... Your DbContext should not have any knowledge of your ViewModel. You would typically select entities from your DbContext and then map them to a VewModel in your Controller. Commented Apr 24, 2015 at 14:16
  • Thank you, I had a feeling I was approaching the issue the wrong way. Commented Apr 24, 2015 at 14:19

3 Answers 3

5

Think about the words in ViewModel. A super simplified way of looking at it would be that a ViewModel is a way to translate data back and forth between the view and model. But, it shouldn't be used as your domain model.

If your ViewModel and your domain model are exactly the same, then you don't need a ViewModel. They aren't always needed. Think of your use-case, and then choose appropriately.

Sign up to request clarification or add additional context in comments.

2 Comments

I'd like to add that, in this case, the Controller should convert the Entity object to the ViewModel.
Thank you for your input. I'm exceedingly new with C# and MVC. I appreciate your help.
3

Basically I wish to use my view model in my controller and pass it to my view.

You can do this within your controller action

var viewModel = new BigViewModel();
// assign values/objects/collections to your viewModel properties here
viewModel.Property1 = x;
viewModel.Property2 = y;
viewModel.Property3 = z;

return View(viewModel);

And that's all you need to do.

Comments

1

1.The answer above says everything that you need to do for passing the data from the controller. BUT you also need to say in your view for example

@model MvcTestApplication.Models.ViewModel

so you can display and get the displayed data like this:

 @Html.EditorFor(model=>model.Materials_Packer.Materials_PackerName)

2.I assume you want after this to use the view to update the database for example. Everything from the view can be used for posting the data. That happens easily by creating a ViewModel object as a parameter in your POST method.And other thing is that if you are missing something in your view, some ID for example, because you dont want to show it, that Id will be null in your object. If your database supports NULL for that Id is ok, but if not you will need to declare it in the post method. So what you need as a code will look something like this:

viewModel.Materials_Packer.Materials_PackerId = value++;
db.Table_Name.Add(viewModel.Materials_Packer);

and this will insert new row in database if model is Valid.

thats how it basicly works

You dont need to declare anything in the context. Also you must use the viewModel.object when querying the database instead the real object.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.