Using Razor within JavaScript

后端 未结 12 2572
萌比男神i
萌比男神i 2020-11-22 04:12

Is it possible or is there a workaround to use Razor syntax within JavaScript that is in a view (cshtml)?

I am trying to add markers to a Google map...

12条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-22 04:48

    The following solution seems more accurate to me than combine JavaScript with Razor. Check this out: https://github.com/brooklynDev/NGon

    You can add almost any complex data to ViewBag.Ngon and access it in JavaScript

    In the controller:

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            var person = new Person { FirstName = "John", LastName = "Doe", Age = 30 };
            ViewBag.NGon.Person = person;
            return View();
        }
    }
    

    In JavaScript:

    
    

    It's allows any plain old CLR objects (POCOs) that can be serialized using the default JavascriptSerializer.

提交回复
热议问题