简体   繁体   English

将数据从 JavaScript 代码传递到基于 ASP.NET Core 3.1 MVC 的应用程序中的 Razor 分部视图

[英]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>

result:结果: 在此处输入图片说明

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");
        }

result:结果: 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 asp.net core mvc 中将数据从 Partial View 传递到其父 View - Passing data from Partial View to its parent View in asp.net core mvc 从ASP.NET MVC Partial View中调用JavaScript - Call JavaScript from within an ASP.NET MVC Partial View asp.net mvc将使用$ .get()呈现的部分视图中的任何数据传递到ContentPage - asp.net mvc passing any data from a partial view that is rendered with $.get() to ContentPage 从ASP.NET MVC3中的局部视图加载Javascript - Load Javascript from a Partial View in ASP.NET MVC3 将 javascript 数组从视图传递到 ASP.Net MVC 5 中的控制器 - Passing javascript array from view to controller in ASP.Net MVC 5 调用 JavaScript function 从 Z1848E732214CCE460F9B24FE0C28 视图中的 C# 代码 - Call a JavaScript function from C# code in asp.net core 5 MVC view ASP.Net MVC 将数据从脚本传递到新视图 - ASP.Net MVC passing data from script to new view ReferenceError: 'x' 未定义 - asp.net mvc 部分视图和 jQuery/JavaScript - 将整个 model 传递给操作方法 - ReferenceError: 'x' is not defined - asp.net mvc partial view and jQuery/JavaScript - passing the entire model to an action method 在ASP.NET MVC应用程序内的iframe中呈现部分视图 - render partial view inside an iframe within asp.net mvc application ASP.net 核心 MVC 部分视图未卸载 JS - ASP.net core MVC Partial view not unloading JS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM