开发者

how to read javascripts values and send them as c# variable

开发者 https://www.devze.com 2023-03-04 18:15 出处:网络
I have a dropdown as follows: @Html.DropDownList(\"studentList\", myModel.GetMyList()) I have a variable that stores the selected value in javeascript.

I have a dropdown as follows:

 @Html.DropDownList("studentList", myModel.GetMyList())

I have a variable that stores the selected value in javeascript.

 userID = $("#studentList> option:selected").attr("value")

However, I need to send the seleted value in c# to my controller. How can I achive that?

How do I read the selected value from the dropdown in c# or how can I "translate" 开发者_开发问答the javascript value in c# varaiable?


It's "Simple", Create a HiddenField, and store the selected values in that, before post your data to server.

to get the selected values try something like that:

var myValues= new Array();
$("#studentList> option:selected").each(function(){
    myValues.push($(this).attr('value'));
})

$('#hiddenField').attr('value', myValues.join(','));

On then server-side, just have to split the string to get the values


Try putting the value in a hidden field via JS

<%=Html.Hidden("student", "none")%>

Then access the value of the hidden field from c#


Your combobox selected value should already be sent over to the server as long as its in the form. Is there a reason that isn't sufficient?

0

精彩评论

暂无评论...
验证码 换一张
取 消