码迷,mamicode.com
首页 > Web开发 > 详细

MVC dropdownlist使用

时间:2015-07-29 11:45:47      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:

View中代码

@{
    ViewBag.Title = "dropdownlist";
}

<h2>dropdownlist</h2>
@using (Html.BeginForm())
{
    <table>
        <tr>
            <td>test1</td>
            <td>
                <input type="radio" id="GenderM" name="Sex" value="" /><input type="radio" id="GenderF" name="Sex" value="" checked /><input type="text" id="1" name="1" value="1">
            </td>
        </tr>
        <tr>
            <td>test2</td>
            <td>
                @Html.DropDownList("name", ViewBag.selList1 as IEnumerable<SelectListItem>)
            </td>
        </tr>
    </table>

    <input type="submit" value="submit">

Control中代码 

public ActionResult dropdownlist()
        {
            var q = from p in modeltest.Students select p;
            List<student> droptest = q.ToList();
            SelectList selList1 = new SelectList(droptest,"id","name");
            ViewBag.selList1 = selList1.AsEnumerable();  //选择数据库数据绑定
            return View();
        }
        
        [HttpPost]
        public ActionResult dropdownlist(string test="test")
        {
            var q = from p in modeltest.Students select p;
            List<student> droptest = q.ToList();
            ViewData["name"] = new SelectList(droptest,"id","name");     //重构dropdownlist
            var name = modeltest.Students.Find(Convert.ToInt32(Request.Form["name"]));
            formtest fm = new formtest { id = Convert.ToInt32(Request.Form["1"]), value1 = Request.Form["sex"], value2 = name.name };  //通过view中的数据给model对象赋值
            modeltest.formtest.Add(fm);
            modeltest.SaveChanges();
            return View(fm);
        }

 

MVC dropdownlist使用

标签:

原文地址:http://www.cnblogs.com/lovejunjuan/p/4685433.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!