简体   繁体   English

我们如何使用Jquery和ASP.NET MVC 4复杂模型数据设置HTML元素的值

[英]How do we set the value of an HTML element using Jquery and ASP.NET MVC 4 complex model data

In one of my ASP.NET MVC 4 view, I am using a Model's data to set the values of different HTML elements. 在我的ASP.NET MVC 4视图之一中,我正在使用模型的数据来设置不同HTML元素的值。 I can use this model in the view to display the values such as: 我可以在视图中使用此模型来显示诸如以下的值:

<div>@Model.Category.Name</div> etc...

But one of the div tag <div id="DivTotalCost"></div> needs to display the total cost of all the products in all the categories. 但是div标签<div id="DivTotalCost"></div>需要显示所有类别中所有产品的总成本。 So at the end of the view I have the following script code to set the value of this DivTotalCost tag. 因此,在视图末尾,我具有以下脚本代码来设置此DivTotalCost标记的值。 But the following code does not set this value. 但是以下代码未设置此值。 I have put an alert statement to test the value and the alert displays: 我放置了一个警报语句来测试该值,并且警报显示:

function(){
  return total;
}

View: 视图:

@model MyMVC_app.Models.ProductDetails

<div>@Model.Category.Name</div> etc...
<div id="DivTotalCost"></div>

@section scripts{
    @Scripts.Render("~/bundles/jqueryval")
    @Scripts.Render("~/bundles/jqueryui")
    <script type="text/javascript">
        $(document).ready(function () {
            var TotalCost = function () {
                @if(Model.ProductCategoryList != null)
                {
                    var total = "";

                    foreach(var item in Model.ProductCategoryList)
                    {
                        foreach(var product in @item.products)
                        {
                            total += product.price;
                        }
                    }
                }
                return total;
            }
            alert(TotalCost);
            $('#DivTotalCost').text(TotalCost);
        });
    </script>
}

Please help. 请帮忙。

Thanks..Nam 谢谢..Nam

First: The alert displays 第一:警报显示

function(){
  return total;
}

because you're passing the function itself, no its result. 因为您要传递函数本身,没有结果。 Your alert (and the div text assignment)should use the function result, like this 您的警报(和div文本分配)应使用函数结果,如下所示

alert(TotalCost());

now, inside the function you're mixing razor and javascript, and both are different things and run in a different time each, so you should adjust your code like this (not tested, but you might get the idea) 现在,在函数内部您将razor和javascript混合在一起,两者都是不同的东西,并且分别在不同的时间运行,因此您应该像这样调整代码(未经测试,但您可能会明白)

$(document).ready(function () {
var TotalCost = function () {
               @{
                var total = 0;
                if(Model.ProductCategoryList != null)
                {   
                    foreach(var item in Model.ProductCategoryList)
                    {
                        foreach(var product in @item.products)
                        {
                            total += product.price;
                        }
                    }
                }
              }//end of razor block
                return @total;
            }
        });

You can also create a ViewModel and add that logic on it, just in case you're open to try a different approach 您还可以创建一个ViewModel并在其上添加该逻辑,以防万一您愿意尝试其他方法

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

相关问题 将jQuery数据表与ASP.NET MVC一起使用时如何更新模型数据 - How to update model data when using jQuery Data table with ASP.NET MVC 如何使用 asp.net mvc 中的模型声明 jquery 对象? - How do I declare a jquery object using the model in asp.net mvc? 使用GET的ASP.Net MVC模型绑定复杂对象 - ASP.Net MVC Model Binding Complex Object using GET 如何在 ASP.NET MVC 视图中使用 JQuery 将 @HTML.EditorFor 设置为只读 - How to set @HTML.EditorFor to readonly using JQuery in an ASP.NET MVC View ASP.NET MVC / Jquery Unobtrusive Ajax,在返回html数据后尝试检查元素是否加载了Jquery - ASP.NET MVC / Jquery Unobtrusive Ajax, trying to check if element is loaded with Jquery after the html data is returned 如何在ASP.NET MVC 4中进行复杂的客户端数据验证 - How to do complex client-side data validation in ASP.NET MVC 4 如何在文本框中设置值 - 使用jquery的asp.net? - How to set the value in the textboxes - asp.net using jquery? 将AngularJS模型发布到ASP.NET MVC控制器中的复杂模型 - Post AngularJS model to complex model in ASP.NET MVC controller 我们可以使用Html.Partial Helper附加jQuery中的HTML(大块)吗? C#ASP.Net MVC 5 - Can We Append (Chunk of) HTML in jQuery using Html.Partial Helper? C# ASP.Net MVC 5 ASP.NET MVC jQuery HTML5数据属性 - ASP.NET MVC jQuery HTML5 data attribute
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM