Using ViewData to pass data from Controller to View
Asked Answered
C

4

5

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 ?

Cullen answered 15/5, 2015 at 12:45 Comment(2)
TempData is not ViewData! Show how you have tried to access the ViewData propertyFurlough
Sorry about my mistake, i've tried it correctly anyway and it don't work.Cullen
S
8

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>
}
Seth answered 15/5, 2015 at 12:55 Comment(1)
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.Cullen
B
2

You can use ViewBag for this

Controller :

ViewBag.name= "bob";

View :

 <p>Bonjour @ViewBag.name</p>
Beardsley answered 15/5, 2015 at 13:3 Comment(9)
Using .__ instead of ["__"] occurs an error in the Controller code.Cullen
that is because you are using the keyword "try".Beardsley
Please use ViewBag instead of ViewDataBeardsley
I think you are getting confused between ViewBag and ViewData, please make sure you are using the viewBag in controller as well as in viewBeardsley
My mistake, i juste copy paste a wrong line. But it do not works... I've already use ViewBags/Datas, i do not understand why it's not working...Cullen
can you please share the exact code you are writing now?Beardsley
I don't confuse, i'm using the same View"thing" in View and in Controller.Cullen
If it is going in the if condition. then it must work.Beardsley
I do not understand why but it don't !Cullen
P
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?

Provender answered 15/5, 2015 at 12:49 Comment(5)
Excuse me i did it wrong in the example, anyway it don't work if ViewData is in Controller and View.Cullen
Are you calling the ViewData inside the View or LayoutPage?Provender
I do not really get your question, I'm calling the ViewData, on the Html (body) part of the View.Cullen
Are you sure that your Action method is not returning before attribution of ViewData? If possible, post the full code of your actionProvender
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?Provender
D
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.

Doublure answered 15/5, 2015 at 13:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.