0

I have a controller method like this:

    public ActionResult Create(string project_title)
    {
        return View(new ProjectViewModel()
        {
            Title = project_title
        });
    }

On another page, I want to take the value of a textbox, and send as a querystring parameter to this method.

My view on this page looks like this:

@using (Html.BeginForm("Create", "Project", FormMethod.Get))
{
    <input type="text" id="project_title" name="project_title" placeholder="Title of your project" class="input-lg" />
    <input type="submit" value="Get started" class="btn btn-success btn-lg" />
}

However, while I thought that would work, I get the following error:

No parameterless constructor defined for this object.

1 Answer 1

4

I suspect that the error message you are getting has nothing to do with the code you have shown in your question. Probably the ProjectController class in which this Create action is defined has some non-default constructor for which you haven't properly configured your DI framework to inject the correct dependency:

public class ProjectController : Controller
{
    public ProjectController(IFoo foo)
    {
    }

    public ActionResult Create(string project_title)
    {
        return View(new ProjectViewModel()
        {
            Title = project_title
        });
    }
}

If you don't have a default (parameterless) parameterless constructor of your controller ASP.NET MVC default's controller factory won't be able to instantiate this ProjectController unless you are using some DI framework and specify the concrete type to be injected in this constructor.

Sign up to request clarification or add additional context in comments.

1 Comment

You are correct. I am starting to setup my windsor framework and forgot to hook it up - duh! You just saved me for a long time debugging this code... (note to self: goto bed sometimes). THANKS!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.