Click here to Skip to main content
14,880,310 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

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
   
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
Krunal Rohit 5-Nov-15 0:46am
   
forgot to write @ before the foreach.
See the updated comment.

-KR
Member 11944583 5-Nov-15 0:51am
   
then output like this:

@foreach(var i in ViewData["countries"]) {
i

}
Krunal Rohit 5-Nov-15 1:08am
   
// controller
<pre> IEnumerable<string> list = new List<string>
{
"INDIA",
"PAKISTHAN",
"AUSTRALIA",
"SOUTH AFRICA"
};
ViewData["countries"] = list;

// view
@foreach (string item in ViewData["countries"] as List<string>)
{
<p>@item</p>
}
</pre>

-KR
Member 11944583 5-Nov-15 1:10am
   
already tried this but no use.actually while typing i am not getting any intellisence is there any problem.
Member 11944583 5-Nov-15 1:11am
   
getting output what we written in Div tag.
Krunal Rohit 5-Nov-15 1:15am
   
See the updated solution please.

-KR
Member 11944583 5-Nov-15 1:17am
   
no use rohit
Krunal Rohit 5-Nov-15 1:21am
   
I've tested the solution and then updated it here. It's working fine.

-KR
Member 11944583 5-Nov-15 1:25am
   
i am using mvc 2.0 in vs 2010 and .net framework 4.0
in the same environment did u finish this.
Krunal Rohit 5-Nov-15 1:30am
   
No, but that is not the issue. you can convert this to your razor syntax.
Replace @ with the <%: and at the end of the statement %>.

-KR
Member 11944583 5-Nov-15 1:35am
   
getting errors could please modify it.
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<dynamic>" %>

<div>


<% foreach(var i in ViewData["countries"])%>
{
<p><% i %></p>
}
</div>
Member 11944583 5-Nov-15 1:49am
   
this is my code:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<dynamic>" %>

<div>


<%foreach(string i in (List<string>)ViewData["countries"])
{%>
<p>
<% i.Tostring(); %>

</p>
<%} %>


</div>
i am getting blank page as output.
Krunal Rohit 5-Nov-15 1:59am
   
Something's wrong. Did you try with debugger ?

-KR
Member 11944583 5-Nov-15 2:05am
   
got it just we forgot "=" symbol before i variable thats it.thank you for support KR.
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>
   

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