I'm using MVC4 with Razor in my application. My controller contains C# coding and view contains cshtml codes. My dropdown list looks like this.
@Html.DropDownListFor(x => x.StateName, Model.StateList, "--Please Select--")
I want to set the selected values of list to the ViewData ponent in Controller. Let me know what are all the possibilities to do this.
I'm using MVC4 with Razor in my application. My controller contains C# coding and view contains cshtml codes. My dropdown list looks like this.
@Html.DropDownListFor(x => x.StateName, Model.StateList, "--Please Select--")
I want to set the selected values of list to the ViewData ponent in Controller. Let me know what are all the possibilities to do this.
Share Improve this question edited Mar 10, 2013 at 9:30 tereško 58.5k25 gold badges100 silver badges150 bronze badges asked Mar 8, 2013 at 10:37 Pandiyan CoolPandiyan Cool 6,5958 gold badges53 silver badges90 bronze badges2 Answers
Reset to default 3you can store the selected value in hidden field and get the hidden field value at controller like..
.cshtm file
<script language="javascript" type="text/javascript">
$(document).ready(function () {
// storing selected value to hidden field
$("#Selected").val($("#id").val());
$("#id").change(function () {
// every time dropdown changes the value will be storing in hidden field
$("#Selected").val($("#id").val());
});
)};
</script>
@Html.HiddenFor("Selected")
@Html.DropDownList("id", new SelectList(Model.StateList, "--Please Select--"))
controller...
you can read hidden field value like..
string str = base.Request["Selected"].ToString()
The second way ajax call.
<script language="javascript" type="text/javascript">
$(document).ready(function () {
// storing selected value to hidden field
$("#Selected").val($("#id").val());
$("#id").change(function () {
$.ajax({
url: "~/ ControllerName/ActionMethodName",
type: 'POST',
cache: false,
data: { Selected: $("# Selected").val() },
success: function (data) {
//
}
});
});
)};
</script>
Controller:
private string ActionMethodName (string Selected)
{
String value = Selected;
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744583125a4582156.html
评论列表(0条)