c# - How to send a dropdown list selected value from MVC view to controller's variable ViewData? - Stack Overflow

I'm using MVC4 with Razor in my application. My controller contains C# coding and view contains cs

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 badges
Add a ment  | 

2 Answers 2

Reset to default 3

you 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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信