Answer the question
In order to leave comments, you need to log in
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>
public void CreateFeedBack(string note)
{
ViewBag.Note = note;
}
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@ViewBag.Note
Answer the question
In order to leave comments, you need to log in
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:
public ActionResult Index()
{
// здесь что-то явно происходит...
ViewBag.SomeVar = "some value";
return View(); // <--- явно возвращает конкретное представление
}
@{
Layout = null;
}
<h2>Index</h2>
@ViewBag.SomeVar
public ActionResult CreateFeedBack(string note)
{
ViewBag.Note = note;
return View();
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question