Click here to Skip to main content
15,883,883 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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" });
}
Posted

hi,
i would suggest you to use html helper method like:
<%=Html.DropDownListFor(x=>x.SelectedDateFormat,(SelectList)(ViewData["list"])) %>

because you would not be able to find the selected data of select - option dropdownlist on post.it will be lost on post.
 
Share this answer
 
Hello

if Your Return Str is not empty and it may return value


than simply USed in Your Success block

C#
if (data.status == "sucess") {
$("#department").html(data.department);

}


first you check what it will return using alert
alert(data.department);



I hope Your Problem Will Solve If not Please give me your Review.
 
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