【问题标题】:MVC 3 DropDownList Syntax for a View/Model/Controller视图/模型/控制器的 MVC 3 DropDownList 语法
【发布时间】: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


    【解决方案1】:

    似乎您使用的是 MvcPropertyManagement.Models.Property 作为模型,而不是 GarageId 所在的 MvcPropertyManagement.Models.ViewModels.PropertyViewModel。

    尝试在视图上将模型更改为MvcPropertyManagement.Models.ViewModels.PropertyViewModel:

    @model MvcPropertyManagement.Models.ViewModels.PropertyViewModel
    

    更新: 模型中使用的属性类:

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

    创建动作:

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

    【讨论】:

    • 当我更改为您的 @model 时,此页面的所有其他字段属性都失去了与属性模型的关系。
    • @Shawn 听起来您正在尝试在同一页面上使用两种不同的模型,但这是无法做到的。然后你需要在PropertyViewModel 作为Property 类的属性。
    • 我只是想关注我在上面列出的 Stackoerverflow 上的另一个帖子 。这篇文章有一个模型、控制器、视图和一个额外的 ViewModel 类,我完全不知道为什么。
    • @Shawn 使用另一篇文章中的视图模型作为模型。为了解决您的问题,我会将PropertyViewModel 的属性添加到Property 或其他方式。
    • 我已将属性添加回我的 Property 类模型,但无法弄清楚如何让 Get Create (Controller) 新建一个 SelectList。
    猜你喜欢
    • 1970-01-01
    • 2015-01-19
    • 1970-01-01
    • 1970-01-01
    • 2011-09-12
    • 2012-02-12
    • 2012-11-08
    • 2013-12-06
    • 1970-01-01
    相关资源
    最近更新 更多