是否可以在@using(Ajax.BeginForm)代码块中使用@Html.RenderPartial? 如果我使用 @Html.RenderPartial,它不起作用,如果我将整个 razor 代码直接放在 @using(Ajax.BeginForm) block 中,它就起作用。
有人可以建议一下这是否可能吗?
最佳答案
如果 Html.RenderPartial
就在 @using(Ajax.BeginForm(...))
之后,那么您不需要在对 Html 的调用之前添加前缀.RenderPartial 与 @
作为 razor 理解您仍然在同一个代码块中。
所以,这工作正常:
@using (Ajax.BeginForm(...)) {
Html.RenderPartial("_PartialView", Model);
<p>after partial view inside the ajax form</p>
}
如果在 `@using(Ajax.BeginForm(...)) 和渲染部分之间有一些 html,那么 razor 将在调用 RenderPartial 之前结束代码块,您需要执行以下操作:
@using (Ajax.BeginForm(new AjaxOptions())) {
<div>
<h3>partial view inside the ajax form</h3>
@{ Html.RenderPartial("_PartialView", Model); }
</div>
}
另请注意,Html.RenderPartial
是一个返回 void 并在内部调用 Write 的方法,因此使用它的语法与使用 Html.Partial
时略有不同,这就是为什么您需要用“@{”将其括起来(当不在代码块中时)并以分号结束。请参阅this question
关于asp.net-mvc-4 - 如何在 Ajax.Beginform 中使用 @Html.RenderPartial?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19270211/