gpt4 book ai didi

c# - DNN MVC 模块 PartialView 返回整个页面

转载 作者:太空宇宙 更新时间:2023-11-03 12:06:40 25 4
gpt4 key购买 nike

我正在为 DNN 创建一个 MVC 模块,并尝试将表单作为局部 View 返回,以允许用户输入动态数量的对象,但是当我对返回 PartialView 和将其附加到页面,我收到了包含在我的 DNN 皮肤布局中的整个页面的 HTML。

我按照示例从 DNN 的 official samples repo 渲染部分 View 和 this general MVC guide .我已确保从我的 ActionResult 返回一个 PartialView,并在我的部分 View Razor 模板中将 Layout 设置为 null。

我的行动:

 public PartialViewResult BlankEducationForm()
{
return PartialView(new Education());
}

我的部分观点:

@inherits DotNetNuke.Web.Mvc.Framework.DnnWebViewPage<Chris.Modules.ResumeTest2.Models.Education>

@{
Layout = null;
}

@using System.Text.RegularExpressions
@using DotNetNuke.Web.Mvc.Helpers

<div class="education-editor">
<div class="form-row">
<div class="form-group col-md-6">
<label for="inputEmail4">School Name:</label>
@Html.TextBoxFor(m => m.SchoolName, new { @class = "form-control", @placeholder = "Degree" })
</div>
<div class="form-group col-md-6">
<label for="inputPassword4">Degree(s) Earned:</label>
@Html.TextBoxFor(m => m.Degree, new { @class = "form-control", @placeholder = "Degree" })
</div>
</div>
<div class="form-row">
<div class="form-group col-md-4">
<label for="inputEmail4">Start Year:</label>
@Html.TextBoxFor(m => m.StartYear, new { @class = "form-control", @placeholder = "Start Year" })
</div>
<div class="form-group col-md-4">
<label for="inputPassword4">End Year:</label>
@Html.TextBoxFor(m => m.EndYear, new { @class = "form-control", @placeholder = "End Year" })
</div>
<div class="form-group col-md-4">
<label for="inputPassword4">Display Order:</label>
@Html.TextBoxFor(m => m.Order, new { @class = "form-control", @placeholder = "Order" })
</div>
</div>


</div>

通过 AJAX 调用调用它的父 View :

@inherits DotNetNuke.Web.Mvc.Framework.DnnWebViewPage<Chris.Modules.ResumeTest2.Models.Resume>

@using System.Text.RegularExpressions
@using DotNetNuke.Web.Mvc.Helpers


<div id="Items-@Dnn.ModuleContext.ModuleId">
<div id="resume-container">
<form method="post" action="@Url.Action("Create", "Resume")">
<h1>General Information</h1>
<div class="form-group">
<label for="exampleInputPassword1">Resumé Name:</label>
@Html.TextBoxFor(m => m.ResumeName, new { @class = "form-control", @placeholder = "Resume Name" })
</div>

<div class="form-row">
<div class="form-group col-md-4">
<label for="inputEmail4">First Name:</label>
@Html.TextBoxFor(m => m.FirstName, new { @class = "form-control", @placeholder = "First Name" })
</div>
<div class="form-group col-md-4">
<label for="inputPassword4">Last Name:</label>
@Html.TextBoxFor(m => m.LastName, new { @class = "form-control", @placeholder = "Last Name" })
</div>
<div class="form-group col-md-4">
<label for="inputPassword4">Middle Name:</label>
@Html.TextBoxFor(m => m.MiddleName, new { @class = "form-control", @placeholder = "Middle Name" })
</div>
</div>

<div class="form-row">
<div class="form-group col-md-6">
<label for="inputEmail4">Address 1</label>
@Html.TextBoxFor(m => m.Address1, new { @class = "form-control", @placeholder = "Address 1" })
</div>
<div class="form-group col-md-6">
<label for="inputPassword4">Address 2</label>
@Html.TextBoxFor(m => m.Address2, new { @class = "form-control", @placeholder = "Address 2" })
</div>
</div>

<div class="form-row">
<div class="form-group col-md-4">
<label for="inputEmail4">City:</label>
@Html.TextBoxFor(m => m.City, new { @class = "form-control", @placeholder = "City" })
</div>
<div class="form-group col-md-4">
<label for="inputPassword4">State:</label>
@Html.TextBoxFor(m => m.State, new { @class = "form-control", @placeholder = "State" })
</div>
<div class="form-group col-md-4">
<label for="inputPassword4">ZIP:</label>
@Html.TextBoxFor(m => m.Zip, new { @class = "form-control", @placeholder = "ZIP" })
</div>
</div>

<div class="form-row">
<div class="form-group col-md-6">
<label for="inputEmail4">Phone Number:</label>
@Html.TextBoxFor(m => m.Phone, new { @class = "form-control", @placeholder = "Phone" })
</div>
<div class="form-group col-md-6">
<label for="inputPassword4">E-Mail:</label>
@Html.TextBoxFor(m => m.Email, new { @class = "form-control", @placeholder = "E-mail" })
</div>
</div>
<h1>Education</h1>
<div id="education-forms"></div>
@Html.ActionLink("Add another...", "BlankEducationForm", "Resume", null, new { id = "addItem" })
@Html.HiddenFor(m => m.ResumeId)
<button type="submit" class="dnnPrimaryAction">Create</button>
</form>
</div>
<script>
$('#addItem').click(function () {
$.ajax({
url: this.href,
cache: false,
success: function (html) {
$('#education-forms').append(html);
}
});
return false;
});
</script>
</div>

它确实从请求中插入了 HTML,其中包括表单,但出于某种原因,它认为我需要整个页面。我不确定 DNN 识别我的 Controller 时是否存在某种问题,但我没有理由期待会出现这样的问题。我真的很难过。

最佳答案

在您的 ajax 调用的成功函数中,而不是使用:

success: function (html) {
$('#education-forms').append(html);
}

请尝试以下操作:

success: function (html) {
$('#education-forms').html(html);
}

///更新以跟进评论

您的部分 View 的 .cshtml 是什么?例如,如果它被称为 _MyPartialView.cshtml,请尝试以下操作之一(不记得是否需要 .cshtml):

public PartialViewResult BlankEducationForm()
{
return PartialView("_MyPartialView", new Education());
return PartialView("_MyPartialView.cshtml", new Education());
}

关于c# - DNN MVC 模块 PartialView 返回整个页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54523802/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com