0

I have a view, where by clicking on the button, the data in the lists of the model should be synced. The ActionLink has the following code:

@Html.ActionLink("Accept Change", "AcceptChange", "Question", new { index = i, draftAnswers = Model.DraftAnswers, finalAnswers = Model.FinalAnswers }, new { @class = "acceptButton" })

The model has the following structure:

[NotMapped]
[Serializable]
public class SurveyFill
{
    public List<Question> FinalAnswers { get; set; }
    public List<Question> DraftAnswers { get; set; }
    public Country Country { get; set; }
}

The AcceptChange ActionResult on the QuestionController:

 public ActionResult AcceptChange(int index, List<Question> draftAnswers, List<Question> finalAnswers)
    {
        int country = finalAnswers.FirstOrDefault() != null ? finalAnswers.FirstOrDefault().Country.ID : 0;

        if (country != 0)
        {
            finalAnswers[ind].SyncProperties(draftAnswers[ind]);

            SurveyFill survey = new SurveyFill();
            survey.Country = this.Database.Countries.Where(c => c.ID == country).SingleOrDefault();
            survey.FinalAnswers = finalAnswers;
            survey.DraftAnswers = draftAnswers;
            return View(survey);
        }
        return RedirectToAction("Index");
    }

The issue is that the lists from the model are not send to the AcceptChange. Anyone understands where is the bottleneck?
I tried to read some threads on the forum, but couldn't find a mistake.

syam
  • 799
  • 1
  • 12
  • 30
JCaptain
  • 3
  • 1
  • 7
  • 1
    What is the purpose of `NotMappedAttribute` set on class level? Usually that attribute set on properties which are not mapped as table columns in EF. – Tetsuya Yamamoto Apr 18 '18 at 09:16
  • @TetsuyaYamamoto the class is not mapped to any table in EF. That's why it's on the class level. – JCaptain Apr 18 '18 at 09:18
  • 1
    You cannot pass complex objects (`DraftAnswers` and `FinalAnswers`) using `@Html.ActionLink()` - look at the query string your generating. Which is fortunate because they are collections and you would almost certainly exceed the query string limit and throw an exception. You pass the ID of the object and get the collections in the GET method if you need them –  Apr 18 '18 at 09:19
  • @StephenMuecke, thanks for comment. But then, if there are more than 200 objects in each list, that would be difficult to control and implement. Aren't the model data somehow stored on the context and accessible? Since, the only Thing I want is to just update a `finalAnswer` with certain `index`, as it's noticeable from **AcceptChange**. – JCaptain Apr 18 '18 at 09:24
  • Well, I think passing collections with GET method with `ActionLink` is not a good idea. Putting collections as part of query string means that it may break default `maxQueryStringLength` limit set in config file - use collections only in controller action and you just need to pass unique values to be processed. – Tetsuya Yamamoto Apr 18 '18 at 09:25
  • _Aren't the model data somehow stored on the context and accessible?_ - Of course not. The only data the controller receives is what the browser sends it (the web is stateless). And if you really have 200 objects, then you would certainly exceed the query string limit and throw an exception –  Apr 18 '18 at 09:37

0 Answers0