How to add a script in a partial view in MVC4?

前端 未结 9 1947
天涯浪人
天涯浪人 2020-12-05 09:15

This is the code which I have in my partial view

@model Contoso.MvcApplication.Models.Exercises.AbsoluteArithmetic

@using(Html.BeginForm())
{
9条回答
  •  天涯浪人
    2020-12-05 09:51

    There is no common solution for this issue but you can do the following simplest ways:

    1) You can create a set of extension method as the following:

    https://stackoverflow.com/a/6671766/5208058

    2) Simply move your javascript codes into a separated partial view and on your main view, render 2 partial views. One for the main partial view and the other for your scripts as the following:

    {
        // markup and razor code for Main View here
    
        @Html.Partial("Main_PartialView")
    }
    
    @section Scripts
    {
        @Html.Partial("JavaScript_PartialView")
    }
    

    Hope it helps.

提交回复
热议问题