【发布时间】:2012-04-17 06:31:51
【问题描述】:
我已经浏览了这里的大多数关于 MVC 以及如何使用 DropDownList 的博客文章,但收效甚微。
我试图在这个链接上模仿一个帖子,但显然不适合我:Drop-Down Menu Causing Invalid Model State. ASP.NET MVC 3
目标是在 HTTP GET 创建视图中有一个下拉列表供用户选择家庭车库拥有多少辆汽车。
我目前收到的错误是:
编译器错误消息:CS1061: 'MvcPropertyManagement.Models.Property' 不包含定义 对于 'GarageId' 并且没有扩展方法 'GarageId' 接受第一个 'MvcPropertyManagement.Models.Property' 类型的参数可以是 找到(您是否缺少 using 指令或程序集引用?)
第 84 行:第 85 行: 第 86 行:@Html.DropDownListFor(model => model.GarageId, Model.LkupGarageTypes) 第 87 行:
@Html.ValidationMessageFor(model => model.GarageType) 第 88 行:
我的模特:
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; }
}
控制器:
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; }
}
}
创建视图:
<div class="editor-field">
@Html.DropDownListFor(model => model.GarageId, Model.LkupGarageTypes)
@Html.ValidationMessageFor(model => model.GarageType)
</div>
【问题讨论】:
标签: asp.net-mvc