我有一个 View Index.cshtml
:
@{
ViewBag.Title = "Index";
}
<h2>Add</h2>
<p>Begin building a question set by clicking the button below.</p>
<input type="button" value="Create" onclick="location.href='@Url.Action("Product", "Add")'" />
在我的 AddController 中调用 Product 操作:
public ActionResult Product()
{
ViewBag.Partial = true;
return PartialView("Product");
}
我想要实现的是:
当用户加载页面时,我的部分 View (Product.cshtml
) 中的内容被隐藏,但是当他们单击调用 Product
的“创建”按钮时在我的 AddController
中执行操作 然后我想将 Product.cshtml
部分 View 加载到我的 Index.cshtml
中,以便后者仍然具有其原始内容,但这次是将 Product.cshtml
注入(inject)其中。
目前,它只返回部分 View ,而不是两个 View 。
这在 MVC 中可以实现吗?
最佳答案
是的,基本上你需要的是得到你的PartialView
通过 AJAX 调用并将其加载到您的页面中。使用 jQuery 最简单
您可以使用简单的 JavaScript 来完成。
在按钮中 onclick
输入加载函数的名称,例如 onclick="partialLoad()"
放div
与一些id
在您的页面上,例如 <div id="loadFrame"></div>
然后你的脚本:
function parialLoad() {
$("#loadFrame").load("/Product");
}
关于c# - 返回 View 内的部分 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51885006/