c# - 返回 View 内的部分 View

标签 c# asp.net-mvc

我有一个 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/

相关文章:

c# - 类型 Microsoft.OData.Edm.Date 和 System.Nullable System.DateTimeOffset 之间没有定义强制运算符

html - 导航栏中的 <div> 未正确对齐

c# - 为什么 Microsoft 语音识别系统会匹配语法,即使没有说出该语法的单词?

asp.net-mvc - 从 Html.RouteLink 生成绝对 url

c# - MVC Controller 需要空构造函数,但未使用接口(interface)并抛出错误

javascript - MVC Controller 在页面加载时被击中两次

c# - 无法替换文本文件中的字符串

c# - 是否可以在运行时切换 app.config?

c# - 如何使委托(delegate)线程成为STA

C# 读取、修改然后将二进制数据写入文件。最佳 session ?