4

I'm trying to pass data from a controller to view, and display these data. But after many tries I can't.

Controller code :

public ActionResult ValidSearch(ProductSearchParam gp)
{

    if (gp.IsCandidate)
    {
        ViewBag.abc="bob";
        List<ProductCandidate> prodClist = new List<ProductCandidate>();
        prodClist = Pcs.SearchParam(gp,0,100);
        ViewBag.total = prodClist.Count;
        return View("ValidSearchCandidate", prodClist);
    }
    else
    {
        List<Product> prodlist = new List<Product>();
        prodlist = Ps.SearchParam(gp,0,100);
        ViewBag.total = prodlist.Count;
        return View("ValidSearch", prodlist);
    }

    //return View();
}

View code :

<body>
    <div>

        <p>Bonjour @ViewBag.abc</p>

I've even try TempData or ViewBag, unsuccessfully.

I do not understand why the value is not getting passed to the View correctly Can someone help me to solve this ?

Juan Salvador Portugal
  • 1,233
  • 4
  • 20
  • 38
Antoine Ravier
  • 63
  • 1
  • 1
  • 9

4 Answers4

7

Since you have set some property on the ViewData on the Controller, you will have it available on the ViewData on View. On the other hand, you have the TempData which is used when you need to transfer information between requests to another route (redirects to an action). For sample:

public ActionResult ValidSearch(ProductSearchParam gp)
{
   List<ProductCandidate> prodClist = new List<ProductCandidate>();

   ViewData["Nom"] = "bob";

   return View("ValidSearchCandidate", prodClist);
}

And in the view:

@ViewData["Nom"].ToString()

@foreach(var prod in  Model)
{
   <li>@prod.Name</li>
}
Felipe Oriani
  • 37,948
  • 19
  • 131
  • 194
  • Thank you for your response, i've tried your View code and ProductCandidate is a class i made, so it's "unfindable" in the View. – Antoine Ravier May 15 '15 at 13:04
2

You can use ViewBag for this

Controller :

ViewBag.name= "bob";

View :

 <p>Bonjour @ViewBag.name</p>
Aman Khanna
  • 137
  • 1
  • 10
1

In your example you've used ViewData in Controller and TempData in View

If you use ViewData["Nom"] in controller, you should use ViewData["Nom"] in view. Are you use that you are not misspelling the object?

Fabio
  • 11,892
  • 1
  • 25
  • 41
  • Excuse me i did it wrong in the example, anyway it don't work if ViewData is in Controller and View. – Antoine Ravier May 15 '15 at 12:55
  • Are you calling the ViewData inside the View or LayoutPage? – Fabio May 15 '15 at 12:57
  • I do not really get your question, I'm calling the ViewData, on the Html (body) part of the View. – Antoine Ravier May 15 '15 at 13:00
  • Are you sure that your Action method is not returning before attribution of ViewData? If possible, post the full code of your action – Fabio May 15 '15 at 13:05
  • As I can see, there are 2 views: ValidSearch and ValidSearchCandidate. The ViewData["try"] exists only when ValidSearchCandidate is the chosen one. Are you that you are putting the @ViewData["try"] inside the correct view? – Fabio May 15 '15 at 13:17
1

Replace ViewBag["try"] to ViewData["try"] in your controller code.

And, In the else condition you have no any declaration of ViewData["try"] so may be you have to check the ViewData before using it on your view.

Sunil Shrestha
  • 303
  • 1
  • 7