asp.net-mvc 没有为此对象定义无参数构造函数。在 ASP.NET MVC 控制器中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14374242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
No parameterless constructor defined for this object. in ASP.NET MVC Controller
提问by baynezy
I am sure this is quite straightforward but I am a bit stuck here. The routing defined for my app is just the default. I have the following controller defined.
我相信这很简单,但我有点卡在这里。为我的应用程序定义的路由只是默认设置。我定义了以下控制器。
namespace Baynes.Wedding.Web.Controllers
{
public class AdminController : Controller
{
private readonly IAuthProvider _authProvider;
private readonly IDocumentRepository _documentRepository;
public AdminController(IAuthProvider authProvider, IDocumentRepository documentRepository)
{
_authProvider = authProvider;
_documentRepository = documentRepository;
}
public ViewResult EditDocument(int id)
{
var document = _documentRepository.Select(id);
return View(new DocumentEditViewModel(document));
}
[HttpPost]
public ActionResult EditDocument(DocumentEditViewModel model)
{
if (ModelState.IsValid)
{
_documentRepository.Update(model.ToDocument());
return RedirectToAction("ListDocuments");
}
return View();
}
}
}
When I navigate to /Admin/EditDocument/1/then the first action executes exactly as expected, rendering the following view:-
当我导航到/Admin/EditDocument/1/然后第一个动作完全按预期执行时,呈现以下视图:-
<h2>@ViewBag.Title</h2>
@using (Html.BeginForm("EditDocument", "Admin", FormMethod.Post)) {
@Html.ValidationSummary(true)
@Html.HiddenFor(m => Model.Id)
<div>
@Html.LabelFor(m => Model.Title)
</div>
<div>
@Html.TextBoxFor(m => Model.Title)
</div>
<div>
@Html.LabelFor(m => Model.Body)
</div>
<div>
@Html.TextAreaFor(m => Model.Body)
</div>
<div>
@Html.LabelFor(m => Model.Url)
</div>
<div>
@Html.TextBoxFor(m => Model.Url)
</div>
<input type="submit" value="Edit" />
}
On submitting this I get an error:-
提交时,我收到一个错误:-
No parameterless constructor defined for this object.Other questions seemingly related questions MVC: No parameterless constructor defined for this objectsuggest that it is a to do with the IoC container not being set up properly, but surely the fact that the first action executes without a problem means that isn't the problem here?
No parameterless constructor defined for this object.其他问题看似相关的问题MVC: No parameterless constructor defined for this object表明这与未正确设置 IoC 容器有关,但肯定的是,第一个操作执行没有问题的事实意味着这不是问题这里?
Any help would be greatly appreciated.
任何帮助将不胜感激。
Regards.
问候。
Simon
西蒙
回答by Mediator
add to class DocumentEditViewModel default constructor
添加到类 DocumentEditViewModel 默认构造函数
public DocumentEditViewModel (){}
回答by JTMon
The MVC framework is trying to create an instance of the DocumentViewModel class but it can't find a publicly accessible default constructor (that does not take any arguments). You can either define such a default constructor like @simplyDenis suggested or define a cusotm ModelBinder that can create the instance using your custom constructor.
MVC 框架正在尝试创建 DocumentViewModel 类的实例,但找不到可公开访问的默认构造函数(不带任何参数)。您可以定义像@simplyDenis 建议的这样的默认构造函数,也可以定义可以使用自定义构造函数创建实例的 cusotm ModelBinder。
回答by Vitaliy Markitanov
Most likely you have dependency injections mechanism. But MVC requires "special" way to register container. Look at this link Adding Unity to Your Web Applications, patterns and practices
很可能你有依赖注入机制。但是 MVC 需要“特殊”的方式来注册容器。查看此链接将 Unity 添加到您的 Web 应用程序、模式和实践
回答by chrisn
Does DocumentEditViewModel have a parameterless constructor? I believe this is needed for modelbinding on your post view.
DocumentEditViewModel 有无参数构造函数吗?我相信这是在您的帖子视图上进行模型绑定所必需的。
回答by Jess
I had a slightly different problem. One of my Model classes was abstract.
我有一个稍微不同的问题。我的模型类之一是abstract.
回答by Jamaxack
If DocumentEditViewModel constructor's accessibility level is protected you will get also the same error.
如果 DocumentEditViewModel 构造函数的可访问性级别受到保护,您也会收到相同的错误。

