-1

I have the following method in controller

public ActionResult Categorized() 
{ 
    var x=new Test.Models.MobileClass(); 
    return View(x);
}

x object contains methods that return xmldocument data

how can I pass this object to view to access methods and display data from xmldocument in browser

I can display it element element by using the following code

document.writeln("@Model.getxml().ChildNodes.Count");

but I want to use for loop displaying the contents of object and the following code didn't work in javascript

var size=parseInt("@Model.Getxml().ChildNodes.Count");
for  (var i=0; i<size; i++)

{

    document.writeln("@Model.Getxml().ChildNodes[i].InnerText");

 }

can you help me please

3
  • 2
    If the code in your view has to know specific details about the structure of an XML document, you need a better ViewModel. Commented May 3, 2013 at 20:22
  • javascript is client side, in the browser, the view and the Model is server side, you generate html at the server side and the browser receives that html Commented May 3, 2013 at 20:23
  • You already asked this question once and the exact same answers still apply: stackoverflow.com/q/16351921/1043198 Commented May 3, 2013 at 22:19

2 Answers 2

1

First of all, your view should not be calling Getxml and mucking about with the XML DOM. It's the job of the controller to present the view with "ready to render" data. That's what a ViewModel is for.

public ActionResult Categorized() 
{ 
    var foo = new Test.Models.MobileClass(); 
    var xml = foo.Getxml();
    var viewData = xml.ChildNodes.Cast<XmlNode>().Select(x => x.InnerText);
    return View(viewData);
}

Now we're passing an IEnumerable<string> to the view, containing just the values we want to render.

In the view, you should not be using javascript to render your data to HTML - use the view to render your data to HTML - the Razor template engine is really good at that! Something like...

<ul>
@foreach (var item in Model) {
    <li>@item</li>
}
</ul>
Sign up to request clarification or add additional context in comments.

Comments

0

The javascript will fail, since the variable i will be evaluated on the server side and doesn't exist then. Use a foreach in the view, with razor:

@foreach(var child in Model.Getxml().ChildNodes)
{
    child.InnerText
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.