anouar
anouar

Reputation: 317

How to show a table basing on the value 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.

The DropDownList load its items from the table Genre in the base.

I would like to show a table of values and that using a list already defined in the model.

The values of table depends on the value of DropDownList. I have always this error :

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

This is the code of my view :

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

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Application
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

<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>

   <table border = "transparent">
    <tr>
        <th>

        </th>

        </tr>

        <% foreach (var item in Model.FaItems) { %>
    <tr>
         <td>
            <%: Html.DisplayFor(modelItem => item.Nom_Famille) %> 
        </td>


    </tr>
    <% } %>
    </table>
</form>
</asp:Content>

and this is the Controller :

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

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


        if (vv.SelectedGenre == "Famille")
        {

            vv.FaItems = db.Familles.ToList();


        }
        else if (vv.SelectedGenre == "Sous Famille")
        {
            vv.SFItems = db.Sous_Familles.ToList();

        }
        return View(vv);

    }

and finally this is the model 'FlowViewModel' :

public class FlowViewModel
    {

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

        public List<Famille> FaItems { get; set; }
        public List<Sous_Famille> SFItems { get; set; }
        [NotMapped]
        public SelectList GenreItems { get; set; }
        public string SelectedGenre { get; set; } 
}

Stack Trace :

enter image description here

Upvotes: 0

Views: 222

Answers (2)

000
000

Reputation: 27247

Here is an alternative that will remove the need for duck-type checking.

public class FlowViewModel
{

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

    public List<Famille> FaItems { get; set; }
    public List<Sous_Famille> SFItems { get; set; }
    [NotMapped]
    public SelectList GenreItems { get; set; }
    public string SelectedGenre { get; set; } 

    public FlowViewModel()
    {
        FaItems = new List<Famille>();
        SFItems = new List<Sous_Famille>();
    }
}

Upvotes: 1

Adam Tal
Adam Tal

Reputation: 5961

If this condition is false:

vv.SelectedGenre == "Famille"

You don't set a value to vv.FaItems and therefor as long as vv.SelectedGenre != "Famille", then vv.FaItems is null and will throw a NullReferenceException when trying to use foreach over it.

You can either add an else and put a new empty list, or wrap the foreach with an if (vv.FaItems != null) to avoid the exception.

Upvotes: 3

Related Questions