从ASP.Net MVC中的Dropdownlist访问值 [英] Accessing values from Dropdownlist in ASP.Net MVC

查看:64
本文介绍了从ASP.Net MVC中的Dropdownlist访问值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

请帮我如何从下拉列表中访问所选值,并在Asp.net MVC中将值插入数据库



在控制器中:

Hi Guys,
please help me how to access selected value from the dropdownlist and to insert value into database in Asp.net MVC

In controller:

List<int> li = new List<int>();
            for (int i = 20; i < 50; i++)
            {
                li.Add(i);

            }
            ViewData["j"] = new SelectList(li);



在视图中


In view

<%=Html.LabelFor(Model=>Model.age) %>
                <%=Html.DropDownList("j")%>



在模型中


In model

public class details
{
[DisplayName("Age")]
        public int age { get; set; }

}

推荐答案

您始终可以从HttpPost的Request对象获取数据

例如。

You can always get the data from Request object at HttpPost
eg.
var value1 = Request["SimpleProp1"];
var value2 = Request["SimpleProp2"];
var value3 = Request["ComplexProp1.SimpleProp1"];


<![CDATA[<%=Html.DropDownList(Model=>Model.age)%>]]>










public Actionview PostAction(Model model)
{
    //selected combobox value
    int age = model.age;
}


In View You have to do something like this




@Html.DropdownListFor(Model=>Model.age,new SelectListItem{@ViewBag.j,DataFieldId,DataFieldValue},"Select Text");










i have shown you genral format,use like this. This will surely help you.


这篇关于从ASP.Net MVC中的Dropdownlist访问值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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