H
H
helloitsme2014-05-18 14:43:06
JavaScript
helloitsme, 2014-05-18 14:43:06

Why is the ViewBag value lost?

I send data to the controller via ajax.

<script>
        $.feedback({
            ajaxURL: '@Url.Action("CreateFeedBack", "Email")',
            html2canvasURL: 'js/html2canvas.js'
        });
    </script>


They come here:
public void CreateFeedBack(string note)
        {
            ViewBag.Note = note;

        }

This information needs to be displayed on the page.
Here is the view:
@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

@ViewBag.Note

But it's empty here. How to be?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Valery Abakumov, 2014-08-08
@manisha

Good afternoon!
@manisha I have to make 2 remarks:
1. The CreateFeedBack method has an input parameter called note , which means that the method must take some value for this parameter. If it does not take any value, then the variable will receive the default value. Because If your note variable is a string , it will default to null.
You form the URL as follows: As a result, the value '/Email/CreateFeedBack' will be written to your ajaxURL variable, but without the request parameter " note
", so the server will write null to the CreateFeedBack action method in the note variable . In order to correctly pass the variable to the request method, the Url.Action method has an overloaded method that accepts an object with request parameters:
If you use it, then the code is:
the value '/Email/ CreateFeedBack ?note=my+some+note' (or something like that) will already be written to the ajaxURL variable, and when you send a request to this URL in the CreateFeedBack action method, your string will be written to the note variable : "my some note ".
2. As @MIsternik rightly said ,

ViewBag is used when generating a page on the server
, i.e. this means the following:
ViewBag is formed when your request comes to an action method, this action method does something and returns a specific view , like this:
public ActionResult Index()
{
      // здесь что-то явно происходит...
      ViewBag.SomeVar = "some value";
      return View(); // <--- явно возвращает конкретное представление
}

And in the view itself:
@{
    Layout = null;
}
<h2>Index</h2>

@ViewBag.SomeVar

the ViewBag will have a SomeVar property with the value "some value".
Your action method does not return anything , so it will not form a view .
You need to do this:
public ActionResult CreateFeedBack(string note)
{
      ViewBag.Note = note;
      return View();
}

In this case, the value from the incoming request parameter will be written to the note variable , which will be written to the Note property of the dynamic ViewBag object, generate a view that will use ViewBag.Note, and return a response to the client. The response will be a normal html code.
Please note that because You are using an ajax request to an action method, then that action method must return a partial view - i.e. a view that has Layout = null; , otherwise, in response to an ajax request, you will receive a full html code along with DOCTYPE, html, head, body tags and others. If you explicitly specify Layout = null; , then in the answer you will receivejust the code from the view and nothing else (no layers/master pages).
As they say, take the time and learn the materiel - read books on ASP.NET MVC, because the question posed belongs to the category of questions of the basics of ASP.NET MVC, its foundation. Believe me, after reading at least 1 book from cover to cover, a lot of questions will be removed from you, and a complete picture of how to work with ASP.NET MVC will form in your head.
I hope I helped you a little.
Good luck!

M
MIsternik, 2014-06-13
@MIsternik

I can be wrong, but ViewBag is used when generating a page on the server, and when an ajax request is made, the page is no longer formed. Why not get the result from CreateFeedBack via ajax and jsom to already change the data on the page.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question