admin管理员组文章数量:1331083
I want to use a javascript variable to pass as a parameter to my class constructor in C#.
How can I translate the javascript variable ID to C# such that I am able to pass the value on User.IsOnLeave
?
<script type="text/javascript">
var ID;
var dateToEvaluate;
function convertVariable() {
*if (User.IsOnLeave(***ID***, dateToEvaluate)) {...}*
}
</script>
I want to use a javascript variable to pass as a parameter to my class constructor in C#.
How can I translate the javascript variable ID to C# such that I am able to pass the value on User.IsOnLeave
?
<script type="text/javascript">
var ID;
var dateToEvaluate;
function convertVariable() {
*if (User.IsOnLeave(***ID***, dateToEvaluate)) {...}*
}
</script>
Share
Improve this question
edited Jan 18, 2020 at 1:17
Horai Nuri
5,57817 gold badges84 silver badges136 bronze badges
asked Oct 13, 2011 at 8:23
learninglearning
11.7k36 gold badges89 silver badges156 bronze badges
3 Answers
Reset to default 5You can't access JS variables directly from C#, because JS is client-side and C# is server-side. You can make a controller action and make an AJAX request to it with those parameters. Like this:
JS:
var id;
var dataToEvaluate;
jQuery.ajax({
type: 'POST',
url: 'SomeController/SomeAction',
data: { id: id, dataToEvaluate: dataToEvaluate },
success: function(data) {
// do what you have to do with the result of the action
}
});
controller:
public ActionResult SomeAction(string id, string dataToEvaluate)
{
// some processing here
return <probably a JsonResult or something that fits your needs>
}
One of the way (IMO, the only way) of working with your C# code inside your JavaScript code is to make Ajax
calls.
jQuery.ajax() is a good choice.
The easiest option is to just render the value to a hidden textbox or dom element, then have javascript access the field.
For example
<input type="hidden" value="set from c#" id="myValue" />
in javascript
var dateToEvaluate = document.getElemenetById("myValue").value;
Or if you Javascript is in the same file as your HTML. You could just say in javascript:
var dateToEvaluate = @myValue;
assuming razor syntax
本文标签: aspnet mvcconvert javascript variable to C variableStack Overflow
版权声明:本文标题:asp.net mvc - convert javascript variable to C# variable - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742271786a2444441.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论