Asp.Net MVC 4: ¿Podemos configurar Layout = null, cuándo cargar datos Html desde ajax Call?

I am adding another view page (with Ajax Request) into my existing view Page, like in this way -> $("divId").html(data); Here data is my View Page which I am getting with ajax call. My question is Can I remove 'Layout' or set it as Layout = Null, for this Ajax returned View page?

preguntado el 31 de julio de 12 a las 09:07

1 Respuestas

In controller action you can return a PartialView, this will only send the HTML from the view to the client without the code from the Layout. You may implement your Action this way:

public ActionResult MyAction()
    if (Request.IsAjaxRequest()) 
         return PartialView();
    return View();

So you can use the same Action for a normal and an Ajax request.

Respondido 31 Jul 12, 10:07

A bit confusion, please clarify it- Does partialView() function automatically omits/leave the Layout contents or I need to create a new view as a partial itself to get the desired output? I am asking because, after return PartialView("myView") I am still getting the same previous output. - himanshupareek66

PartialView only returns the HTML from the view, without using the Layout. You shouldn't get the same output for an ajax request. Make a Breakpoint to see, if your action is going thru the if clause and returning the partial view. - DanielB

Yes, with Break point I am seeing that it is going into if block (IsAjaxRequest), and returning Partial view, but still getting the view with Layout. Need to clear one thing only, can a View(with inheriting any Layout), may be used as a partial view too? if No, then I need to create a new Partial view. - himanshupareek66

PartialView should not render the layout. Can you post your View? I define Layout in _ViewStart.cshtml and not within the view. - DanielB

Actually this was the answer of my question, that We can not use a view for both "View" and "partialView" ( as you said - PartialView should not render the layout.) Thanks for your valuable time. Now I am with actual approach. - himanshupareek66

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.