简体   繁体   English

将C#Razor语法与jQuery合并

[英]Merge C# Razor syntax with jQuery

I'm working on a project using C# razor engine and I wondering if it is possible to merge razor syntax with jQuery. 我正在使用C#剃刀引擎进行项目开发,我想知道是否可以将jQuery剃刀语法合并。 Is that possible? 那可能吗?

An approach I have done is something like this: 我做的一种方法是这样的:

<script>
    $(document).ready(function() {
        @Html.Action("GoNext", "Actions", new { Id= ViewBag.Id, justifymessage = @:$("#msg").val(), Action = 3 })
    });
</script>

Razor is server side while jQuery is client side. Razor是服务器端,而jQuery是客户端。 You can merge them... You may only write a razor code inside jQuery but razor will render it result before jQuery. 您可以合并它们。您可以只在jQuery中编写剃刀代码,但是剃刀将在jQuery之前呈现它的结果。

To call an action from server, use Ajax. 要从服务器调用动作,请使用Ajax。 See what you can do 看看你能做什么

 <script>
   $(document).ready(function() {
      var url="@Url.Action("GoNext","Actions", new {Id= ViewBag.Id, Action = 3 })";
      url+="&justifymessage = "+$("#msg").val();
      jQuery.get(url).done(function(htmls){
            jQuery('#appendable').html(htmls);
       });
   });
 </script>
 <div id='appendable'></div>

if say, you want to redirect to that url try: 如果说,您想重定向到该URL,请尝试:

<script>
$(document).ready(function() {
    var yoururl = "@Url.Action("GoNext", "Actions", new { Id= ViewBag.Id, justifymessage = "#param#", Action = 3 })";

    location.href=yoururl.replace("#param#", $("#msg").val());
});
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM