Copied to clipboard

Flag this post as spam?

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


  • Nancy A. 44 posts 175 karma points
    May 23, 2019 @ 20:26
    Nancy A.
    0

    Not able to pass model from view to controller

    Hey, on a page, I loop through a collection of objects that contain fields related to a photo album, to create a page of different albums. I envision linking to a specific photo album page by passing the model from the link to the controller - e.g.;

    @model List<cms.ViewModels.PhotoAlbumVm>
    
    @foreach (var photo in Model)
    {
        <div class="col-lg-4 portfolio-item">
    
                <a href="@photo.PhotoAlbumUrl">
                    <img class="card-img-top grow" src="@photo.PhotoAlbumCoverPhoto" alt="placeholder" />
                </a>
                <div class="card-body">
                    <a href="@photo.PhotoAlbumUrl">
                        <h4 class="card-title">@photo.PhotoAlbumTitle </h4>
                    </a>
                    <h5>@photo.PhotoAlbumDate.ToString("MM/dd/yyyy")</h5>
                    <p>
                        @photo.PhotoAlbumSummary
                    </p>
                </div>
                <a [email protected]("SinglePhotoAlbum", "PhotoGallery", new {model = photo})>Link</a>
    
        </div>
    }
    

    The URL.Action isn't working, the model that is being passed to that controller is null. How do I go about passing along a specific instance of the object w/ its contents back to the controller?

  • Nigel Wilson 945 posts 2077 karma points
    May 24, 2019 @ 04:08
    Nigel Wilson
    100

    Hi Nancy

    I might be wrong but you can only pass a simple value in the action, not an object.

    So potentially the following

    <a [email protected]("SinglePhotoAlbum", "PhotoGallery", new {imageId = photo.Id})>Link</a>
    

    Hope this helps

    Nigel

  • Nancy A. 44 posts 175 karma points
    May 24, 2019 @ 13:09
    Nancy A.
    0

    Thanks! I suspected that was the case but was surprised to find examples of objects being passed from the view/controller in a Google search - clearly I didn't understand the use case there.

  • Jonathan Distenfeld 105 posts 618 karma points
    May 24, 2019 @ 11:06
    Jonathan Distenfeld
    0

    Hi Nancy,

    i think Nigel ist right. You can't pass objects in a GET - Request. But you could use newtonsoft.json to convert the object into a json-string and convert it back to your model-type again in your controller.

    See this example: https://www.newtonsoft.com/json/help/html/SerializingJSON.htm

    ~Jonathan

  • Nancy A. 44 posts 175 karma points
    May 24, 2019 @ 13:10
    Nancy A.
    0

    Perfect! I'll try using this and see what comes out of it. Thank you!

Please Sign in or register to post replies

Write your reply to:

Draft