简体   繁体   English

ViewBag上的Javascript代码mvc 4

[英]ViewBag on a Javascript code mvc 4

I am having a trouble doing this: I have a ViewBag that contains the list of documents Id's and the response time for each one, created like this: 我在这方面遇到了麻烦:我有一个ViewBag,其中包含文档ID列表和每个文档的响应时间,如下所示:

Controller : 控制器

ViewBag.Documents= (from n in db.DocumentType
                     select new {a = n.DocumentTypeId, b=n.ResponseTime});

Based on the document that the user selected on a dropdownlist, the javascript code calculates the real duration of processing the document (duration). 基于用户在下拉列表中选择的文档,javascript代码计算处理文档的实际持续时间(持续时间)。

DocumentId(int)          Response(int)
    1                     2 days
    2                     3 days
    3                     1 day

The complete javascript code calculates the final date based on response time without weekends and holidays, but the only part that it is not working is the following part of JavaScript code: 完整的javascript代码根据没有周末和假日的响应时间计算最终日期,但唯一不起作用的部分是JavaScript代码的以下部分:

JavaScript at the view: 视图中的JavaScript:

function CalculateTime(documenttype) {
                var duration= 0;
                var jScriptArray= new Array();
                var array = '@Html.Raw(Json.Encode(ViewBag.Documents))';
                for (var i = 0; i < array.length; i++) {
                    for (var j = 0; j < 2; j++) {
                        jScriptArray [i][j] = array [i][j];
                    }
                }
                for (i = 0; i < jScriptArray.length; i++) {
                   if (documenttype== jScriptArray [i][0]) duration= jScriptArray [i][1];
                }
                return duration;
            }

I already have the correct documenttype variable based on a dropdownlist but this script is not working. 我已根据下拉列表获得了正确的documenttype变量,但此脚本无效。 I think is the way I want to use the ViewBag, but I am not pretty sure. 我认为是我想要使用ViewBag的方式,但我不太确定。 What I need to know is: what is wrong with my code or another way to do this. 我需要知道的是:我的代码或其他方法有什么问题。

Thanks in advance 提前致谢

只需删除引号即可

 var array = @Html.Raw(Json.Encode(ViewBag.Documents));

Just remove the quotes(') and you have to Newtonsoft.Json for this. 只需删除引号('),你就必须使用Newtonsoft.Json。

var array = @Newtonsoft.Json.JsonConvert.SerializeObject(ViewBag.Documents);

This will work. 这会奏效。

A friend of mine helped me solve my problem. 我的一个朋友帮助我解决了我的问题。 Some of you told me to use Json, so we did this: 有些人告诉我使用Json,所以我们这样做了:

Controller: 控制器:

using System.Web.Script.Serialization;
using Newtonsoft.Json;

...

var timeList= db.DocumentType.ToList();

ViewBag.ResponseTimes= new JavaScriptSerializer().Serialize(
                JsonConvert.SerializeObject(new
                {
                    times= (from obj in timeList
                               select new { DocumentTypeId= obj.DocumentTypeId, ResponseTime= obj.ResponseTime})
                }, Formatting.None));

JavaScritp at the View: 视图中的JavaScritp:

function CalculateTime(documenttype) {
                var duration = 0;
                var jScriptArray = new Array();
                var array = $.parseJSON(@Html.Raw(ViewBag.ResponseTimes));
            for (i = 0; i < array.times.length; i++) {
                if (parseInt(documenttype) == array.times[i].DocumentTypeId) {
                    duration= array.times[i].ResponseTimes;
                }
            }
            return duration;
        }

Thanks! 谢谢!

PS: I don't know which of both answers give the acceptance cause we used part of both.... PS:我不知道两个答案中的哪一个给出了接受因为我们使用了两者的一部分....

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM