views:

511

answers:

2

I have a List View which has a strongly typed ViewModel which includes the entity list I am working with together with some other session-type stuff I am carrying aruound.

On clicking an item in the list (an Html.ActionLink) to go to the Details view I can easily pass the entity id. But I also want to pass the rest of the ViewModel from the View.

I can build the ActionLink with various QueryString parameters and then a custom ModelBinder can pick them up and hydrate the ViewModel object for me again. However, I don´t like this.

I can get the custom ViewModel to rehydrate when it is POSTed back to the same page etc., but how can I get the ViewModel into a Controller Action using a GET to another View without using a ModelBinder and by simply placing the ViewModel object as a parameter in the target Action method?

A: 

I don't think you can do what you want, which from what I gather is the following:

  1. While rendering the List action, you want to create a link to another action (potentially on another controller, but that's not key here)

  2. This action should, when fired, have access to the original ViewModel that existed when the ActionLink method was first executed.

Unfortunately, items #1 and #2 are completely disconnected from each other and so there is no real mechanism to pass the current ViewModel to a link that will be executed in a different session.

That's not to say there aren't workarounds, of course:

You can generate the action link like so:

<%= 
    Html.ActionLink( 
                    "Label", 
                    "Action",  
                    "Controller",
                    new {Parameter1 = Model.Data1, Parameter2 = Model.Data2},
                    null
                   ) 
%>

Within your linked action method, you can instantiate the ViewModel using the parameters passed to that action method.

David Andres
Yes, I have tried soemthing like this, i.e. passing the strongly typed viewmodel bits to a GET ActionLink on the same controller but different action with a parameter that matches the actionlink parameters both in type and name. However, this always returns a null parameter in the Action which was why I used the QueryString approach. I thought it would be bound by a default modelbinder. Note: I am assuming there is a default modelbinder configured in MVC 1.0 because I cannot see a ComplexModelBinder as shown in tutorials
Redeemed1
actually just found the defaultModelBinder so please ignore the end of last comment. Going to try this with the default now configured
Redeemed1
Yes, I can see that on entry to the target Action I have an instantiated parameter ok, however, the values are not reflected into it.
Redeemed1
@Redeemed1: What does your action method look like (the one you're linking to)
David Andres
The target Action signature is public ActionResult Details(ProjectUIPViewModel projectUIPViewModel, int id, int? TransactionPage, int? SubProjectsPage, int? ActiveAccordion) and there is a ModelBinder for the first Parameter.On further thought I decided I am going to try to use a POST back to the orginal Method which first rendered the view and then try a RedirectToAction from there which worked for me from a Search Action to a List action
Redeemed1
A: 

I just tried this, and it seemed to work. Also tried the without a form and it worked also. Not sure if this is exactly what you wanted though.

Action

[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Index(TestModel model)
{
  ViewData["Message"] = model.Test1;
  return View();
}

Model

public class TestModel
{
  public string Test1 { get; set; }
  public string Test2 { get; set; }
}

View

<% using (Html.BeginForm("Index","Home",FormMethod.Get))
{ %>
        <%=Html.TextBox("Test1")%>
        <%=Html.TextBox("Test2")%>
        <input type=submit value=submit />
<% }%>
mxmissile
thanks mxmissile, but no, this isn´t really what I want.
Redeemed1