0

I have a model which contains a list of another model. Let's say I have a MovieModel:

public class MovieModel
{
    public int MovieId { get; set; }
    public string Title { get; set; }
    public string Director { get; set; }
}

Then I have the RentalModel:

public class RentalModel
{
    public int RentalId { get; set; }
    public string CustomerId { get; set; }
    public List<MovieModel> Movies { get; set; }
}

Then I have a place where all the rentals are displayed, which by clicking on the rental, its details will be displayed, from the "ShowRentals.aspx" to "ShowRentalDetails.aspx"

<% using (Html.BeginForm()) { %>
<% foreach(var rent in Model) { %>
<div class="editor-label">               
    <div class="editor-field">                
               <%: rent.RentalId %>

<%: Html.ActionLink("Details", "ShowRentalDetails", 
                    new {rentalId = rent.RentalId,
                    customerId = rent.CustomerId,
                    movies = rent.Movies,
    })%>    

When I debug, I see that the Movies list is always null. This is because only primitive parameters are passed successfully, such as the Ids. I was never able to pass complex types. I really need this list to be passed on to the controller. Is it maybe because the actionlink is not capable? What other work-arounds can I do? I've been stuck on this for a while. Nevermind the bare code here, this is just to show you what I'm doing with the list. Please help.

(follow up)

In the Controller, here's the two actions, ShowRentals and ShowRentalDetails:

 public ActionResult ShowRentals()
    {
        MembershipUser user = Membership.GetUser(User.Identity.Name, true);
        Guid guid = (Guid)user.ProviderUserKey;

        Entities dataContext = new Entities();
        Member member = dataContext.Members.Where(m => m.UserID == guid).First();

        IEnumerable<RentalModel> toReturn = from r in member.Rentals
                                            select new RentalModel
                                                {
                                                    RentalId = m.RentalID,
                                                    CustomerId = m.CustomerID,
                                                };
        return View(toReturn);


    }

[Authorize]
    public ActionResult ShowRentalDetails(RentalModel model, List<MovieModel> movies)
    {    
        return View("ShowRentalDetails", model);           
    }

I can't set it in ShowRentals because the array of movies in the database is of Movie type and not MovieModel, so the two lists are not compatible. It is null in the model when passed from ShowRentals view and the model is reconstructed by mvc, and it also doesn't work when explicitly passed from the actionlink as a parameter. help!

dumbfreak
  • 37
  • 3
  • 10
  • Is it important to send the entire object or could you send Movie ID and load it on the server? – Huske Aug 12 '11 at 11:57
  • I need to send the entire list. Or you mean send the entire model? Either way I need to access that list on the controller side, to display it on the details view page – dumbfreak Aug 12 '11 at 13:55
  • can I see your controller as well? – Shaokan Aug 12 '11 at 13:58

2 Answers2

0

It is clear you cant pass complex view models through action link. There is a possibility to pass simple objects which does not have any complex properties. There is another way you can do as multiple submit buttons and do a post to controller. Through the submit you have possibilities to post complex view models

0

I believe Html.ActionLink performs a GET and you can't pass complex data types using a GET.

If you could refetch the movie list in your ShowRentDetails controller by using the rental id I think that would be best.

Otherwise, you could look up EditorFor templates. If you make an editorfor template for MovieModel and post a RentalModel to ShowRentDetails then you could get the MovieModel list that way.

See http://weblogs.asp.net/shijuvarghese/archive/2010/03/06/persisting-model-state-in-asp-net-mvc-using-html-serialize.aspx for another way.

On a side note, theres no need to make

List<MovieModel> movies

a second parameter in ShowRentDetails when it's already included in the model

Source: ASP.NET MVC - Trouble passing model in Html.ActionLink routeValues

Community
  • 1
  • 1
DMulligan
  • 8,993
  • 6
  • 33
  • 34
  • I know about that, thing is I was just testing to see how to pass the movie list to the controller. That seemed the only way since the model itself is not being updated, the movie list returns null inside the model once it gets to the controller side – dumbfreak Aug 12 '11 at 14:32
  • Also, I can't seem to be able to fetch the movie list, since on the controller side and accessing the database, i can only get a list of Movie types and not MovieModel types, which is what i need to display in the details view. – dumbfreak Aug 12 '11 at 14:33
  • @dumbfreak Try the link I edited into my answer. Never tried that, but given what you want it might be your best bet. – DMulligan Aug 12 '11 at 14:39