[RESOLVED]How to save dropdown selected text into database using EF In MVC4?

Hi Friends ,

           I would like to know how to save  Dropdown selected text  and Checkbox checked value in data base using EF.Can anyone provide example Code ?

Thx in advance 

-Ernesto Karthik

 

HI  Dharmesh ,

                       I have  binded the dropdown values in the dropdown. i would like to save the selected Text in the dropdown in another table.How to do that using EF 

Thx ,

-Ernesto Karthik

 

 

Hi Karthik ,

I will suggest use Begin form in your view , so you can pass selected values to your controller/ action. Once you get this value in  Post action, you can call EF method or DAL method.

For example :

@using (Html.BeginForm("ActionName", "ControllerName", FormMethod.Post))
    {

/// Dropdown 1

/// Dropdown 2
<input type="submit" value="Html PsBk Click" /> }

Now in "ActionName" you need to write code to read this param.

[HttpPost]
        public ActionResult ActioName(PersonnelModel Pmodel)
        {
   // Read model data from this param ex. Pmodel.dropdown1 // Pmodel.dropdown2         
----
-- }

For more details : http://msdn.microsoft.com/en-us/library/dd505244(v=vs.118).aspx

1. Entity class must have properties to hold the text and value

2. In View, keep hidden with name of above text property

3. In View, keep dropdown for above value property

3. Update the hidden value wit dropdown selecteditem on selection of dropdown at client side

then the text also gets posted

EF creates POCO classes of your database.  MVC allows you to send those objects to the user to edit and post.  In your controller you can take an instance of that class like this:

public ActionResult SavedPerson(Person person){
int rowssaved = 0;
//first see if model state is valid
if (ModelState.IsValid){

using (var db = new myEFEntities()){
   db.Person.Add(person);
   rowsaved=db.SaveChanges();
}
var results = new VMSavedPerson(person, rowsaved);
return View(SavedPerson);
}

Leave a Reply