MVC4中的下拉绑定 [英] Dropdown binding in MVC4

查看:72
本文介绍了MVC4中的下拉绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我在我的视图中添加了一个下拉列表并从数据库中绑定了..

现在我想在控制器中获取所选值我怎么能得到它?

以下是我的观点和控制器

查看:

< script>

$(文件)。 ready(function(){

$ .post(/ User / GetDepartment,{actionname:GetDepartment},function(data){

var sd = ;

if(data.status ==sucess){

$(#department)。append(data.department);



}

},json)。成功(function(){



var sd =;

});



});



< / script>

< select id =department>< / select>





控制器:

public JsonResult GetDepartment()

{



SqlConnection con = new SqlConnection(connStr);

SqlDataAdapter cmd = new SqlDataAdapter(select * from [Department],con);

DataSet ds = new DataSet() ;

cmd.Fill(ds);



var str =;

for(int i = 0;我< ds.Tables [0] .Rows.Count; i ++)

{

str + =< option> + ds.Tables [0] .Rows [i] [1] .ToString()+< / option>;



}



返回Json(新{department = str,status =sucess});

}

Hello,
I have added a dropdown in my view and binded from database ..
now i want to get the selected value in controller how can i get that?
below is my view and controller
View :
<script>
$(document).ready(function () {
$.post("/User/GetDepartment", { actionname: "GetDepartment" }, function (data) {
var sd = "";
if (data.status == "sucess") {
$("#department").append(data.department);

}
}, "json").success(function () {

var sd = "";
});

});

</script>
<select id="department"></select>


Controller:
public JsonResult GetDepartment()
{

SqlConnection con = new SqlConnection(connStr);
SqlDataAdapter cmd = new SqlDataAdapter("select * from [Department]", con);
DataSet ds = new DataSet();
cmd.Fill(ds);

var str = "";
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
str += "<option>" + ds.Tables[0].Rows[i][1].ToString() + "</option>";

}

return Json(new { department = str, status = "sucess" });
}

推荐答案

(document).ready(function(){
(document).ready(function () {


.post(/ User / GetDepartment,{actionname:GetDepartment},function(data) ){

var sd =;

if(data.status ==sucess){
.post("/User/GetDepartment", { actionname: "GetDepartment" }, function (data) {
var sd = "";
if (data.status == "sucess") {


(#department)。append(data.department);



}

},json)。成功(功能) ()


var sd =;

});



});



< / script>

< select id =department>< / select> < br $>




控制器:

public JsonResult GetDepartment()

{



SqlConnection con = new SqlConnection(connStr);

SqlDataAdapter cmd = new SqlDataAdapter(select * from [Department],con);

DataSet ds = new DataSet() ;

cmd.Fill(ds);



var str =;

for(int i = 0;我< ds.Tables [0] .Rows.Count; i ++)

{

str + =< option> + ds.Tables [0] .Rows [i] [1] .ToString()+< / option>;



}



返回Json(新{department = str,status =sucess});

}
("#department").append(data.department);

}
}, "json").success(function () {

var sd = "";
});

});

</script>
<select id="department"></select>


Controller:
public JsonResult GetDepartment()
{

SqlConnection con = new SqlConnection(connStr);
SqlDataAdapter cmd = new SqlDataAdapter("select * from [Department]", con);
DataSet ds = new DataSet();
cmd.Fill(ds);

var str = "";
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
str += "<option>" + ds.Tables[0].Rows[i][1].ToString() + "</option>";

}

return Json(new { department = str, status = "sucess" });
}


这篇关于MVC4中的下拉绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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