MVC 6 Entity Scaffolding bug
See original GitHub issue public class Quiz
{
public int Id { get; set; }
[Required, MaxLength(255)]
public string Title { get; set; }
[Required]
public DateTime DateCreated { get; set; }
public Difficulty DifficultyLevel { get; set; }
}
public enum Difficulty
{
Beginner = 1,
Intermediate,
Advanced
}
public class Question
{
public int Id { get; set; }
[Required]
public Quiz Quiz { get; set; }
[Required]
public int QuizId { get; set; }
[Required]
public string QuestionContent { get; set; }
}
I created a new controller using MVC + Entity Framework, it generated the following code on Create:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Id,QuestionContent,QuizId")] Question question)
{
if (ModelState.IsValid)
{
_context.Add(question);
await _context.SaveChangesAsync();
return RedirectToAction("Index");
}
ViewData["QuizId"] = new SelectList(_context.Quizzes, "Id", "Quiz", question.QuizId);
return View(question);
}
In my view
@model MeMeA.Guru.Entities.Question
<h2>Create</h2>
<form asp-action="Create">
<div class="form-horizontal">
<h4>Question</h4>
<hr />
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="QuestionContent" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="QuestionContent" class="form-control" />
<span asp-validation-for="QuestionContent" class="text-danger" />
</div>
</div>
<div class="form-group">
<label asp-for="QuizId" class="col-md-2 control-label"></label>
<div class="col-md-10">
<select asp-for="QuizId" class ="form-control" asp-items="ViewBag.QuizId"></select>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
</form>
<div>
<a asp-action="Index">Back to List</a>
</div>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
The view always generates an error
An unhandled exception occurred while processing the request.
NullReferenceException: Object reference not set to an instance of an object.
Microsoft.AspNetCore.Mvc.Rendering.MultiSelectList.Eval(Object container, String expression)`
I just wanted to let you guys know this error occurs in the default code generation when adding a controller and view.
Issue Analytics
- State:
- Created 7 years ago
- Comments:7 (3 by maintainers)
Top Results From Across the Web
Error trying to scaffold a view in ASP.NET Core 6.0 MVC
I'm trying to scaffold a new razor view using Visual Studio. I select a template, my model and my DbContext , then I...
Read more >Unable to Resolve Scaffolding Issue in ASP.NET Core ...
The error in my original post was encountered when I was attempting to scaffold an MVC Controller with Views using Entity Framework.
Read more >MVC 6 Entity Scaffolding bug · Issue #247
I just wanted to let you guys know this error occurs in the default code generation when adding a controller and view.
Read more >Fix Update: Scaffolding failed, could not load information for ...
In this video, I going to fix this error, Scaffolding failed in ... dot net core and entity framework core | dot net...
Read more >How To Fix Common Errors Using ASP.NET MVC Scaffolding
Error : EntityType 'PersonModel' has no key defined. Define the key for this EntityType. Another common error is the following: Error no key ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This is fixed for the upcoming Dev14 release.
@bwoodard007 This was a known issue with scaffolding in preview1
The problem is the way the Select list is created in the controller.
ViewData["QuizId"] = new SelectList(_context.Quizzes, "Id", "Quiz", question.QuizId);
This needs to change to
ViewData["QuizId"] = new SelectList(_context.Quizzes, "Id", "Title", question.QuizId);
More details: https://github.com/aspnet/Scaffolding/issues/232