baynezy
baynezy

Reputation: 7036

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

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. 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?

Any help would be greatly appreciated.

Regards.

Simon

Upvotes: 10

Views: 46352

Answers (6)

Jamaxack
Jamaxack

Reputation: 2460

If DocumentEditViewModel constructor's accessibility level is protected you will get also the same error.

Upvotes: 0

Jess
Jess

Reputation: 25029

I had a slightly different problem. One of my Model classes was abstract.

Upvotes: 0

Vitaliy Markitanov
Vitaliy Markitanov

Reputation: 2438

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

Upvotes: 1

JTMon
JTMon

Reputation: 3199

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.

Upvotes: 5

chrisn
chrisn

Reputation: 308

Does DocumentEditViewModel have a parameterless constructor? I believe this is needed for modelbinding on your post view.

Upvotes: 1

Mediator
Mediator

Reputation: 15378

add to class DocumentEditViewModel default constructor

public DocumentEditViewModel (){}

Upvotes: 15

Related Questions