0

There is a _Layout page and partial view rendered into this _Layout page via AJAX call as shown below:

enter image description here

Controller:

public ActionResult Index()
{
    ViewBag.PageTitle = "Issues";
    return PartialView("_Index");
}

On the other hand, there is a title in the _Layout page and I want to update the title with the ViewBag data returned from Controller. But as AJAX call only updates the Page Content, the Page Title cannot be updated as the _Layout page had already been rendered before calling partialview. So, in this scene what is the most suitable approach to update the Page Title? I think I can update it on AJAX success, but if it is possible I would like to update it by just using ViewBag without an extra property or an extra partialview. Any idea?

Jack
  • 1
  • 21
  • 118
  • 236
  • 1
    The value of a `ViewBag` property will not be passed to the success callback of your ajax call - only the rendered html that was generated on the server. You could always create a title element in the partial with its value and use jquery to move it once the partial has been added. –  Oct 28 '16 at 06:44
  • @StephenMuecke Thanks a lot. In that case do I need to change my Controller method? In addition to this should I call the new partialview (title) at the same time when calling the first one? Or on success of the first one? Could you please give an sample code? – Jack Oct 28 '16 at 06:54
  • 1
    I don't know what your controller code is :) Or what your existing html in the 'title' section is or what you want to update (but yes, you could make a second ajax call in the success callback of the first to get another partial, but it seems unnecessary –  Oct 28 '16 at 06:57
  • I posted the Controller code and in the Layout page I just want to update **

    ViewBag.PageTitle

    ** field. On the other hand, what is unnecessary? I could not understand. Could you please explain a little bit more? Thanks in advance...
    – Jack Oct 28 '16 at 07:03
  • 1
    In your partial view include `

    ViewBag.PageTitle

    ` and then in the success call back you can use jquery to move it to the position in the layout after the partial has been added to the DOM (along the lines of [this answer](http://stackoverflow.com/questions/1279957/how-to-move-an-element-into-another-element))
    –  Oct 28 '16 at 07:07
  • @StephenMuecke As I use the title only this Layout page, is it good idea to change it content by using **innerHtml** method on AJAX success? For example if I render List partialView of Student I display "Student List" and if I render Issue List partial view I display "Issue List". Is it good idea? – Jack Oct 28 '16 at 20:05
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/126936/discussion-between-stephen-muecke-and-clint-eastwood). –  Oct 28 '16 at 21:18
  • @StephenMuecke Many thanks for your reply. Could you please clarify me about my last message on the chat? >>> – Jack Nov 14 '16 at 07:37
  • >>> Many thanks for your helps. Sorry, I have been busy and not had time to reply. The approach is ok, but I am not sure if I explained what I want to do and if the there is a better approach than it. Could you please clarify me about what is the bet approach for the following situation? As shown on the image of my question on http://stackoverflow.com/questions/40299448/passing-viewbag-data-to-layout-page-on-ajax-success-in-asp-net-mvc?noredirect=1#comment67883838_40299448 – Jack Nov 14 '16 at 07:37
  • >>> I have a _Layout page in my MVC5 project and there is two div for the dynamic contents: Header and Page Content. I render the page content by using partialview without any problem. On the other hand, of course I can use page title for each partialview instead of _Layout page, BUT there is a setting div in the Header div and I do not want to repeat this div for each view. So, I should render (display) the titles for _Layout page instead of partialview. – Jack Nov 14 '16 at 07:38
  • >>> For this reason first I want to return ViewBag from the Controller but cannot display as I use AJAX for rendering partialview. After that I decided to display the title on the success of AJAX call by setting the title dynamically using the approach as in your last comment (I pass the title parameter to the AJAX call and use it on the success). Is it clear now? I so, could you please inform me what is the best approach to do this? Is the code last comment is the best? Or s there a better one? – Jack Nov 14 '16 at 07:38

0 Answers0