在@using(html.BeginForm)语句之后,视图不再访问@ViewBag,我也不知道原因。我收到错误"名称' ViewBag'在当前上下文中不存在"。这是为什么?任何帮助,将不胜感激。
@{ Layout = null; }
@model IEnumerable<ReportViewModel>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<link href="StyleSheets/css" rel="stylesheet" />
<link rel="stylesheet" type="text/css" href="~/StyleSheets/css" />
<link rel="stylesheet" type="text/css" href="~/StyleSheets/css" />
<script src="@Url.Content("~/Js/utills.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Js/jQuery/jquery-1.11.0.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Js/rt.js")" type="text/javascript"></script>
<title>@ViewBag.title</title>
</head>
<body>
@*@if ((ViewBag.site != "Sites" && ViewBag.site != null) || ViewBag.site != "PrintThis")
{*@
@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "ReportForm" }))
{
if (@ViewBag.print != "PrintReport")
{
<div class="im-row im-padding-4 im-padding-top-16" id="Header" style="width: 100%;">
<div class="im-container" style="width: 95%;">
<div class="im-col--3">
@Html.DropDownList(
"OrderSelector",
new SelectList(new[]
{
new SelectListItem {Text = "L", Value = @Url.Action("Pdf", "Report/", new {SecurityCode = @ViewData["SCode"], site = @ViewBag.site, team = @ViewBag.team, sortOrder = "L"})},
new SelectListItem {Text = "R", Value = @Url.Action("Pdf", "Report/", new {SecurityCode = @ViewData["SCode"], site = @ViewBag.site, team = @ViewBag.team, sortOrder = "R"})},
new SelectListItem {Text = "D", Value = @Url.Action("Pdf", "Report/", new {SecurityCode = @ViewData["SCode"], site = @ViewBag.site, team = @ViewBag.team, sortOrder = "D"})},
new SelectListItem {Text = "T", Value = @Url.Action("tPdf", "Report/", new {SecurityCode = @ViewData["SCode"], site = @ViewBag.site, team = @ViewBag.team, sortOrder = "T"})},
}, "Value", "Text"),
new { id = "OrderSelector", @class = "sortOrder", onchange = "CallChangeFunc(this.value)" }
)
<input id="SCode" type="hidden" value="@ViewData["SCode"]" />
<input id="SelectedSort" type="hidden" value="@ViewBag.sortOrder" />
</div>
<div class="im-col--5" style="margin-top: 15px;">
<span>(to change your default sort preference, click on the 'Settings' tab)</span>
</div>
<div class="im-col--2">
@Html.ActionLink("Print", "Print", "Report", new { site = @ViewBag.site, team = @ViewBag.team, sortOrder = @ViewBag.sortOrder }, new { @class = "im-btn im-btn--primary im-btn--sm printBtn", id = "PrintPdf" })
</div>
<div class="im-row im-padding-4 im-col--12" style="width: 100%;">
<div class="im-container">
<hr />
</div>
</div>
</div>
</div>
}
}
<div class="im-container Report im-no-padding-left" style="width: 95%;" id="Report">
<div class="im-row title">
<div class="im-col--6 im-no-padding-left">
<p class="im-text--extra-bold-weight inline">
CC
</p>
<p class="inline">
- @ViewBag.status, @ViewBag.site, @ViewBag.team, @ViewBag.user
</p>
</div>
<div class="im-col--6">
<p class="im-text--extra-bold-weight inline">
@{ string[] currentDate = DateTime.Now.ToString("g").Split(' ');}
@currentDate[0] at @currentDate[1]
</p>
<p class="im-float--right im-text--color-grey im-text--extra-bold-weight inline">
@Model.Count() Rat(s)
</p>
</div>
</div>
答案 0 :(得分:1)
您应该删除@
声明中的if
字符。因为@
用于指定剃刀中的C#代码。但是,如果您启动类似if
语句的C#代码,则不应再次使用@
字符。像这样:
if (ViewBag.print != "PrintReport")
{
...
}
但如果您的声明嵌套在HTML代码中,则应使用if
启动@
声明。例如:
<div class="im-container" style="width: 95%;">
@if(ViewBag.print != "PrintReport")
{
...
}
<div class="im-col--3">
</div>
</div>
视图后不再访问@ViewBag @using(html.BeginForm)声明,我不知道为什么。
因为您刚刚使用了C#代码:
@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "ReportForm" }))
{
if (ViewBag.print != "PrintReport")
{
您现在没有添加HTML代码。因此,它让您相信您正在使用C#代码。但是,如果您开始添加HTML代码,它会实现HTML代码,之后您应该在添加新的C#代码之前添加@
。
因此,您不必在C#语句中使用@
,但必须在将C#代码嵌套在HTML代码中的地方使用它。像这样:
<p class="inline">
- @ViewBag.status, @ViewBag.site, @ViewBag.team, @ViewBag.user
</p>
最后,对于您的代码示例,您应该更改它:
<body>
@*@if ((ViewBag.site != "Sites" && ViewBag.site != null) || ViewBag.site != "PrintThis")
{*@
@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "ReportForm" }))
{
if (@ViewBag.print != "PrintReport")
{
<div class="im-row im-padding-4 im-padding-top-16" id="Header" style="width: 100%;">
<div class="im-container" style="width: 95%;">
<div class="im-col--3">
@Html.DropDownList(
"OrderSelector",
new SelectList(new[]
{
new SelectListItem {Text = "L", Value = Url.Action("Pdf", "Report/", new {SecurityCode = ViewData["SCode"], site = ViewBag.site, team = ViewBag.team, sortOrder = "L"})},
new SelectListItem {Text = "R", Value = Url.Action("Pdf", "Report/", new {SecurityCode = ViewData["SCode"], site = ViewBag.site, team = ViewBag.team, sortOrder = "R"})},
new SelectListItem {Text = "D", Value = Url.Action("Pdf", "Report/", new {SecurityCode = ViewData["SCode"], site = ViewBag.site, team = ViewBag.team, sortOrder = "D"})},
new SelectListItem {Text = "T", Value = Url.Action("tPdf", "Report/", new {SecurityCode = ViewData["SCode"], site = ViewBag.site, team = ViewBag.team, sortOrder = "T"})},
}, "Value", "Text"),
new { id = "OrderSelector", @class = "sortOrder", onchange = "CallChangeFunc(this.value)" }
)
<input id="SCode" type="hidden" value="@ViewData["SCode"]" />
<input id="SelectedSort" type="hidden" value="@ViewBag.sortOrder" />
</div>
<div class="im-col--5" style="margin-top: 15px;">
<span>(to change your default sort preference, click on the 'Settings' tab)</span>
</div>
<div class="im-col--2">
@Html.ActionLink("Print", "Print", "Report", new { site = ViewBag.site, team = ViewBag.team, sortOrder = ViewBag.sortOrder }, new { @class = "im-btn im-btn--primary im-btn--sm printBtn", id = "PrintPdf" })
</div>
<div class="im-row im-padding-4 im-col--12" style="width: 100%;">
<div class="im-container">
<hr />
</div>
</div>
</div>
</div>
}
}
<div class="im-container Report im-no-padding-left" style="width: 95%;" id="Report">
<div class="im-row title">
<div class="im-col--6 im-no-padding-left">
<p class="im-text--extra-bold-weight inline">
CC
</p>
<p class="inline">
- @ViewBag.status, @ViewBag.site, @ViewBag.team, @ViewBag.user
</p>
</div>
<div class="im-col--6">
<p class="im-text--extra-bold-weight inline">
@{ string[] currentDate = DateTime.Now.ToString("g").Split(' ');}
@currentDate[0] at @currentDate[1]
</p>
<p class="im-float--right im-text--color-grey im-text--extra-bold-weight inline">
@Model.Count() Rat(s)
</p>
</div>
</div>
</div>
</body>