0

I'm trying to do this:

public ActionResult Index(List<Client> Client)
{
    if (Client != null)
        return View(Client);

    return View(db.Client.ToList());
}

[HttpPost]
public ActionResult Search(string cnpj)
{
    List<Client> Client = db.Client // here it finds one client
        .Where(c => cnpj.Equals(c.Cnpj))
        .ToList();

    return RedirectToAction("Index", Client);
}

After action Search, it goes to Index, but the Client parameter is always null..

Someone knows why?


I do it and works:

public ActionResult Index(string cnpj)
{
    if (!string.IsNullOrEmpty(cnpj))
    {
        List<Client> clients = db.Client
        .Where(c => cnpj.Equals(c.Cnpj))
        .ToList();

        return View(clients);
    }

    return View(db.Client.ToList());
}
MuriloKunze
  • 15,195
  • 17
  • 54
  • 82

2 Answers2

0

y cant to simply call the function instead of redirecting? call this from the Search Action

Index(Client)

what happens in redirect is that a HTTP Code of 302 is sent to the browser with the redirect URL then browser sends a new request to the server and therefore the Client is null because the browser cant send it back. EDIT : -
after reading the comments in this case u have two options
1.one is to make another Index action and change the parameter type to string so now ull be able to call that directly
2.Use TempData(). which is a special store provided by the MVC which can store an object for some time and it looses its value when it is accessed for the first time.
simply add the client list to temp data TempData.Add("Client",Client) and then use it in action Index as TempData["Client"]

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Parv Sharma
  • 12,581
  • 4
  • 48
  • 80
  • But if I do it, it returns a search url.. and it doesn't exist – MuriloKunze Apr 19 '12 at 17:00
  • I tried to create two action Index(one with string parameter and another with no parameters) but it doesn't work: The current request for action 'Index' on controller type 'ClienteController' is ambiguous between the following action methods – MuriloKunze Apr 19 '12 at 17:11
0

Hi you should create a custom ModelBinder to pass custom types, as showed by this question: ASP.NET MVC controller actions with custom parameter conversion?

He then recommends a really good blog post: ASP.NET MVC controller actions with custom parameter conversion?

Hope this helps

Community
  • 1
  • 1
coffeeyesplease
  • 1,018
  • 9
  • 15