简体   繁体   English

我无法在 C# 中使用 Jquery 和 Ajax 从控制器向 Razor 视图发送值

[英]I can't send a value to a Razor View from controller using Jquery and Ajax in C#

What I'm trying to do is to return an object from the controller and read all its values one by one in a razor view.我想要做的是从控制器返回一个对象并在剃刀视图中一个一个地读取它的所有值。 The value is going well from view to controller;该值从视图到控制器进展顺利; the issue is when I try to return it from the controller to view.问题是当我尝试从控制器返回它进行查看时。 I'm using ASP.NET, Razor Views (HTML5), AJAX and JQuery.我正在使用 ASP.NET、Razor 视图 (HTML5)、AJAX 和 JQuery。 Please, let me know if you need further information about this.如果您需要有关此的更多信息,请告诉我。

These are my three controls in the view:这些是我在视图中的三个控件:

        <div class="form-group">
            @Html.LabelFor(model => model.IdProducto, "IdProducto", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("IdProducto", null, htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.IdProducto, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.CantidadProducto, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.CantidadProducto, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.CantidadProducto, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.ValorTotal, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.ValorTotal, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.ValorTotal, "", new { @class = "text-danger" })
            </div>
        </div>

This is my JQuery statement:这是我的 JQuery 语句:

    <script type="text/javascript">
        $(function () {
            $('#IdProducto, #CantidadProducto').on('change', function () {
                var IdProducto = $('#IdProducto').val();
                var CantidadProducto = $('#CantidadProducto').val();

                if (CantidadProducto.length > 0) {
                    $.getJSON('@Url.Action("GetProductValue")', { idProducto: IdProducto }, function (data) {
                        $.each(data, function (i, item) {
                            $('#ValorTotal').val(( Number(CantidadProducto) * Number(item.Precio) ));
                        });
                    });
                }
                else {
                    $('#ValorTotal').val(0);
                };
            });
        });
    </script>

And, this is my controller code:而且,这是我的控制器代码:

[HttpPost]
        public JsonResult GetProductValue(string idProducto)
        {
            Producto producto = db.Producto.Find(Convert.ToInt32(idProducto));

            if (producto == null)
            {
                producto = new Producto();
            }

            return Json(producto);
        }

$.getJSON fetchs data using http GET request while in your controller your action method is POST $.getJSON使用 http GET请求获取数据,而在您的控制器中,您的操作方法是POST

You can change it to HttpGet or use $.post method您可以将其更改为HttpGet或使用$.post方法

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

相关问题 c#控制器中来自razor的selectList的值 - value of selectList from razor in c# Controller 使用Ajax将int从视图传输到C#中的控制器 - Transferring an int from view to controller in C# using Ajax C# Leaflet 如何使用 ajax 将数据从视图正确发布到控制器? - C# Leaflet how do i properly post data from view to controller using ajax? 如何从 JQuery Ajax 向后端 C# 发送文件和字符串? - How can I send a file and a string to my backend C# from JQuery Ajax? C# Ajax 无法从控制器响应中获取数据 - C# Ajax can't get data from controller response 使用C#/ Razor MVC将数据从表传递到控制器 - Pass Data to Controller from Table in View with C#/Razor MVC 显示列表从 controller 到 Razor 查看 C# MVC 5 - Show List From controller to Razor View C# MVC 5 C#MVC 4 RAZOR-使用@foreach将JSON从控制器返回列表到视图 - C# MVC 4 RAZOR - JSON return List from Controller to View using @foreach 我无法从 AJAX 中的 C# controller 发布请求中获取数据 - I can't get data from AJAX post request in C# controller 尝试使用ajax调用将值从前端javascript发送到后端c#,但值在后端变为NULL,我不知道我哪里出错了 - trying to send value from front end javascript to backend c# using ajax call,but the value is getting NULL in backend,i don't know where i did mistake
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM