在MVC3 Razor视图中的局部视图引擎
问题描述:
我有MVC3 Razor视图引擎中的视图,如下图所示。现在我想确认连接动作输出显示在这个链接文本下不是新页面。我怎样才能完成这项工作?在MVC3 Razor视图中的局部视图引擎
请用示例代码解释。
我查看像这样:
@model ESimSol.BusinessObjects.COA_ChartsOfAccount
@{
ViewBag.Title = "Dynamic Account Head Configure";
}
<h2>Dynamic Account Head Configure</h2>
<table border="0">
<tr>
<td> Select an Server Connection </td>
<td style="width:5px">:</td>
<td>@Html.DropDownListFor(m => m.DBConnections, Model.DBConnections.Select(x => new SelectListItem() { Text = x.ConnectionName, Value = x.DBConnectionID.ToString()}))</td>
</tr>
<tr>
<td> </td>
<td style="width:5px"></td>
<td>@Html.ActionLink("Confirm Connection", "ConformConnection")</td>
</tr>
</table>
和我的控制器动作像如下:
public ActionResult ConfirmConnection()
{
return PartialView();
}
答
首先移动您的标记的局部视图。之后定义一个呈现你的局部视图的动作方法。
[ChildActionOnly]
public ActionResult ConfirmConnection(COA_ChartsOfAccount model)
{
return PartialView("MyPartialView", model);
}
ChildActionOnly属性确保此操作方法不能被HTTP请求调用。
然后,您可以使用Html.Action方法随时显示它。
@Html.Action("ConfirmConnection", "MyController", new { model = Model })
忽略将模型作为参数传递,如果它不通过显示它的页面进行更改。您可以在您的操作方法中检索它。
答
我使用jQuery和Ajax对于这种事情的大风扇...... http://api.jquery.com/jQuery.ajax/
如果你遵循了典型的MVC模式,那么你可以添加一个动作链接使用类似的页面...
@Html.ActionLink("controller", "action", args);
,但我会去为Ajax驱动的方法...
<script type="text/javascript">
var ajaxBaseUrl = '@Url.Action("yourController", "ConformConnection", new { args })';
$(link).click(function() {
var currentElement = $(this);
$.ajax({
url: ajaxBaseUrl,
data: { any other queryString stuff u want to pass },
type: 'POST',
success: function (data) {
// action to take when the ajax call comes back
}
});
});
});
</script>