0
votes

I have looked through most of the blog postings here dealing with MVC and how to use a DropDownList with little success.

I tried to mimic a post at this link but obviously not working for me: Drop-Down Menu Causing Invalid Model State. ASP.NET MVC 3

The objective is to have a drop-down listing for a user to choose how many cars a home garage holds in the HTTP GET Create View.

The error that I am presently receiving is:

Compiler Error Message: CS1061: 'MvcPropertyManagement.Models.Property' does not contain a definition for 'GarageId' and no extension method 'GarageId' accepting a first argument of type 'MvcPropertyManagement.Models.Property' could be found (are you missing a using directive or an assembly reference?)

Line 84: Line 85: Line 86: @Html.DropDownListFor(model => model.GarageId, Model.LkupGarageTypes) Line 87:
@Html.ValidationMessageFor(model => model.GarageType) Line 88:

My Model:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.ComponentModel.DataAnnotations;
    using System.Web.Mvc;
    using MvcPropertyManagement.Models;
    using MvcPropertyManagement.Models.ViewModels;

    namespace MvcPropertyManagement.Models
    {
        public class Property
        {
            public bool Garage { get; set; }
        
            [Display(Name="Garage Capacity")]
            public string GarageType { get; set; }
    }

Controller:

    using System;
    using System.Data;
    using System.Collections.Generic;
    using System.Data.Entity;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using MvcPropertyManagement.Models;
    using MvcPropertyManagement.Models.ViewModels;

    public ActionResult Create()
    {
        PropertyViewModel viewModel = new PropertyViewModel();
        viewModel.LkUpGarageType = new SelectList(db.LkUpGarageTypes, "GarageTypeID",         "LkUpGarageType"); 
        return View(viewModel);
    } 

PropertyViewModel:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using MvcPropertyManagement.Models;

    namespace MvcPropertyManagement.Models.ViewModels
    {
        public class PropertyViewModel
        {
            public int? GarageId { get; set; }
            public IEnumerable<SelectListItem> LkUpGarageType { get; set; }        
        }
    }

Create View:

<div class="editor-field">
    @Html.DropDownListFor(model => model.GarageId, Model.LkupGarageTypes)
    @Html.ValidationMessageFor(model => model.GarageType)
</div>
1

1 Answers

0
votes

Seems like you are using MvcPropertyManagement.Models.Property as the model, not MvcPropertyManagement.Models.ViewModels.PropertyViewModel where GarageId is.

Try changing the model to MvcPropertyManagement.Models.ViewModels.PropertyViewModel on the view with:

@model MvcPropertyManagement.Models.ViewModels.PropertyViewModel

UPDATE: The property class, which is used a the model:

public class Property
{
  public bool Garage { get; set; }

  [Display(Name="Garage Capacity")]
  public string GarageType { get; set; }

  public int? GarageId { get; set; }

  public IEnumerable<SelectListItem> LkUpGarageType { get; set; } 
}

The create action:

public ActionResult Create()
{
  Property viewModel = new Property();
  viewModel.LkUpGarageType = new SelectList(db.LkUpGarageTypes, "GarageTypeID",         "LkUpGarageType"); 
  return View(viewModel);
}