使用@ Html.Partial Asp.Net MVC将变量从视图传递到部分视图

时间:2019-01-30 08:36:59

标签: c# asp.net-mvc partial-views

我有这样的代码:

<div id="SettingsTabPanel" class="tabbable-line" style="padding-top:20px;">
 <div id="ProjectTimeLine" class="tab-pane">
   @{ var test = 10;}
   @test
 </div>
  @Html.Partial("~/Views/PortfolioResearch/Partials/_LabUtilization.cshtml")
</div>

_LabUtilization.cshtml中,我将变量测试称为@test。但是有错误,因为@test未定义。我应该怎么做才能在test中调用变量_LabUtilization.cshtml

1 个答案:

答案 0 :(得分:0)

使用某种模型向部分模型发送值

//your model
public class SomeModel
{
   public string test {get;set;}
}

//you view
@model SomeModel
<div id="SettingsTabPanel" class="tabbable-line" style="padding-top:20px;">
 <div id="ProjectTimeLine" class="tab-pane">
   @{ var test = 10;
Model.test = test;
}
   @test
 </div>
  @Html.Partial("~/Views/PortfolioResearch/Partials/_LabUtilization.cshtml", Model)
</div>


//your _LabUtilization partial

@model SomeModel

<span> @Model.test</span>