保存下拉选择的值mvc2实体框架 [英] save the dropdown selected value mvc2 entity framework

查看:106
本文介绍了保存下拉选择的值mvc2实体框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友们,我正在使用Entity Framework在MVC2中制作一个新的Web应用程序.这是我在MVC中的第一个应用程序.我有一个表名 tblState,其中有3列staId Int,staName Varchar(200),cntId Int

这是我的代码,用于在Homecontroller中保存状态..

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..

[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 :

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



查看:



View :

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



但是如何保存下拉列表的选定值,以在选定国家/地区下保存新州.

请帮帮我.

谢谢&问候
Parveen Rathi



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

推荐答案

传递与下拉菜单名称相同的参数
Pass a parameter with the same name as your dropdown name
[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();
            }

        }


这篇关于保存下拉选择的值mvc2实体框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆