2

I am new to asp .net and MVC. I have a service method called Jumbo(). This method returns a list containing first name, last name and contact number. And I call this method from my main project as below. Also I return it to the view page.

var info = add.jumbo();
ViewData["sample"] = info;

return View("FormResults");

When I debugged the code the variable 'info' contains all the data I needed. The problem is how do I iterate the variable in html table using razor. I searched a lot to find a solution but I failed. Please help me with a solution. Thanks in advance.

4 Answers 4

5

First you need to pass the model to the view:

return View("FormResults", info);

And in the view, you'll need to state the Type of model, and use @ to indicate code rather than html:

@model List<Foo> // Whatever your list is
@foreach (var item in Model)
{
    <span>@item.Text</span>
}

Here's a blog about it in detail: http://weblogs.asp.net/scottgu/asp-net-mvc-3-new-model-directive-support-in-razor

Sign up to request clarification or add additional context in comments.

4 Comments

I don't use Model. I call the service from my controller and store it in a variable. Also I pass my data to the view. I have a separate project for my services. Actually I am not sure whether I am doing it correctly. Please help me
Consider I have my service method Jumbo() in project1 and I call that method from project2. I have a list named UserTable in project1. I retrieve data from db and store it in that list(project1). From my project2 I jsut call that method and store it to a variable. How can I include that list to my View in project2? Please help me...
@user3627446 if UserTable is the class name, then change Foo from this answer to UserTable and change item.Text to the correct property name, i.e item.FirstName.
I tried replacing Foo with List<userTable> but it show error 'Only assignment,call, increment,decrement,await and new object expressions can be used as a statement'
4

Case 1: Basically you can observe this within your application itself. MVC itself provide you your answer. When ? When you creating View against the action which returning list. Say your action returning list of customer then you can observe following code.

@model IEnumerable<Customer>

<h2>Customers</h2>
<p>
    @Html.ActionLink("Create New", "CreateCustomer", "ControllerName")
</p>
<table>
    <tr>

        <th>
            @Html.DisplayNameFor(model => model.Email)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Name)
        </th>
        <th></th>
    </tr>

    @foreach (var item in Model)
    {    
        <tr>           
            <td>
                @Html.DisplayFor(modelItem => item.Email)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Name)
            </td>

                <td>                        

                    @Html.ActionLink("Edit |", "UpdateCustomer", new { id =  item.Id}) 
                    @Html.ActionLink("Details", "Details", new { id =  item.Id}) 
                    @Html.ActionLink("Delete", "Delete", new { id =  item.Id})                      
               </td>           
        </tr>
    }
</table>

Case 2: your scenario is when you sending the list within ViewData. Then you have to cast ViewData into respective model list and then you can perform same foreach loop. Action: var info = add.jumbo(); ViewData["sample"] = info;

return View("FormResults");

View:

  @if (ViewData["sample"] != null)
    {
        List<Info> infoList = (List<Info>)ViewData["sample"];
        foreach (var i in infoList)
        { 
        //Perform your html here enclosed with html tags.
        }        
    }

Comments

3
@model  Models.Nodes 
@foreach (var item in Model)
{
 <li>
    <span> item.name </span>
 </li>
}

Comments

2

In your view write a foreach loop as below

@model List<Object>
@foreach(var item in Model)
{
  item.YourProperty;//without html
  <label>@item.YourProperty</label> //in html
}

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.