[英]Passing data from JavaScript code to the a Razor partial view within ASP.NET Core 3.1 MVC based application
Here are some details about our development environment:以下是有关我们开发环境的一些详细信息:
-DevExpress 20.1.7 ( we are using DevExtreme ) -DevExpress 20.1.7(我们使用的是 DevExtreme)
-Microsoft Visual Studio Enterprise 2019 (Version 16.4.6) -Microsoft Visual Studio Enterprise 2019(版本 16.4.6)
-ASP.NET Core 3.1.0 -ASP.NET 核心 3.1.0
I canNot do the following with JavaScript because the document javascript object is undefined inside the following razor code.我无法使用 JavaScript 执行以下操作,因为文档 javascript 对象在以下 razor 代码中未定义。
@await Html.PartialAsync("UpldPopupContentTmpltPartial", new ViewDataDictionary(ViewData) { { "BookId", document.getElementById("HiddenSelectedUploadDataType").Value } });
@await Html.PartialAsync("UpldPopupContentTmpltPartial", new ViewDataDictionary(ViewData) { { "BookId", document.getElementById("HiddenSelectedUploadDataType").Value } });
Could someone please give me a detailed explanation with sample code that will show me how I can pass the document.getElementById("HiddenSelectedUploadDataType").Value to the aforementioned partial view?有人可以用示例代码给我一个详细的解释,向我展示如何将 document.getElementById("HiddenSelectedUploadDataType").Value 传递给上述部分视图?
@yiyi-you Thank you for your detailed explanation within your answer. @yiyi-you 感谢您在回答中的详细解释。 However, I used the following line of code with @Html.Raw , but even though I'm practical if deadline dates for projects are really close, and I have to get stuff done, I still prefer the proper code implementation practices especially when it makes it easier for code reuse in the future and/or security and/or clarity:
但是,我将以下代码行与 @Html.Raw 一起使用,但是即使我很实用,如果项目的截止日期非常接近,并且我必须完成工作,但我仍然更喜欢正确的代码实现实践,尤其是当它使将来的代码重用和/或安全性和/或清晰度更容易:
@await Html.PartialAsync("UpldPopupContentTmpltPartial", new ViewDataDictionary(ViewData) { { "BookId", @Html.Raw("document.getElementById('HiddenSelectedUploadDataType').Value")} });
@await Html.PartialAsync("UpldPopupContentTmpltPartial", new ViewDataDictionary(ViewData) { { "BookId", @Html.Raw("document.getElementById('HiddenSelectedUploadDataType').Value")} });
Is the aforementioned code adhere to proper implementation practices?上述代码是否遵循正确的实现实践? If yes or no then please do explain.
如果是或否,请解释。 Appreciate feedback.
感谢反馈。 Thanks.
谢谢。
There are two solutions:有两种解决方案:
1.pass model data to Partial,here is a demo: 1.将模型数据传递给Partial,这里是一个演示:
TestPartial.cshtml: TestPartial.cshtml:
@model Model1
<input asp-for="HiddenSelectedUploadDataType" />
@await Html.PartialAsync("UpldPopupContentTmpltPartial", new ViewDataDictionary(ViewData) { { "BookId",Model.HiddenSelectedUploadDataType } })
Model1:型号1:
public class Model1
{
public string HiddenSelectedUploadDataType { get; set; }
}
UpldPopupContentTmpltPartial.cshtml: UpldPopupContentTmpltPartial.cshtml:
<div>@ViewData["BookId"]</div>
2.You can use ajax to pass document.getElementById("HiddenSelectedUploadDataType").Value
to action,and action return partial view to view,here is a demo: 2.可以使用ajax将
document.getElementById("HiddenSelectedUploadDataType").Value
传递给action,action返回局部视图查看,这里有一个demo:
TestPartial.cshtml: TestPartial.cshtml:
@model Model1
<input asp-for="HiddenSelectedUploadDataType" />
<div id="partial"></div>
@section scripts{
<script>
$(function () {
$.ajax({
url: '/TestSlick/GetPartial',
type: 'POST',
data: {
BookId: $("#HiddenSelectedUploadDataType").val()
},
success: function (data) {
$('#partial').html(data);
}
});
})
</script>
}
TestSlickController:
public IActionResult TestPartial() {
Model1 m = new Model1 { HiddenSelectedUploadDataType="sdsss"};
return View(m);
}
public IActionResult GetPartial(string BookId) {
ViewData["BookId"] = BookId;
return PartialView("UpldPopupContentTmpltPartial");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.