简体   繁体   中英

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. 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. You can merge them... You may only write a razor code inside jQuery but razor will render it result before jQuery.

To call an action from server, use 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:

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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