Click here to Skip to main content
15,892,517 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Problem Description:
I a trying to pass the Ienumerable data from controller to view to populate my dropdownlist.While doing so i have somehow succeded to get the data till the controller.How do i pass it to the view?.I am a bit unaware about the razor syntax as m a rookie fr mvc.
Any help would be really appreciated.

*note Forgive any code abuses.
Thanks.
Model:
XML
public class TS_entryscreen
   {
       public static string connstr = System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
       SqlConnection connfordata = new SqlConnection(connstr);

      public string Name { get; set; }
    

       public IEnumerable<string> fillalldrp()
   {
        DataTable dtfrfilluser = new DataTable();
        connfordata.Open();
        SqlCommand cmdfruser = new SqlCommand("select (s000_firstname+s000_lastname)as Name  from s000_users", connfordata);
        SqlDataAdapter dafruser = new SqlDataAdapter(cmdfruser);
        connfordata.Close();
        dafruser.Fill(dtfrfilluser);

        List<string> lstusers = new List<string>();
        lstusers = dtfrfilluser.Rows.OfType<DataRow>().Select(dr => dr.Field<string>("Name")).ToList();

        IEnumerable<string> myEnumerable = lstusers;

        return myEnumerable;

   }

Controller:
XML
public ActionResult Index()
      {
          TS_entryscreen tsuser = new TS_entryscreen();
         IEnumerable<string> tsnewuser = tsuser.fillalldrp();
         return View(tsnewuser);
      }

View:
Quote:
@model loginntimesheet.Models.TS_entryscreen
@{
ViewBag.Title = "Home Page";
}
@section featured {

}
Posted

1 solution

You should really go through a book or on-line tutorials for MVC as you can't learn something from scratch by asking questions on a forum. Your model is defined by what you return from the controller

return View(tsnewuser);


tsnewuser is IEnumerable<string> so your view needs to define that as the model, but you are defining

@model loginntimesheet.Models.TS_entryscreen


So that needs to be

@model IEnumerable<string>


You access the model using "Model" (Model is a special variable that will always be the type defined in @model), so to loop through your strings

C#
foreach (string name in Model)
{
    <p>@name</p>
}


However a drop down comprises of a list of things that have two values, a name and a value, and you also need somewhere to store the selected item, so you can't make a drop down from a list of strings. There is a special structure used to make dropdowns, if you have a look at this there will be some sample code

http://odetocode.com/blogs/scott/archive/2013/03/11/dropdownlistfor-with-asp-net-mvc.aspx[^]
 
Share this answer
 
v3

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