Click here to Skip to main content
15,849,594 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
Hi.. friends, I'm making a new web application in MVC2 using Entity Framework. This is my first application in MVC. I have a table name tblState having 3 column staId Int, staName Varchar(200), cntId Int

This is my code to save state in Homecontroller..
C#
[HttpPost]
       public ActionResult addstate(tblState state)
       {
           try
           {
               entites.AddTotblState(state);
               entites.SaveChanges();
               return RedirectToAction("states");
           }
           catch
           {
               return View();
           }

       }


Here is Dropdown List in my view. The code for bind drop down is

Home controller :
C#
ViewData["name"] = (from n in entites.tblCountry
                         select new { n.cntId,n.cntName}).ToList();


View :

HTML
<%=Html.DropDownList("name", new SelectList((IEnumerable)ViewData["name"], "cntId", "cntName"))%>


But how to save the selected value of Dropdown List to save a new State under selected Country.

Please help me.

Thanks & Regards
Parveen Rathi
Posted

1 solution

Pass a parameter with the same name as your dropdown name
SQL
[HttpPost]
        public ActionResult addstate(string name, tblState state)
        {
            try
            {
                state.cntId=Convert.ToInt32(name);
                entites.AddTotblState(state);
                entites.SaveChanges();
                return RedirectToAction("states");
            }
            catch
            {
                return View();
            }

        }
 
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