Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Luke 4 posts 51 karma points
    Aug 07, 2014 @ 18:15
    Luke
    0

    Post data to surface controller

    Hi, I'm having some trouble posting data to a surface controller. I have the following model:

    public class QuestionAndAnswerModel
    {
        public string Answer { get; set; }
        public string Question { get; set; }
    }
    

    And I have a Post method in my surface controller as follows:

    public ActionResult PostAnswers(IList<QuestionAndAnswerModel> questionAndAnswers)
    {
        ....
    }
    

    Now its almost working, I know the method gets called as I can hit breakpoints in the method, but my problem is, the questionsAndAnswers parameter is always null. I've checked Request.Form and it is populated with the data I have submitted. I've tried a few different types for the parameter (IEnumerable, IList) but that doesn't make any difference. So I'm wondering if my input fields are set up correctly.. Currently they are in the following format:

    <input name="[@index].Question" type="hidden" value="Question" />
    <textarea name="[@index].Answer" class="form-control" rows="3"></textarea>
    

    The naming convention of each element is based off what I have found referenced here for standard controllers. I'm just wondering if anyone knows why my form values aren't coming through correctly? Hopefully its really obvious! Thanks

  • Luke 4 posts 51 karma points
    Aug 08, 2014 @ 12:11
    Luke
    0

    Finally figured this out! Basically, because I was using question numbers starting from 1, I also passed that in as the @index for the inputs on the page. This number needs to start at 0 otherwise it seems asp.net doesn't work out that it is a list.

Please Sign in or register to post replies

Write your reply to:

Draft