Manoz
Manoz

Reputation: 6617

return RedirectToAction() not passing object

I have two partialViews in my ASP.NET MVC4 Application-

[HttpGet]
public ActionResult Autocomplete_Search(string accountHead, List<LedgerModel> ledge)
{
    if (!String.IsNullOrEmpty(accountHead)) {
        ledge = (from u in db.LedgerTables
                 where u.AccountHead.Contains(accountHead) && u.FKRegisteredRecord == this.LoggedInUser.RegisterID
                 select new LedgerModel {
                     AccID = u.AccID,
                     Place = u.Place,
                     AccountHead = u.AccountHead,
                     DateAccountHead = Convert.ToDateTime(u.DateAccountHead) != null ? Convert.ToDateTime(u.DateAccountHead) : DateTime.Now

                 }).ToList();
        return RedirectToAction("_ProductSearchList", ledge);

    }
    return View();
    //return Json(ledge, JsonRequestBehavior.AllowGet);
}

And-

public ActionResult _ProductSearchList(List<LedgerModel> ledge) {
            List<LedgerModel> ledger = null;
            if (ledge != null) {
                ledger = (from u in ledge
                         select new LedgerModel {
                             AccID = u.AccID,
                             Place = u.Place,
                             AccountHead = u.AccountHead,
                             DateAccountHead = Convert.ToDateTime(u.DateAccountHead) != null ? Convert.ToDateTime(u.DateAccountHead) : DateTime.Now

                         }).ToList();

                return PartialView(ledge);
            }
            else {
                return PartialView(ledge);
            }
        }

Okay now when I send string through a textbox, Action AutoComplete_Search is called. At the time of redirection to another method named _ProductSearchList I am sending an object ledge of listType to This method. But It says ledge null in _ProductSearchList action's parameters.

However this object is a list type and contains records. How do I get this object ledge which is redirected to action _ProductSearchList?

Upvotes: 0

Views: 2147

Answers (3)

Manoz
Manoz

Reputation: 6617

Thanks all for giving time on this issue.

As @Damian S described about complex object redirecting is notable advice for me.

However I was able to find simplest solution to this problem to use DataDictionary in C#.

I managed it with using TempData[] to store details in easiest way I guess because it is most Precise and trivial technique.

Using TempData[]

Storing record in TempData[] in AutoComplete_Search() controller-

TempData["Records"]= ledge;

Usage in ProductSearchList controller

List<ledgerModel> ledge= (List<ledgerModel>)TempData["Records"];

Solved my problem and headache of playing with objects methods to methods.!

Upvotes: 0

Damian S
Damian S

Reputation: 156

In the first you can`t get List ledge in get request in Autocomplete_Search.

You can`t pass complex object in redirecting. You can only pass a simple scalar value.

Check answer in this thread:

send data between actions with redirectAction and prg pattern

Upvotes: 1

AirL
AirL

Reputation: 1917

The second parameter taken by RedirectToAction is not a model, it is a route. That's why you are not receiving what you expect in your _ProductSearchList action.

I'm not quite sure that something like this would work because i don't know how a list of complex objects could be serialized in the url (or even if this is recommanded), but here is what would be expected :

return RedirectToAction("_ProductSearchList", new { ledge = ledge });

To pass your list, you have the TempData option (quote from MSDN) :

An action method can store data in the controller's TempDataDictionary object before it calls the controller's RedirectToAction method to invoke the next action. The TempData property value is stored in session state. Any action method that is called after the TempDataDictionary value is set can get values from the object and then process or display them. The value of TempData persists until it is read or until the session times out. Persisting TempData in this way enables scenarios such as redirection, because the values in TempData are available beyond a single request.

Don't forget to take a look at Using Tempdata in ASP.NET MVC - Best practice before using it.

Upvotes: 3

Related Questions