Click here to Skip to main content
15,898,371 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Can someone in cp pls show me how to implement a dropdownlist and send it to view. Also, storing and save the selected value. I have been battling on this for months.
Posted
Comments
Michael_Davies 16-May-15 14:29pm    
Show your code.
[no name] 17-May-15 4:49am    
I have a class with selectlist on it:

public class Ready
{
[Key]
public int ReadyId { get; set; }
public string Name { get; set; }
public string Food { get; set; }

public SelectList CountrySelectList { get; set; }

}

Then also a Country class where i wrote the list of countries in the database so that i can be able to retrieve the list from the controller end..

public class Country
{
[Key]
public int Country_Id { get; set; }
public string Country_Name { get; set; }
}

and in my controller i have this:

public ActionResult Create()
{
/*Getting data from database*/
List<country> objcountrylist = (from data in db.Countries
select data).ToList();
Country objcountry = new Country();

objcountry.Country_Name = "Select";
objcountry.CountryId = 0;
objcountrylist.Insert(0, objcountry);
SelectList objmodeldata = new SelectList(objcountrylist, "Country_Id", "Country_Name", 0);
/*Assign value to model*/
Ready objcountrymodel = new Ready();

objcountrymodel.CountrySelectList = objmodeldata;

return View(objcountrymodel);
}

The Http post of the create action is what i don't know how to code inorder to get and save the selected country in the database.

Here is my view:


<div class="form-group">
@Html.LabelFor(model => model.Country, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.Country, Model.CountrySelectList, "select country now -", new { @class = "form-control", @style = "width:200px;" })
@Html.ValidationMessageFor(model => model.Country)
</div>
</div>


Please, i need your help.. i am set to create an application for like the past three months now.. but i just don't know why Visual studio has 1000 errors inside of it. Please, help me with my code. i am still a newbie in this area.

When i checked the database of "Ready", i now see: CountrySelectList_DataValueText and CountrySelectList_DataText
[no name] 16-May-15 17:33pm    
This is not my first time asking this question. Actually, what i want to know is how to save selectd value into database.
That is , coding creaté post in controller
jame01 16-May-15 18:19pm    
im not sure about your question and what type of databse but you may use this if u use sql database
sqlcommand cmd=new sqlcommand("insert into table2 values (select id from table2 where name =@name)",conn)
cmd.parametrs.addwithvalue("@name",combobox.text);
cmd.executenonquery;
[no name] 17-May-15 4:49am    
I have a class with selectlist on it:

public class Ready
{
[Key]
public int ReadyId { get; set; }
public string Name { get; set; }
public string Food { get; set; }

public SelectList CountrySelectList { get; set; }

}

Then also a Country class where i wrote the list of countries in the database so that i can be able to retrieve the list from the controller end..

public class Country
{
[Key]
public int Country_Id { get; set; }
public string Country_Name { get; set; }
}

and in my controller i have this:

public ActionResult Create()
{
/*Getting data from database*/
List<country> objcountrylist = (from data in db.Countries
select data).ToList();
Country objcountry = new Country();

objcountry.Country_Name = "Select";
objcountry.CountryId = 0;
objcountrylist.Insert(0, objcountry);
SelectList objmodeldata = new SelectList(objcountrylist, "Country_Id", "Country_Name", 0);
/*Assign value to model*/
Ready objcountrymodel = new Ready();

objcountrymodel.CountrySelectList = objmodeldata;

return View(objcountrymodel);
}

The Http post of the create action is what i don't know how to code inorder to get and save the selected country in the database.

Here is my view:


<div class="form-group">
@Html.LabelFor(model => model.Country, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.Country, Model.CountrySelectList, "select country now -", new { @class = "form-control", @style = "width:200px;" })
@Html.ValidationMessageFor(model => model.Country)
</div>
</div>


Please, i need your help.. i am set to create an application for like the past three months now.. but i just don't know why Visual studio has 1000 errors inside of it. Please, help me with my code. i am still a newbie in this area.

When i checked the database of "Ready", i now see: CountrySelectList_DataValueText and CountrySelectList_DataText

1 solution

why don't you query it out in and store it in the list of your table class like
List<admin> admins_CBOx = new List<admin>();
and bind it to combobox like
Combobox1.DataSource = admins;

and use selectedvalue event from combobox event

and use there methodlike

Combobox1.selectedValue = some of your variable and do what ever you want to do with it

Since you SeletedValue got everthing you need just create another valiable from a table class like

admin TempadminCBBOX = Combobox1.SelectedValue;

and you got what you selected in combobox;

Cheer!
 
Share this answer
 

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