简体   繁体   中英

Compilation error of DropDownList in ASP MVC and Entity Framework

I am developing an ASP .Net MVC 3 application using C# and SQL Server 2005.

I am using also Entity Framework and Code First Method.

I have a view 'Application' which contain a DropDownList like this :

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MvcApplication2.Models.FlowViewModel>" %>

<h2>Application</h2>
  <form id="form1" runat="server">

  <h2><%= Html.Encode(ViewData["Message"]) %> </h2>
    <div>         
      <%:Html.Label("Type :")%><%: Html.DropDownListFor(model => model.SelectedGenre, Model.GenreItems)%>
    </div>

I have a compilation error when i try to access to the page of the view :

Compilation Error

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

This is a part of the controller:

[HttpGet]
public ActionResult Application(Genre genre)
{
    var vv = new FlowViewModel();
    vv.GenreItems = db.Genres.ToList();
    return View(vv);
}

and this a part of the model:

public class FlowViewModel
{
    [Key]
    public string IDv { get; set; }
    public List<Genre> GenreItems { get; set; }
    public string SelectedGenre { get; set; } 
}

2nd APPROACH :

Controller :

[HttpGet]
    public ActionResult Application(Genre genre)
    {
        var vv = new FlowViewModel();

        vv.GenreItems = new SelectList(db.Genres.ToList(), "ID_G", "ID_G");


        return View(vv);

    }

View :

<div>         
         <%:Html.Label("Type :")%><%: Html.DropDownListFor(model => model.SelectedGenre, new SelectList(Model.GenreItems,"ID_G","ID_G")%>

   </div>

Model :

[NotMapped]
 public SelectList GenreItems { get; set; }
 public string SelectedGenre { get; set; } 

Error :

Compilation Error

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Doesn't DropDownListFor require a SelectList to populate values? I mean how does your drop down list know which property of the Genre object to use for Value and which for Text? You are passing a List but you need to convert that list to a SelectList. This might not be the cause of your bug but it certainly could be...

 <%: Html.DropDownListFor(model => model.SelectedGenre, new SelectList(Model.GenreItems, "ValueFieldName", "TextFieldName"))%>

I find the problem of the solution.

I have to create the Controller of the Model Genre and Generate its Views using Entity Framework in order to create the table Genre in the base.

Infact, the source of my problem is db (private GammeContext db = new GammeContext();) which is always taken as NULL in the execution because there is no table Genre in the base.

So the code of the view is :

<div>         
         <%:Html.Label("Type :")%><%: Html.DropDownListFor(model => model.SelectedGenre, Model.GenreItems)%>

   </div>

and the controller :

[HttpGet]
        public ActionResult Application(Genre genre)
        {
            var vv = new FlowViewModel();

            vv.GenreItems = new SelectList(db.Genres.ToList(), "ID_G", "ID_G");



            return View(vv);

        }

and this is the model :

public class FlowViewModel
    {

        [Key]
        public string IDv { get; set; }

        [NotMapped]
        public SelectList GenreItems { get; set; }
          public string SelectedGenre { get; set; } 
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM