Salman Shaykh
Salman Shaykh

Reputation: 221

How to write serverside code in csHTML

I want to write some server side code to display the data stored in the View Data of the page in the csHtml page. I have done some research but couldn't find a solution. Output of the code is enter image description here

  @(Html.Kendo().TabStrip()
                    .Name("Logins")
                    .SelectedIndex(0)
                    .Animation(animation => animation.Open(open => open.Fade(FadeDirection.In)))
                    .Items(items =>
                    {


                        items.Add().Text("Contact Information").Content(
                            "<div class='employee-details'>" +
                                "<ul>" +
                                "<%>for(int i=0;i<8;i++)"+
                        "{<%>"+
                                    "<li><label>LoginID:</label>"+viewDataServer[i].LoginID.ToString()+"</li>" +
                                    "<li><label>ServerID:</label>" + viewDataServer[i].ServerID.ToString() + "</li>" +
                                    "<li><label>UserID:</label>" + viewDataServer[i].UserID.ToString() + "</li>" +
                                    "<li><label>Password:</label>" + viewDataServer[i].passwd.ToString() + "</li>" +
                                "<%>}<%>"+
                                "</ul>" +
                            "</div>"

                          );      

                    })
                .ToClientTemplate())

Upvotes: 0

Views: 2949

Answers (1)

pwdst
pwdst

Reputation: 13755

I think that you are looking at the wrong view engine page on the Telerik tabstrip demo site - instead of "index.aspx" you need to look at "index.cshtml" if you are using the Razor view engine.

I think that the correct syntax in your case would be-

@(Html.Kendo().TabStrip()
                    .Name("Logins")
                    .SelectedIndex(0)
                    .Animation(animation => animation.Open(open => open.Fade(FadeDirection.In)))
                    .Items(items =>
                    {
                        items.Add().Text("Contact Information").Content(@<div class='employee-details'>
                                <ul>
                                @for(var i=0; i<8; i++)
                                {
                                    <li><label>LoginID:</label>@viewDataServer[i].LoginID.ToString()</li>
                                    <li><label>ServerID:</label>@viewDataServer[i].ServerID.ToString()</li>
                                    <li><label>UserID:</label>@viewDataServer[i].UserID.ToString()</li>
                                    <li><label>Password:</label>@viewDataServer[i].passwd.ToString()</li>
                                }
                                </ul>
                            </div>);
                    })
                .ToClientTemplate())

I would however strongly encourage you not to use ViewData to page data from your controller to the view in this way. This is exactly the purpose of a view model, which would be strongly typed. If for some reason using a view model is not practical, I would suggest you at least consider ViewBag - which is a dynamic type - meaning that you could pass an IEnumerable (or List) of a strongly typed class, perhaps your Contact object, and simply enumerate through them using something like-

@foreach (var contact in ViewBag.Contacts) { ... }

The ideal would absolutely be to set the model to a List or IEnunerable like-

@model IEnumerable<Contact>

And then enumerate in your View as follows-

@foreach (var contact in Model) { ... }

Upvotes: 1

Related Questions