0

I need to create an empty form for database update. In the controller this is called: public ViewResult Create() => View("Edit", new List());

<TargetFramework>netcoreapp2.2</TargetFramework>
I have a class MeetingListContext : DbContext that has 3 models:
1. DbSet List
2. DbSet ATime (Look up table to show time name instead of time id)
3. DbSet DOW (Look up table to show day name instead of day id)

Model

namespace DEIGList.Models
{
    public partial class MeetingListContext : DbContext
    {
        public MeetingListContext()
        {
        }

        public MeetingListContext(DbContextOptions<MeetingListContext> options)
            : base(options)
        {
        }

        public virtual DbSet<ATime> ATime { get; set; }
        public virtual DbSet<Dow> Dow { get; set; }
        public virtual DbSet<List> List { get; set; }

The View:

@model DEIGList.Models.MeetingListContext

In the controller:

 public ViewResult Create() => View("Edit", new List());

The expected result will be a form with empty data fields for fill in data. I would like to be able to use drop downs (lookups) for the day and time.

But when I use @model DEIGList.Models.MeetingListContext I get:

InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'DEIGList.Models.List', but this ViewDataDictionary instance requires a model item of type 'DEIGList.Models.MeetingListContext'.

Suggestion?

2 Answers 2

0

You need change from @model DEIGList.Models.MeetingListContext

to @model DEIGList.Models.List

The type of @model must same type with type of model in View("Edit", new List())

Update:

If you want to have drop down for Day and Time, you need update Controller:

public ViewResult Create() {
    MeetingListContext ctx = new MeetingListContext();
    ViewBag.ATime = ctx.ATime.ToList();
    ViewBag.Dow  = ctx.Dow.ToList();
    return View("Edit", new List());
}

Add dropdown helper in View:

@Html.DropDownList("ATime", new SelectList(ViewBag.ATime, "ATimeID", "ATimeName"))
@Html.DropDownList("Dow", new SelectList(ViewBag.Dow, "DowID", "DowName"))
Sign up to request clarification or add additional context in comments.

Comments

0

Your post pointed me towards a solution. The solution was to do a select with an id == 0.

var list = _context.List
     .Where(l => l.ListId == 0)
     .Include(t => t.ATime)
     .Include(d => d.Dow)
     .ToList();
 ViewBag.thelist = list;

 return View("Edit", new List());

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.