0

So why isn't this working out for me? I have a actionlink which should take a model instance back into the controller.

Questions view

@foreach (var question in Model.Select(item => item as Question))
{
  ..
  @Html.ActionLink(@question.Title, "SingleQuestion", "Posts", question, null)
  ..
}

Controller

public ActionResult SingleQuestion(Question question)
{
   return View();
}

This won't work Error enter image description here

But when use this solution: Is there any way to pass a whole model via html.actionlink in ASP.NET MVC 3?

It does, Is this because a model instance can be to complex?

Thanks

Community
  • 1
  • 1
Jim Vercoelen
  • 1,048
  • 2
  • 14
  • 40
  • Well, according to the error message your model doesn't have a parameterless constructor. So the model binder can't create an instance of it. You're either going to need to create a parameterless constructor or a custom model binder – David Dec 12 '15 at 23:39
  • 1
    It seems that your model has a constructor with parameters. In this case, you need another one **without** parameters like **public Question() {}** for EF. In the answear of the linked question, you pass the model as parameter - Not a very secure and clean way, you'd be better to pass a key like **QuestionId** as parameter. Then you can fetch the question related to the given id in the target ActionResult. – Lion Dec 12 '15 at 23:43
  • @David yes, i also tried adding a parameterless constructor but the model itselve is a child class. – Jim Vercoelen Dec 13 '15 at 00:16
  • @Lion Thanks for response, I'am going to try this out! – Jim Vercoelen Dec 13 '15 at 00:16
  • @JimVercoelen: Being a child class doesn't preclude having a parameterless constructor. The model binder isn't really interested in the inheritance hierarchy of the class, just in creating an instance of it. – David Dec 13 '15 at 00:17
  • @David ow oke, yeah I am just started on asp so it's all a bit strange! But thanks for responses! – Jim Vercoelen Dec 13 '15 at 00:25

2 Answers2

2

Html.ActionLink helper method generates an anchor tag and clicking on which usually do a GET request to the target url.

You should not be trying to pass a complex object to a GET action method. You should be passing a unique id using which the GET action can rebuild/re-query your complex object and use that in your view.

@foreach (var question in Model.Select(item => item as Question))
{
  <p>
  @Html.ActionLink(@question.Title, "SingleQuestion", "Posts",
                                               new {questionId=question,Id}, null)
  </p>
}

And in your GET Action method

public ActionResult SingleQuestion(int questionId)
{
   Question q = GetQuestionFromSomeWhere(questionId);
   if(q!=null)
   {
     return View(q);
   }
   return View("NotFound");
}
Shyju
  • 214,206
  • 104
  • 411
  • 497
0

For your Question class, add at least 1 empty constructor with no parameters:

public class Question {
     public Question () {} //add this
     public Question (string param1) { //your constructor }
}
Lucas Rodriguez
  • 1,203
  • 6
  • 15