Copied to clipboard

Flag this post as spam?

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


  • Christina 127 posts 390 karma points notactivated
    Nov 05, 2019 @ 13:11
    Christina
    0

    Populate a List in Viewmodel gets null when post from View -> Surfacecontroller

    Hi I have created a FormViewModel who contains a List.

    I populate the List on render and return the model to the partialview.

    The properties from list displays in the view, but when i post the FormViewModel back to Surfacecontroller the List is empty.

    Can someone point me in right direction.

    Thanks /Christina

  • Ben Abbott 19 posts 124 karma points
    Nov 05, 2019 @ 13:37
    Ben Abbott
    0

    The data will only be sent to the surface controller if it's attached to a form element (textbox, dropdown etc). If you need the data sent to the surface controller, one option would be to store the data in a hidden input field

  • Christina 127 posts 390 karma points notactivated
    Nov 05, 2019 @ 14:51
    Christina
    0

    Hi Ben Thank you for helping me. I generate the list dynamically and ID and name will be different. and I get the following error No parameterless constructor has been defined for this object. Im trying to save content from FormViewModel -> Model

    @for(int i = 0; i < Model.Allslopesform.Count(); i++)
    {
        @Html.CheckBoxFor(m => m.Allslopesform[i].IsOpen) 
        @Model.Allslopesform[i].SlopeName
    }
    

    generated html

    <input id="Allslopesform_0__IsOpen" name="Allslopesform[0].IsOpen" type="checkbox" value="true"><input name="Allslopesform[0].IsOpen" type="hidden" value="false"> slope1 ...and so on
    
  • Ben Abbott 19 posts 124 karma points
    Nov 05, 2019 @ 16:36
    Ben Abbott
    0

    The 'No parameterless constructor has been defined for this object' error is usually thrown when a class is being serialised but can't (as it doesn't have a default constructor with no parameters)

    Is the type used for the 'Allslopesform' property a custom class and if so, does it have constructors?

  • Christina 127 posts 390 karma points notactivated
    Nov 05, 2019 @ 18:55
    Christina
    0

    Many Thanks For helping me I figured it out, I had to populate an array

    /C

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies