Zaak
Zaak

Reputation: 482

How to pass List of objects from view to controller action in asp.net mvc3

i have comprehensive search that returns several lists of objects. Each such list is made of objects containing additional lists. The search is very complex in terms of processor load.

once i have the results, i display the original objects via partial view.

  public ActionResult BeginSearch(SearchHomeVM searchParameters)
      {
         var search = new Search(searchParameters);
         linije = search.PretraziLinije();

         return PartialView("_searchResult", linije);
       }

then in that form i wish to display details for a particular item via AJAX call. The problem is i need to use the objects data, not run another search in database. In razor i have:

  @model LinijeSearchResult
  @if (Model.BrojDirektnihLinija > 0)
  {
  <table id="direktneLinije" class="InvisibleTable">
    <thead>
        <tr>
            <th>
                Direktne linije
            </th>
            <th>
            </th>
        </tr>
    </thead>
    @for (int index = 0; index < Model.DirektneLinije.Count; index++)
    {
        LinijaSM item = Model.DirektneLinije[index];
        List<LinijaSM> lin = new List<LinijaSM> { item };
        <tr>
            <td>@item.Naziv
            </td>
            <td>
                @using (Ajax.BeginForm("RenderStanice",
                    new { psd = 0, index = index, lin = lin },
                    new AjaxOptions
                    {
                        HttpMethod = "POST",
                        UpdateTargetId = "staniceLinije",
                        InsertionMode = InsertionMode.Replace
                    }))
                {
                    <input type="submit" value="Stanice" />
                }
            </td>
        </tr>
    }
   </table>

   }
   else
   {
    <text>Nema direktnih linija za odabrane parametre.</text>
    <br />
   }

Here You can see how i am trying to pass the data to controller action that looks like this:

    public ActionResult RenderStanice(List<LinijaSM> lin)
    {
        return PartialView("_staniceSR", lin);
    }

In that controller action i get the empty list. can You please advise on how to accomplish this.

<< EDIT >>

up to this point i have figured out, that the List of any object can not be passed back to controller. the same goes for complex objects. I can pass a integer, but not a list of integers.

Can someone advise me on how to accomplish my goal? I need to pass a list of object back to controller. Can it be done by adding it to context, creating new viewData or something like that?

If that is not possible, can a partial view be rendered via through AJAX, but without a controller action?

Upvotes: 2

Views: 5102

Answers (2)

Zaak
Zaak

Reputation: 482

My current solution to this problem is to persist the data temporarily in Session.

        linije = search.PretraziLinije();
        Session["direktneLinije"] = linije.DirektneLinije;
        Session["jednoPresjedanjeLinije"] = linije.LinijeUzJednoPresjedanje;
        Session["dvaPresjedanjaLinije"] = linije.DirektneLinije;

and pass back and forth only index of the field to access the appropriate data.

    @for (int index = 0; index < Model.DirektneLinije.Count; index++)
    {
        LinijaSM item = Model.DirektneLinije[index];
        <tr>
            <td>@item.Naziv
            </td>
            <td>
                @using (Ajax.BeginForm("RenderStanice",
                    new { index, bpr = 0 },
                    new AjaxOptions
                    {
                        HttpMethod = "POST",
                        UpdateTargetId = "staniceLinije",
                        InsertionMode = InsertionMode.Replace
                    }))
                {

                    <input type="submit" value="Stanice" />
                }
            </td>
        </tr>
    }

I am not sure that this is a very good approach, but since it is a reasonably small amount of data that gets accessed couple of times and then becomes irrelevant, it would be more costly to compute it again or persist it in database.

If any1 can offer a better solution, please do.

Upvotes: 0

Mobius
Mobius

Reputation: 2774

It seems like you should probably be defining your list outside of the For loop and then adding to it in the loop. Otherwise, you'll be defining a new list every time, and only get what the list looks like from the last pass through the for which could be nothing, hence your empty list.

Update: But... even more important, you should be building a view-ready object in the controller before you're passing it to your partial view. Making that object in the view using Razor isn't really very good separation of concerns. Make a view model to send to your full view that contains that list and then just pass that list to your partial view.

Upvotes: 2

Related Questions