【问题标题】:Get a value from a class in script从脚本中的类中获取值
【发布时间】:2021-10-17 12:21:18
【问题描述】:

如何调用操作并在 javascript 中导入值? 我有一个 c# 程序,需要在 javascript 的一部分中获取一个字符串值。

<script type="text/javascript">          
$('#Country').change(function () { var selectedCountry = 
$("#Country").val();
var regionsSelect =  --->i need get a value by call from a class here <------

谢谢。

【问题讨论】:

  • 程序和 Javascript 不会交互,除非“程序”是 Web 浏览器 - 那些应该只是运行 JS,而不考虑它。缺少重要细节。

标签: javascript c# model-view-controller


【解决方案1】:

您可以使用 ajax 来调用控制器中的方法(如果您使用例如 asp.net / core)。

例如:

    [HttpPost]
    public JsonResult AjaxMethod(string selectedCountry)
    {
        var region = getRegion(selectedCountry);
        return Json(region);
    }
$(function () {
            $('#Country').change(function () {
                $.ajax({
                    type: "POST",
                    url: "/ControllerName/AjaxMethod",
                    data: { "selectedCountry": $("#Country").val() },
                    success: function (response) {
                        var regionSelected = response.###
                    },
                    failure: function (response) {
                        // console.log(reponse)
                    },
                    error: function (response) {
                        // console.log(reponse)
                   }
                });
            });
        });

希望为您提供解决方案。 :)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-14
    • 2013-06-15
    • 2020-05-02
    • 1970-01-01
    • 2015-10-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多