简体   繁体   English

在asp.net mvc中的视图中创建viewModel对象

[英]create object of viewModel in view in asp.net mvc

I've created an object of class in controller in asp.net mvc and pass it to view page by view model, 我在asp.net mvc中的控制器中创建了一个类对象,并将其按视图模型传递给视图,

This object has many methods that return different data type ( xmldocument , string , int , array , etc) 该对象具有许多返回不同数据类型的方法( xmldocumentstringintarray等)

I've used the following way to access any methods @Model.Getxml().ChildNodes.Count" "@Model.Getxml().ChildNodes[0].InnerText 我使用以下方法访问任何方法@Model.Getxml().ChildNodes.Count" "@Model.Getxml().ChildNodes[0].InnerText

I want to declare a variable of this object in javascript and call any methods that I want from the variable like the following 我想在javascript中声明此对象的变量,然后从该变量调用所需的任何方法,如下所示

var obj=@Model

And then access any methods from obj variable 然后从obj变量访问任何方法

But I have a problem when I write loop for tracing elements in array like the following 但是当我编写用于跟踪数组中元素的循环时,我遇到了一个问题,如下所示

var size=parseInt("@Model.Getxml().ChildNodes.Count");
for  (var i=0; i<size; i++) 
{
    document.writeln ("@Model.Getxml().ChildNodes[i].InnerText");
}

This code didn't work, Help would be appreciated. 该代码无法正常工作,将不胜感激。

You are mixing up the server-side code (c#) and client-side code (javascript) 您正在混合服务器端代码(c#)和客户端代码(javascript)

You should convert the values in json or xml and put in hidden field and process the value in the hidden field in for loop in javascript aka client-side.. 您应该转换json或xml中的值,然后将其放入隐藏字段中,然后在JavaScript aka客户端的for循环中处理隐藏字段中的值。


To help you identify the server-side code 为了帮助您识别服务器端代码

@Model.Getxml().ChildNodes.Count and @Model.Getxml.ChildNodes[i].InnerText are server code which cannot be combined in your particular case with Javascript code @Model.Getxml().ChildNodes.Count@Model.Getxml.ChildNodes[i].InnerText是服务器代码,在特定情况下不能与Javascript代码组合


To help you identify the client-side code 为了帮助您识别客户端代码

var size=parseInt(<variable>);
for  (var i=0; i<=0; i++) 
{
    document.writeln ("<Text>");
}

Currently I suppose it would be printing as many times the count 目前,我想它的打印次数是

@Model.Getxml.ChildNodes[i].InnerText @ Model.Getxml.ChildNodes [I] .InnerText
@Model.Getxml.ChildNodes[i].InnerText @ Model.Getxml.ChildNodes [I] .InnerText

An alterntive to Harsh Baid's answer would be to create the loop in Razor, rather than in Javascript, ie create a Razor loop to write out lines of JS, like so: Harsh Baid答案的替代方案是在Razor中创建循环,而不是在Javascript中创建循环,即创建Razor循环以写出JS行,如下所示:

<script type="text/javascript">
@foreach(var node in Model.Getxml().ChildNodes)
{
    @String.Format("document.writeln(\"{0}\");", node.InnerText)
}
</script>

This will output something like the following: 这将输出如下内容:

<script type="text/javascript>
    document.writeln("Inner text 1!");
    document.writeln("More inner text!");
    document.writeln("etc.");
    document.writeln("etc.");
    document.writeln("etc.");
</script>

Here, the server-side code loops over these values as it's the only thing that has access to them. 在这里,服务器端代码循环访问这些值,因为它是唯一可以访问它们的东西。 It then effectively writes out a bunch of lines of client-side code with hardcoded strings. 然后,它使用硬编码字符串有效地写出了几行客户端代码。 It's not the most elegant solution, but it might help you to understand the difference. 这不是最优雅的解决方案,但是它可以帮助您了解差异。

This is in contrast to what you are currently trying to do, which is write out Javascript that will loop over these values, which it can't do, because these values no longer exist when Javascript does its thing. 这与您当前正在尝试做的相反,后者正在写出Javascript,它将遍历这些值,而这是无法做到的,因为当Javascript执行其操作时,这些值不再存在。

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

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