0

I am an experienced programmer but relatively new to c# mvc. I am attempting to create my first viewmodel to combine two models into one so a view can access members from both. I have followed instructions on combining distinct models into one view model:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ViApplication.Models;
using System.ComponentModel.DataAnnotations;

namespace ViApplication.ViewModel
{
    public class TemplateMTMQuestionViewModel
    {
        public TemplateVISpdat ThisTemplate { get; set; }
        public MtmTemplateViSpdatQuestion ThisMTMQuestion { get; set; }
    }
}

I have created a controller:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ViApplication.ViewModel;
using ViApplication.Models;
using System.Net;

namespace ViApplication.Controllers
{
    public class TemplatesMTMQuestions : Controller
    {
        private VulnerabilityIndexDatabaseEntities db = new VulnerabilityIndexDatabaseEntities();

        public ActionResult AddQuestionToTemplate(long? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }

            TemplateVISpdat templateVISpdat = GetTemplateByID(id);

            if (templateVISpdat == null)
            {
                return HttpNotFound();
            }

            TemplateMTMQuestionViewModel TMTMQVM = new TemplateMTMQuestionViewModel();

            TMTMQVM.ThisTemplate = GetTemplateByID(id);
            TMTMQVM.ThisMTMQuestion = GetBlankMtmTemplateViSpdatQuestion();

            return View(TMTMQVM);
        }

        public TemplateVISpdat GetTemplateByID(long? id)
        {
            TemplateVISpdat templateVISpdat = db.TemplateVISpdats.Find(id);

            return templateVISpdat;
        }

        public MtmTemplateViSpdatQuestion GetBlankMtmTemplateViSpdatQuestion()
        {
            MtmTemplateViSpdatQuestion TMTMQVM = new MtmTemplateViSpdatQuestion();

            return TMTMQVM;
        }
    }
}

This compiles fine. But when I try to create a view from AddQuestionToTemplate and select Empty and my ViewModel I get:

Unable to retrieve metadata for ViApplication.ViewMdoel.TemplateMTMQuestionViewModel. One or more validation errors were detected during model generation. TemplateMTMQuestionViewModel::EntityType TemplateMTMQuestionViewModel has no key defined

The only difference between this project and other projects is that I am using database first.

Any help would be greatly appreciated.

derloopkat
  • 6,232
  • 16
  • 38
  • 45
Aden
  • 1
  • 1
    For a start view models should not contain data models, just the properties you need from each data model that you need in the view - [What is ViewModel in MVC?](https://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc). Choose the `Empty(without model)` option and then just add `@model ViApplication.ViewModel.TemplateMTMQuestionViewModel` at the top of the view that is created –  Oct 19 '17 at 23:58
  • As mentioned above, it is not a good practice to have ViewModels containing data models. DB classes go into your domain models... and ViewModel is what you want to display in the view... and you can use mapping (say Automapper) to map these two... Check out this great post about how to do this:http://stevemichelotti.com/aspnet-mvc-view-model-patterns/ – Hooman Bahreini Oct 20 '17 at 00:19
  • @argentina55 *DB classes go into your domain models*-not really. They are your entity models. Domain models do not need to reflect the structure of data in db and domain models will have operations and more encapsulation than simple data containers like entity models. – CodingYoshi Oct 20 '17 at 00:34
  • `TemplateMTMQuestionViewModel has no key defined` try adding a key – derloopkat Oct 20 '17 at 00:36
  • @derloopkat i dont think thats the approach to take: its not like that will be needing to be part of the db context. It is just used by the view for display purposes. – CodingYoshi Oct 20 '17 at 00:40
  • @CodingYoshi, design problems were already mentioned above, but OP asked about this error and this issue will come up again later – derloopkat Oct 20 '17 at 00:44

0 Answers0