在 jQuery 中访问 ViewData 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25338307/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Accessing ViewData in jQuery not working
提问by rwkiii
I am trying to pass an IList back to my View where it can be accessible in a jQuery .each(), but I'm not having any success.
我试图将一个 IList 传递回我的视图,在那里它可以在 jQuery .each() 中访问,但我没有任何成功。
Example class:
示例类:
public class MyClass
{
private string _Name = "";
public int Id = null;
public MyClass ( string Name, int Id = null )
{
... Do something ...
}
public string Name
{
get
{
return _Name;
}
set
{
_Name = value;
}
}
}
In Controller:
在控制器中:
IList<MyClass> myClass = null;
myClass = GetNames();
ViewDataDictionary viewData = new ViewDataDictionary(null);
viewData.Add("myNames", Json(myClass));
// The string writer where to render the HTML code of the view
StringWriter stringWriter = new StringWriter();
// Render the Index view in a HTML string
ViewEngineResult viewResult = ViewEngines.Engines.FindView(ControllerContext,
"GeneratePDF", null);
ViewContext viewContext = new ViewContext(
ControllerContext,
viewResult.View,
viewData,
new TempDataDictionary(),
stringWriter
);
viewResult.View.Render(viewContext, stringWriter);
... Omitted for brevity ...
In the View I would like to use jQuery to iterate through ViewData["myNames"]:
在视图中,我想使用 jQuery 遍历 ViewData["myNames"]:
<script type="text/javascript">
_myNames = '@ViewData["myNames"]';
$.each(_myNames, function (i, item) {
...
});
</script>
The above doesn't work and unfortunately I'm not sure how to debug it because the view is being called by EvoPDF PDF Generator.
以上不起作用,不幸的是我不知道如何调试它,因为 EvoPDF PDF Generator 正在调用该视图。
I've also tried this in my View:
我也在我的视图中尝试过这个:
<input type="hidden" id="myNames" value="@ViewData["myNames"]" />
<script type="text/javascript">
_myNames = $('#myNames').val();
$.each(_myNames, function (i, item) {
});
</script>
Tracing through the controller code (where EvoPDF renders the page) I can see that the hidden field is containing the Type Name in the value attribute - not the data.
跟踪控制器代码(EvoPDF 呈现页面的位置)我可以看到隐藏字段在 value 属性中包含类型名称 - 而不是数据。
I've used ViewData many times without problem, but this is a bit different usage than I'm familiar with. Can anyone tell me how to pass an IList/List/IENumerable using ViewData so that I can iterate through it with jQuery .each()?
我已经多次使用 ViewData 没有问题,但这与我熟悉的用法有点不同。谁能告诉我如何使用 ViewData 传递 IList/List/IENumerable 以便我可以使用 jQuery .each() 遍历它?
EDIT
编辑
As an alternative, I could make a jQuery call from within my razor, but I'm not sure how to pass the variable to jQuery:
作为替代方案,我可以从我的剃刀中进行 jQuery 调用,但我不确定如何将变量传递给 jQuery:
@foreach (var myName in (List<MyClass>)ViewData["myNames"])
{
<tr>
<td class="text-center">
<script type="text/javascript">
FormatName('@myName.Name', 1);
</script>
</td>
</tr>
}
But myName.Name
doesn't seem to being passed to the FormatName() function. I know for a fact that myName.Name contains a valid value because it renders fine without the jQuery. How do I pass that var to my jQuery function?
但myName.Name
似乎没有传递给 FormatName() 函数。我知道 myName.Name 包含一个有效值,因为它在没有 jQuery 的情况下呈现良好。如何将该 var 传递给我的 jQuery 函数?
回答by
You first need to convert TempData
property to a javascript array of objects. Oualid KTATA was on the right track with first suggestion, but that only returns a string. In you document ready function:
您首先需要将TempData
属性转换为 javascript 对象数组。Oualid KTATA 的第一个建议是正确的,但它只返回一个字符串。在您的文档就绪功能中:
var _myNames = JSON.parse('@Html.Raw(Json.Encode(ViewData["myNames"]))');
which will return something like
这将返回类似
[{ID:1, Name:"FirstName"}, {ID:2, Name:"SecondName"}, ...]
You can then access it with
然后你可以访问它
$.each(_myNames, function(index, item) {
var id = item.ID;
var name = item.Name;
});
回答by Mohan Krishna Eluri
var UserName [email protected](Json.Encode(this.ViewData["UserName"]));
working fine
工作正常
or
或者
var UserName =JSON.parse('@Html.Raw(Json.Encode(this.ViewData["UserName"]))');
(less time consuming) or controller code:
(更少的时间)或控制器代码:
this.ViewData["UserName"] = HttpUtility.HtmlEncode("somestring");
UI example:
界面示例:
var UserName ="Html.Raw(this.ViewData["UserName"])"
or JUST
要不就
var UserName ="Html.Raw(this.ViewData["UserName"])"
回答by Oualid KTATA
Try to use this when you set the viewData object to your client side variable:
当您将 viewData 对象设置为客户端变量时,请尝试使用它:
var _myNames= @Html.Raw(Json.Encode(ViewData["myNames"]));
OR
或者
_myNames = '<%= ViewData["myNames"] %>'
Instead of:
代替:
_myNames = '@ViewData["myNames"]';