admin管理员组文章数量:1025498
I'm creating a ViewData in an ASP.Net Controller like this:
var elements = db.Requests.GroupBy(user => user.submitUser)
.Select(slt => new { UsersCount = slt.Key, CountReq = slt.Count() });
this will have for example
UsersCount Count
user1 3
user2 3
user3 3
Then in Javascript I want to create an array with UsersCount and Count, for example:
var arrayUsers = ViewData.UsersCount;
var arrayCounter = ViewData.CountReq;
I already tried several solutions and I can't do it. What's the best solution?
CONTROLLER:
public ActionResult Statistics()
{
var elements = db.Requests.GroupBy(user => user.submitUser)
.Select(slt => new { UsersCount = slt.Key, CountReq = slt.Count() });
ViewData["usersCounter"] = elements;
return View();
}
Javascript in VIEW
<script>
var check = @ViewData["usersCounter"]**;**error
....
}
I'm creating a ViewData in an ASP.Net Controller like this:
var elements = db.Requests.GroupBy(user => user.submitUser)
.Select(slt => new { UsersCount = slt.Key, CountReq = slt.Count() });
this will have for example
UsersCount Count
user1 3
user2 3
user3 3
Then in Javascript I want to create an array with UsersCount and Count, for example:
var arrayUsers = ViewData.UsersCount;
var arrayCounter = ViewData.CountReq;
I already tried several solutions and I can't do it. What's the best solution?
CONTROLLER:
public ActionResult Statistics()
{
var elements = db.Requests.GroupBy(user => user.submitUser)
.Select(slt => new { UsersCount = slt.Key, CountReq = slt.Count() });
ViewData["usersCounter"] = elements;
return View();
}
Javascript in VIEW
<script>
var check = @ViewData["usersCounter"]**;**error
....
}
Share
Improve this question
edited Jul 20, 2015 at 0:50
HaveNoDisplayName
8,537106 gold badges40 silver badges50 bronze badges
asked Jul 17, 2015 at 23:36
Andre RoqueAndre Roque
5432 gold badges10 silver badges32 bronze badges
2
- What's the question? – shoover Commented Jul 18, 2015 at 0:06
- try dis var check = '@ViewData["usersCounter"]'; – Light Commented Jul 18, 2015 at 6:29
3 Answers
Reset to default 1To use ViewData in a javascript tag just prefix ViewData with the @ symbol
var check = @ViewData["array"]
In your controller
ViewData["array"] = elements;
Why not just set the variable to your model since it looks like you're setting it to that
var check = @Html.Raw(Json.Encode(Model));
Then you can do whatever
var blur = check.UserName
The code
@ViewData["usersCounter"]
will effectively do this:
@ViewData["usersCounter"].ToString()
You're setting your viewdata to the result of a select, so the .ToString
wil output the generic generic type name directly to the output.
Simply check the source code of the rendered page to see what is being rendered.
Also, specify the 'error' that you get.
If you only change your code to
var check = '@ViewData["array"]'
(as per other answers) then 'check' will not contain the list, only the list.tostring()
If you want to copy the whole list into a js variable, you'll need to serialise it first, eg, something like (with JSON nuget package installed):
var check = @(Newtonsoft.Json.JsonConvert.SerializeObject(ViewData["array"]));
To note: many people will remend that you use a ViewModel rather than ViewData as it's strongly typed as generally easier to work with / maintain.
However, is there as reason you need/want this in javascript? The GroupBy and Select, even after converting to js aren't really js, and are better suited for working with c# or @{}
statements.
Andre have you tried
var check = '@ViewData["array"]'
I'm creating a ViewData in an ASP.Net Controller like this:
var elements = db.Requests.GroupBy(user => user.submitUser)
.Select(slt => new { UsersCount = slt.Key, CountReq = slt.Count() });
this will have for example
UsersCount Count
user1 3
user2 3
user3 3
Then in Javascript I want to create an array with UsersCount and Count, for example:
var arrayUsers = ViewData.UsersCount;
var arrayCounter = ViewData.CountReq;
I already tried several solutions and I can't do it. What's the best solution?
CONTROLLER:
public ActionResult Statistics()
{
var elements = db.Requests.GroupBy(user => user.submitUser)
.Select(slt => new { UsersCount = slt.Key, CountReq = slt.Count() });
ViewData["usersCounter"] = elements;
return View();
}
Javascript in VIEW
<script>
var check = @ViewData["usersCounter"]**;**error
....
}
I'm creating a ViewData in an ASP.Net Controller like this:
var elements = db.Requests.GroupBy(user => user.submitUser)
.Select(slt => new { UsersCount = slt.Key, CountReq = slt.Count() });
this will have for example
UsersCount Count
user1 3
user2 3
user3 3
Then in Javascript I want to create an array with UsersCount and Count, for example:
var arrayUsers = ViewData.UsersCount;
var arrayCounter = ViewData.CountReq;
I already tried several solutions and I can't do it. What's the best solution?
CONTROLLER:
public ActionResult Statistics()
{
var elements = db.Requests.GroupBy(user => user.submitUser)
.Select(slt => new { UsersCount = slt.Key, CountReq = slt.Count() });
ViewData["usersCounter"] = elements;
return View();
}
Javascript in VIEW
<script>
var check = @ViewData["usersCounter"]**;**error
....
}
Share
Improve this question
edited Jul 20, 2015 at 0:50
HaveNoDisplayName
8,537106 gold badges40 silver badges50 bronze badges
asked Jul 17, 2015 at 23:36
Andre RoqueAndre Roque
5432 gold badges10 silver badges32 bronze badges
2
- What's the question? – shoover Commented Jul 18, 2015 at 0:06
- try dis var check = '@ViewData["usersCounter"]'; – Light Commented Jul 18, 2015 at 6:29
3 Answers
Reset to default 1To use ViewData in a javascript tag just prefix ViewData with the @ symbol
var check = @ViewData["array"]
In your controller
ViewData["array"] = elements;
Why not just set the variable to your model since it looks like you're setting it to that
var check = @Html.Raw(Json.Encode(Model));
Then you can do whatever
var blur = check.UserName
The code
@ViewData["usersCounter"]
will effectively do this:
@ViewData["usersCounter"].ToString()
You're setting your viewdata to the result of a select, so the .ToString
wil output the generic generic type name directly to the output.
Simply check the source code of the rendered page to see what is being rendered.
Also, specify the 'error' that you get.
If you only change your code to
var check = '@ViewData["array"]'
(as per other answers) then 'check' will not contain the list, only the list.tostring()
If you want to copy the whole list into a js variable, you'll need to serialise it first, eg, something like (with JSON nuget package installed):
var check = @(Newtonsoft.Json.JsonConvert.SerializeObject(ViewData["array"]));
To note: many people will remend that you use a ViewModel rather than ViewData as it's strongly typed as generally easier to work with / maintain.
However, is there as reason you need/want this in javascript? The GroupBy and Select, even after converting to js aren't really js, and are better suited for working with c# or @{}
statements.
Andre have you tried
var check = '@ViewData["array"]'
本文标签: aspnetUsing ViewData in Javascript codeStack Overflow
版权声明:本文标题:asp.net - Using ViewData in Javascript code - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745612743a2159103.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论