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

前端 未结 9 1946
天涯浪人
天涯浪人 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 10:05

    This worked for me allowing me to colocate JavaScript and HTML for partial view in same file for ease of readability

    In View which uses Partial View called "_MyPartialView.cshtml"

    @Html.Partial("_MyPartialView",< model for partial view>, new ViewDataDictionary { { "Region", "HTMLSection" } } })
    @section scripts{ @Html.Partial("_MyPartialView",, new ViewDataDictionary { { "Region", "ScriptSection" } }) }

    In Partial View file

    @model SomeType
    
    @{
        var region = ViewData["Region"] as string;
    }
    
    @if (region == "HTMLSection")
    {
    
    
    }
    
    @if (region == "ScriptSection")
    {
            
    
                                     
                  
提交回复
热议问题