我有一个带有 4 个文本框的 Razor 页面和一个按钮,用于在其他框下方创建一个附加文本框。
当用户点击按钮时,一个 javascript 函数 addNewSection() 被触发,新的文本部分被插入到页面中。
function addNewSection() {
var section = document.getElementById("lastTextBox");
var newSection = `<div> razor page content here. </div>`
section.insertAdjacentHTML('afterend', newSection);
}
当我直接将 razor 页面内容写入 var newSection 时,此功能有效。然而,实际的剃须刀页面内容有点长。有没有更简洁的方法来做到这一点?
我尝试将页面内容放入新的剃刀页面“Section.cshtml”并将 var newSection 设置为等于该页面(见下文)。但我收到错误“CS1963:表达式树可能不包含动态操作”。知道我怎么能做类似的事情吗?我宁愿不将整个 cshtml 页面放入我的 js 函数中。谢谢!
var newSection = @RenderPage("~/Views/Section.cshtml);
尝试:
var newSection = "@(await Html.PartialAsync("~/Views/Section.cshtml"))";