admin管理员组文章数量:1399823
I have an html link in one file
<a href="#" onClick="xupdate('Home')" id="padlink">Home</a>
On click I want a js function(in another file, with extension js) to execute and that is:
function xupdate(string) {
document.title = string;
//Call razor c# function
}
Now I have a c# function(it reads files and displays their information) in a cshtml file:
@helper fileRead(String file) {
var dataFile = Server.MapPath(file);
Array userData = File.ReadAllLines(dataFile);
foreach (string dataLine in userData) {
foreach (string dataItem in dataLine.Split(',')) {
//dataItem <text> </text>
@Html.Raw(dataItem);
}
}
}
I want to call the fileRead function from the js xupdate() function and send the value of string into fileRead as a parameter.Is there a way to do this?
Note: I have already included the html link in the cshtml file and my functions work perfectly. Also I know that I have to include a file extension when calling the c# function.
I have an html link in one file
<a href="#" onClick="xupdate('Home')" id="padlink">Home</a>
On click I want a js function(in another file, with extension js) to execute and that is:
function xupdate(string) {
document.title = string;
//Call razor c# function
}
Now I have a c# function(it reads files and displays their information) in a cshtml file:
@helper fileRead(String file) {
var dataFile = Server.MapPath(file);
Array userData = File.ReadAllLines(dataFile);
foreach (string dataLine in userData) {
foreach (string dataItem in dataLine.Split(',')) {
//dataItem <text> </text>
@Html.Raw(dataItem);
}
}
}
I want to call the fileRead function from the js xupdate() function and send the value of string into fileRead as a parameter.Is there a way to do this?
Note: I have already included the html link in the cshtml file and my functions work perfectly. Also I know that I have to include a file extension when calling the c# function.
Share Improve this question asked Dec 24, 2014 at 5:26 HzakvHzakv 491 gold badge1 silver badge6 bronze badges3 Answers
Reset to default 2You can not call C# function from javascript directly, Because javascript execute on client side and C# function execute at server side.
So you must call it other way like AJAX.
Define your function in controller and call it via AJAX call.
Use AJAX:
var ret = null;
$.ajax({
async: false,
url: "YourFunctionName_in_Controller",
dataType: "json",
success: function (data) {ret = data;}
});
return ret;
DotNet.invokeMethodAsync('{ASSEMBLY NAME}', '{.NET METHOD ID}', {ARGUMENTS});
https://learn.microsoft./en-us/aspnet/core/blazor/javascript-interoperability/call-dotnet-from-javascript?view=aspnetcore-6.0
本文标签: Call razor c function from javascript functionStack Overflow
版权声明:本文标题:Call razor c# function from javascript function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744197321a2594801.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论