Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: MVC
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 14-Jan-13 20:34pm
PftADev317
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hello
 
if Your Return Str is not empty and it may return <option>value</option>
 

than simply USed in Your Success block
 
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.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 195
1 Afzaal Ahmad Zeeshan 132
2 ProgramFOX 130
3 Maciej Los 115
4 Sergey Alexandrovich Kryukov 75
0 OriginalGriff 6,564
1 Sergey Alexandrovich Kryukov 6,048
2 DamithSL 5,228
3 Manas Bhardwaj 4,657
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web02 | 2.8.1411022.1 | Last Updated 18 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100