How to Pass javascript variable to Controller in MVC from View

前端 未结 1 1781
清歌不尽
清歌不尽 2021-01-07 13:52

all I want to pass one javascript variable to Controller from MVC View from Form. This is my own selected text variable which I want to pass to controller so that I can retr

相关标签:
1条回答
  • 2021-01-07 14:28

    You could set up a hidden field in your form for the state (I assume state is a property on your RegisterModel?) and in your stateID.change function have it assigned the value to the hidden field for it to be passed on submit.

    e.g:

    Hidden field in the form can be set up with (inside your begin form code):

    @Html.HiddenFor(model => model.StateID, new { id = hiddenStateID" })

    Then your jQuery would be (untested, this is more pseduo code to give you the idea):

    $(document).ready(function () 
     {  
            $("#stateID").change(function (){
                 var state= $('#stateID :selected').text();
                 getCities(state);
                 $("#hiddenStateId").val(state);
            });
     });
    
    0 讨论(0)
提交回复
热议问题