Zulqadar Idrishi

Zulqadar Idrishi

  • NA
  • 471
  • 71.8k

Is it bad practice to use JavaScript with Razor Syntax in MVC?

Nov 3 2020 2:39 AM
Hey everyone,
 
Today, One of the developers of my organization said "It is a bad practice to use JavaScript with Razor Syntax(c#) in Asp.Net MVC project."
 
This topic takes lots of discussions but didn't get to any final result. What do you say about it?
 
Here is the following code that I have used in my Asp.Net MVC Project. 
 
//This is my view.cshtml page 
 
@if (ViewBag.ResponseMessage != null)
{
   string ResponseType = @String.IsNullOrEmpty(ViewBag.ResponseType) ? "info" : ViewBag.ResponseType;
   string ResponseTitle = @String.IsNullOrEmpty(ViewBag.ResponseTitle) ? "" : ViewBag.ResponseTitle;
   <script>
      $(document).ready(function () {
         swal('@ViewBag.ResponseMessage', '@ResponseTitle', "@ResponseType").then(function () {
               @if (!string.IsNullOrEmpty(ViewBag.ResponseURL))
                     {
                        @:window.location.href = "@ViewBag.ResponseURL";
                     }
          })
      })
   </script>
}

Answers (1)