47014

create object of viewModel in view in asp.net mvc

Question:

I've created an object of class in controller in asp.net mvc and pass it to view page by view model,

This object has many methods that return different data type (xmldocument, string, int, array, etc)

I've used the following way to access any methods @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

var obj=@Model

And then access any methods from obj variable

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.

Answer1:

You are mixing up the server-side code (c#) and client-side code (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..

<hr />

<strong>To help you identify the server-side code</strong>

@Model.Getxml().ChildNodes.Count and @Model.Getxml.ChildNodes[i].InnerText are server code which cannot be combined in your particular case with Javascript code

<hr />

<strong>To help you identify the client-side code</strong>

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

Currently I suppose it would be printing as many times the count

<blockquote>

@Model.Getxml.ChildNodes[i].InnerText<br /> @Model.Getxml.ChildNodes[i].InnerText

</blockquote>

Answer2:

An alterntive to Harsh Baid's answer would be to create the loop in Razor, rather than in Javascript, i.e. create a Razor loop to write out lines of JS, like so:

<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.

Recommend

  • Does CGImageRelease() free UIImage's memory?
  • JAX-WS RI: Best way to implement a Method Interceptor
  • Is there any API for the command vmmap
  • Encode string to match encoded form field name in PHP POST array
  • Get all existing pointers to an object
  • mapping between two ontologies
  • Debugging Android native application using ndk-gdb
  • Ionic storage “get” returns null only on the second call within method
  • List comprehension with if conditional to get list of files of a specific type
  • Need code translation from VB to C#
  • Best practice to eliminate magic numbers within a member function
  • How to load gif image while ajax content is loading and javascript [duplicate]
  • Eliminate partial duplicate rows from result set
  • Using a canvas object in a thread to do simple animations - Java
  • MonoTouch: How to download pdf incrementally as indicated in the Apple slides “Building Newsstand Ap
  • Mysterious problem with floating point in LISP - time axis generation
  • How to know when stdin is empty if it contains EOF?
  • Email format validation in mvc3 view
  • C# - Is there a limit to the size of an httpWebRequest stream?
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • HTML download movie download link
  • How to add date and time under each post in guestbook in google app engine
  • Why doesn't :active or :focus work on text links in webkit? (safari & chrome)
  • Updating server-side rendering client-side
  • Join two tables and save into third-sql
  • How to model a transition system with SPIN
  • When should I choose bucket sort over other sorting algorithms?
  • Weird JavaScript statement, what does it mean?
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • ORA-29908: missing primary invocation for ancillary operator
  • How do you troubleshoot character encoding problems?
  • Unanticipated behavior
  • How to format a variable of double type
  • How to set the response of a form post action to a iframe source?
  • coudnt use logback because of log4j
  • Setting background image for body element in xhtml (for different monitors and resolutions)
  • Reading document lines to the user (python)
  • Binding checkboxes to object values in AngularJs
  • How to Embed XSL into XML
  • Converting MP3 duration time