locked
partial page update without using partial views in ASP.NET MVC3 RRS feed

  • Question

  • User-502541630 posted

    I would like to know if it is possible to update a section of the view asynchronously without moving that section into a partial view, in ASP.NET MVC 3.0. In webforms model, I can have all the mark-up in just one page, but I can still update a section of the page by specifying the target div on the update panel without moving that section into separate user control. partial view is only useful if that HTML fragment is used in multiple views. creating a partial view just for the purpose of asynchronous update will result in HTML for the page scattered in multiple files. I am using Ajax.BeginForm() to make an ajax call to the server. related article for this http://blogs.msdn.com/b/stuartleeks/archive/2011/04/13/asp-net-mvc-partial-rendering-and-ajaxattribute.aspx

    Wednesday, February 1, 2012 12:19 PM

Answers

  • User1477435862 posted

    Have a look at this tutorial.

    With respect to how you would render it in the view in the first place, all you would need to do is:

    @Html.Action("MyAction")

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 2, 2012 1:08 PM

All replies

  • User1477435862 posted

    The simple answer to your question is no, it isn't possible.  I understand your concern about the HTML for the page being 'scattered' in multiple files but this happens anyway, with layout pages, common HTML fragments, etc, so what's the problem?  Also, using partial views means that you'll get it working. Laughing

    Wednesday, February 1, 2012 1:37 PM
  • User1173667893 posted

    It's possible, but sloppy.  You could load the entire page into a hidden div, then parse that div for the updated elements and then replace those elements on your page with the updated ones.

    I've done something like this to group multiple requests into a single request.  It's not a very good approach for code clarity though.

    Wednesday, February 1, 2012 1:47 PM
  • User1477435862 posted

    Given the choice of 'sloppy' partials or 'sloppy' entire page with loads of extra work, I'd go with partials. ;)

    That said, another approach (and, IMO, a more appropriate approach) would be to use an Action instead of Partial.  it would mean that you don't have to do the extra stuff that Stuart outlined in his blog.

    Wednesday, February 1, 2012 1:58 PM
  • User197322208 posted

    I would like to know if it is possible to update a section of the view asynchronously without moving that section into a partial view,

    Yes. Call with javascript an ACTION . See

    http://bit.ly/mvc_ajax_jquery

    how I add a new row to table.

    Wednesday, February 1, 2012 3:32 PM
  • User-502541630 posted

    Given the choice of 'sloppy' partials or 'sloppy' entire page with loads of extra work, I'd go with partials. ;)

    That said, another approach (and, IMO, a more appropriate approach) would be to use an Action instead of Partial.  it would mean that you don't have to do the extra stuff that Stuart outlined in his blog.

    could you provide more details, how I can use an action method for partial updates (triggered by user action)

    Thursday, February 2, 2012 12:47 PM
  • User1477435862 posted

    Have a look at this tutorial.

    With respect to how you would render it in the view in the first place, all you would need to do is:

    @Html.Action("MyAction")

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 2, 2012 1:08 PM
  • User-683735340 posted

    Hi,

    It isn't possible.

    Friday, February 3, 2012 2:32 AM
  • User1477435862 posted

    Hi,

    It isn't possible.

    What isn't possible?

    Friday, February 3, 2012 3:12 AM
  • User367642263 posted
    Hi What exactly do you want to do in this section? To replace the part of the page you can update HTML of specified element, using JavaScript. If it is a div - just do: " var _html='<p>test</p>' ; $('your div selector').html(_html)". How you going to populate the _html up to you - get from sever or build dynamically or copy from hidden section....
    Friday, February 3, 2012 5:07 AM
  • User-502541630 posted

    Thanks for all the replies

    Monday, February 6, 2012 4:41 AM