Asp.net MVC ViewModel错误-未为此对象定义无参数构造函数

Asp.net MVC ViewModel错误-未为此对象定义无参数构造函数,asp.net,.net,asp.net-mvc,asp.net-mvc-3,razor,Asp.net,.net,Asp.net Mvc,Asp.net Mvc 3,Razor,我想知道如何在创建操作上使用ViewModel?我尝试了几个在论坛上找到的例子,但没有一个解决我的问题。我绞尽脑汁想了好几天,但不知道出了什么问题 无论何时单击“创建”按钮,都会出现以下错误: 没有为此对象定义无参数构造函数 @model MvcMusicStore.ViewModels.AlbumViewModel @{ ViewBag.Title = "Create"; } <h2>Create</h2> <script src="@Url.Con

我想知道如何在创建操作上使用ViewModel?我尝试了几个在论坛上找到的例子,但没有一个解决我的问题。我绞尽脑汁想了好几天,但不知道出了什么问题

无论何时单击“创建”按钮,都会出现以下错误: 没有为此对象定义无参数构造函数

@model MvcMusicStore.ViewModels.AlbumViewModel

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Album</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.AlbumItem.GenreId, "Genre")
        </div>
        <div class="editor-field">
            @Html.DropDownList("Genres", String.Empty)
            @Html.ValidationMessageFor(model => model.AlbumItem.GenreId)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.AlbumItem.ArtistId, "Artist")
        </div>
        <div class="editor-field">
            @Html.DropDownList("Artists", String.Empty)
            @Html.ValidationMessageFor(model => model.AlbumItem.ArtistId)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.AlbumItem.Title)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.AlbumItem.Title)
            @Html.ValidationMessageFor(model => model.AlbumItem.Title)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.AlbumItem.Price)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.AlbumItem.Price)
            @Html.ValidationMessageFor(model => model.AlbumItem.Price)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.AlbumItem.AlbumArtUrl)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.AlbumItem.AlbumArtUrl)
            @Html.ValidationMessageFor(model => model.AlbumItem.AlbumArtUrl)
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>
StoreManager.cs-代码段

public class AlbumViewModel
    {
        public AlbumViewModel()
        {
            //  nothing
        }

        public Album AlbumItem { get; set; }
        public SelectList Genres { get; set; }
        public SelectList Artists { get; set; }
    }

public class Album
    {
        public Album()
        {
            //  nothing
        }

        public virtual int AlbumId { get; set; }
        public virtual int GenreId { get; set; }
        public virtual int ArtistId { get; set; }
        public virtual string Title { get; set; }
        public virtual decimal Price { get; set; }
        public virtual string AlbumArtUrl { get; set; }
        public virtual Genre Genre { get; set; }
        public virtual Artist Artist { get; set; }
    }

public class Artist
    {
        public Artist()
        {
            // nothing
        }

        public virtual int ArtistId { get; set; }
        public virtual string Name { get; set; }
    }

public class Genre
    {
        public Genre()
        {
            // nothing
        }

        public virtual int GenreId { get; set; }
        public virtual string Name { get; set; }
        public virtual string Description { get; set; }
        public virtual List<Album> Albums { get; set; }
    }
公共类AlbumViewModel
{
公共相册视图模型()
{
//没什么
}
公共相册相册项目{get;set;}
公共选择列表类型{get;set;}
公共选择列表艺术家{get;set;}
}
公开课相册
{
公共相册()
{
//没什么
}
公共虚拟int AlbumId{get;set;}
公共虚拟整数GenreId{get;set;}
公共虚拟整数ArtistId{get;set;}
公共虚拟字符串标题{get;set;}
公共虚拟十进制价格{get;set;}
公共虚拟字符串AlbumArtUrl{get;set;}
公共虚拟类型{get;set;}
公共虚拟艺术家{get;set;}
}
公共级艺术家
{
公共艺术家()
{
//没什么
}
公共虚拟整数ArtistId{get;set;}
公共虚拟字符串名称{get;set;}
}
公共课体裁
{
公共体裁()
{
//没什么
}
公共虚拟整数GenreId{get;set;}
公共虚拟字符串名称{get;set;}
公共虚拟字符串描述{get;set;}
公共虚拟列表相册{get;set;}
}

如果每次看到这个问题我都有五分镍币。它通常与模型属性的命名以及如何在
下拉列表中使用它们有关。99.999%的时间是因为人们使用
Html.DropDownList()
并将其命名为与其
SelectList
相同的名称。这就是您应该使用强类型的
DropDownListFor
的原因之一

在这种情况下,您的问题是您有名为
Genres
Artists
SelectList
s,然后在您的视图中您有:

@Html.DropDownList("Genres", String.Empty)
@Html.DropDownList("Artists", String.Empty)
看,是同一个名字

您应该做的是更改模型,使
SelectList
s命名为
GenreList
ArtistList
。然后,将视图更改为使用强类型模型

@Html.DropDownListFor(m => m.AlbumItem.GenreID, Model.GenreList)
@Html.DropDownListFor(m => m.AlbumItem.ArtistID, Model.ArtistList)
发生这种情况的原因是您正在向控制器发布一个名为“流派”的值。默认的模型绑定器尽职尽责地在模型中查找称为类型的内容并实例化它。但是,它找到的不是ID或字符串,而是一个名为Genres的SelectList,当它尝试实例化它时,它发现没有默认构造函数

这就是你的错误。因此,这里充满了关于同一件事的问题。

类似于我在表格中添加了一个下拉列表,但是在我的情况下,它没有(也不打算)与表格一起提交,因为它不在
标记之外:

@Html.DropDownList("myField", Model.MyField)
由于该模型包含仅用于显示的字段,这也导致了为该对象定义的
无参数构造函数
错误,因为该字段根本没有提交

在本例中,我通过添加排除绑定修复了它:

public ActionResult Foo(int id, int? page, [Bind(Exclude = "MyField")]MyModel model)

对我来说,问题在于BeginForm()方法本身。看起来是这样的:

@using (Html.BeginForm("MyAccount", "MyController", Model))
从没有下拉列表的另一个项目的登录页面复制并粘贴


无论如何,从参数列表中删除模型,一切正常:)

在上面的代码中添加了
Genre
Artist
。唯一似乎没有无参数构造函数的是SelectList()。如果您对这些内容进行注释会怎么样?@fvss-新MusicStoreDB()的构造函数是什么?此外,您可能应该发布堆栈跟踪(黄色屏幕),因为这将为您提供在此处抛出错误的行。您为我节省了几个小时,而且在使用webform中使用的模型对象时也可能发生这种情况,并且不像我遇到的那样,模型对象中没有无参数构造函数。
@using (Html.BeginForm("MyAccount", "MyController", Model))