没有为此对象定义无参数构造函数.在ASP.NET MVC控制器中 [英] No parameterless constructor defined for this object. in ASP.NET MVC Controller
问题描述
我确信这很简单,但是我有点卡在这里.为我的应用程序定义的路由只是默认路由.我定义了以下控制器.
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();
}
}
}
当我导航到/Admin/EditDocument/1/
时,第一个动作将完全按预期执行,呈现以下视图:-
When I navigate to /Admin/EditDocument/1/
then the first action executes exactly as expected, rendering the following view:-
<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.
其他看似相关的问题 MVC:无无参数构造函数为此对象定义的定义表示与IoC容器的设置不正确有关,但是可以肯定的是,第一个操作执行没有问题,这意味着这里不是问题吗?
No parameterless constructor defined for this object.
Other questions seemingly related questions MVC: No parameterless constructor defined for this object suggest 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?
任何帮助将不胜感激.
致谢.
西蒙(Simon)
推荐答案
添加到类DocumentEditViewModel的默认构造函数
add to class DocumentEditViewModel default constructor
public DocumentEditViewModel (){}
这篇关于没有为此对象定义无参数构造函数.在ASP.NET MVC控制器中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!