Click here to Skip to main content
15,886,056 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
It is such that I must retrieve information from the database. The content comes from the database is unique and much more than that as I show here

this is in my create.cshtml

HTML
<div class="col-md-6">
   <label>subjects</label>
   <select id="Select1" class="form-control input-lg">
     <option value="1">Danish</option>
     <option value="2">Mathematics</option>
     //There will then be many more after. & I work with Linq
   </select>
</div>


Controllers -> createController.cs have i :

C#
public ActionResult create()
{
   return View();
}


Tablen that I should download from called Professional

I have searched around the net and could not really find anything I could to act in some manner.

the problem is how how can I order it still looked after my own so that it is just to throw into the database when you sign up as users of the page.

How do I done this?
Posted

1 solution

In your create method, you need to populated the list of values from the database using SqlConnection or Entity Framework connection. You can then use ViewBag variable to send the data to the view. The code might look as below:

C#
public ActionResult create()
{
var subjects  = dbContext.Subjects.ToList(); // if the Subjects is the table in the database
ViewBag.Subjects = new SelectList(subjects, "SubjectId", "SubjectName","");
return View();
}


This will give you the list of subject as ViewBag data in the create view.
In the view you can use either Select or DropDownList control to display the Subjects. For Select control you can use as below:
C#
<select id="subjects" name="subjects" class="selectpicker show-tick form-control" title="">
@foreach (var item in ((IEnumerable<SelectListItem>)ViewBag.Subjects))
{
<option>@item.Text</option>     
}
</select></select>


If you use DropDownList then you need to send the data from the controller view Model

C#
@Html.DropDownListFor(m=>m.Subject, new SelectList(Model,"SubjectId","SubjectName"));


Hope this will help
 
Share this answer
 
Comments
Jesper Petersen 21-Jul-15 17:36pm    
Thanks for help, and now its work :)
Mostafa Asaduzzaman 21-Jul-15 17:40pm    
Nice to hear that.

Best regards,
Mostafa

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