Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a mvc application in vs 2010.in that i have a home controller with one method Index().It is going to return a list of countries.That countries list i want to display through view. How to do that in vs 2010.please tell me how to add view for this and what we need to write code for displaying it to end user.


Here is my controller action.

public ActionResult Index()
{
ViewData["countries"]= new List<string>()
{
"INDIA",
"PAKISTHAN",
"AUSTRALIA",
"SOUTH AFRICA"
};
return View();
}
Posted
Updated 14-Jul-20 21:58pm
v2

You can Simply create a controller by myController.cs

public ActionResult Index()
       {
           ViewBag.Countries =new List<string>()
           {
               "Pakistan",
               "UAE",
               "Iran",
               "China"
           };
           return View();
       }

Then create a view by viewName.cshtml
<h2>Index</h2>
<ul>
    @foreach (string strCountry in ViewBag.Countries)
    {
        <li>@strCountry</li>
    }
</ul>
 
Share this answer
 
Rather create the Model for the country, and return that model to view.

// Model
C#
public class Country
{
    public int ID { get; set; }
    public string Name { get; set; }
}

// Controller
C#
public ActionResult Index()
{
    List<country> countries = new List<country>() {
        new Country() { ID = 1, Name = "India"},
        new Country() { ID = 2, Name = "Pakistan"},
        new Country() { ID = 3, Name = "Australia"},
        new Country() { ID = 4, Name = "South Africa"}
    }
    return View(countries);
}</country></country>

// View
ASP
@model IEnumerable<country>
<ul>
    @foreach(var item in Model)
    {
        <li id="@item.ID">@item.Name</li>
    }
</ul></country>

This is the basic idea how you can do it in MVC, you can change this according to your need. :)

[Updates after the comments]
// controller
IEnumerable<string> list = new List<string><br />
{<br />
"INDIA",<br />
"PAKISTHAN",<br />
"AUSTRALIA",<br />
"SOUTH AFRICA"<br />
};<br />
ViewData["countries"] = list;<br />
<br />
// view<br />
@foreach (string item in ViewData["countries"] as List<string>)<br />
{<br />
<p>@item</p><br />
}


-KR
 
Share this answer
 
v2
Comments
Member 11944583 5-Nov-15 0:14am    
cant i use viewdata for transfer data between controller and view for displaying.
Member 11944583 5-Nov-15 0:19am    
i am not able to create country class instance in conyroller what is the problem behind this.
Krunal Rohit 5-Nov-15 0:27am    
Yes you can, do something like:
@foreach(var item in ViewData["Countries"])
{
<p>item</p>
}
Let me know if it works.

-KR
Member 11944583 5-Nov-15 0:41am    
this my code code in view:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<dynamic>" %>

<div>
foreach(var i in ViewData["countries"])
{
<p>i</p>
}
</div>
and i am getting output like:
foreach(var i in ViewData["countries"]) {
i
Member 11944583 5-Nov-15 0:42am    
countries list is not displaying it displaying as it is with in div tag

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900