Question

Is it possible to use @Html.RenderPartial inside @using(Ajax.BeginForm) code block? It doesn't work if I use @Html.RenderPartial, and it works if I put whole of razor code directly inside @using(Ajax.BeginForm) block.

Could anyone please suggest if that's possible or not?

Was it helpful?

Solution

If the Html.RenderPartial is right after the @using(Ajax.BeginForm(...)) then you don't need to prefix the call to Html.RenderPartial with @ as razor understands you are still in the same code block.

So, this works fine:

@using (Ajax.BeginForm(...)) {            
    Html.RenderPartial("_PartialView", Model);
    <p>after partial view inside the ajax form</p>
}

If between the `@using(Ajax.BeginForm(...)) and the render partial you have some html, then razor will end the code block before you call RenderPartial and you will need to do this:

@using (Ajax.BeginForm(new AjaxOptions())) {            
    <div>
        <h3>partial view inside the ajax form</h3>
        @{ Html.RenderPartial("_PartialView", Model); }
    </div>
}

Also notice that Html.RenderPartial is a method that returns void and internally calls Write, so the syntax for using it is slightly different than when you use Html.Partial, that's why you need to surround it with "@{" (when not in a code block already) and end it with semicolon. See this question

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top