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 ?

2
  • TempData is not ViewData! Show how you have tried to access the ViewData property Commented May 15, 2015 at 12:48
  • Sorry about my mistake, i've tried it correctly anyway and it don't work. Commented May 15, 2015 at 12:56

4 Answers 4

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>
}
Sign up to request clarification or add additional context in comments.

1 Comment

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.
2

You can use ViewBag for this

Controller :

ViewBag.name= "bob";

View :

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

9 Comments

Using .__ instead of ["__"] occurs an error in the Controller code.
that is because you are using the keyword "try".
Please use ViewBag instead of ViewData
I think you are getting confused between ViewBag and ViewData, please make sure you are using the viewBag in controller as well as in view
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...
|
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?

5 Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.