【发布时间】:2014-06-03 21:40:24
【问题描述】:
我在asp.net-mvc4 应用程序中有一个view,它可以访问来自controller 和model 的数据。
@model IEnumerable<ViewExampleDatabase4.Models.Messung>
@section Scripts{
@*@Scripts.Render("~/bundles/jqueryval")*@
<script type="text/javascript">
$(document).ready(function () {
$(".datefield").datepicker();
});
</script>
}
<table style="background-color: #009963; font-weight: lighter; font-size: 15px;
text-align: center;" width="740" height="20" cellspacing="2" cellpadding="5" border="1">
<tr>
<td style="background-color: #009963; color: #FFFFFF" colspan="3">
<b> Messungen für Fahrzeug : @Html.Raw(ViewBag.Fahrzeugname) </b>
</td>
</tr>
</table>
@using (Html.BeginForm("ForFahrzeug", "Messungen", FormMethod.Get))
{
<!--Messungsart: @Html.DropDownList("Total")-->
<P>Messungen der letzten Tage: @Html.TextBox("days", "30", new { style =
"width:5%" }) Oder Messungen begin von:
@Html.TextBox("initDate" ,"", new { style = "width:15%", @class = "datefield" })
und Ende:
@Html.TextBox("finalDate" ,"", new { style = "width:15%", @class = "datefield" })
<input type="submit" value="Filter" /> </p>
}
<table>
var total = 0;
@foreach (var item in Model)
{
total = total + item.t_Gesamtzeit_s_msg;
if(Model.Last()==item)
{
<table style="background-color: #009963; font-weight: lighter; font-size: 12px;
text-align: center; " width="740" height="30" cellspacing="2" cellpadding="5">
<tr>
<td style="background-color: #009963; color: #FFFFFF" colspan="3"> Summary </td>
</tr>
<tr style = "border:1px solid #FFFFFF">
<td style="background-color: #CCCCCC; color: #000000; border:1px solid #FFFFFF">
<b>Gesamtzeit</b></td>
<td style="background-color: #CCCCCC; color: #000000; border:1px solid
#FFFFFF">@Html.Raw(total)</td>
<td style="background-color: #CCCCCC; color: #000000; border:1px solid
#FFFFFF">Messende</td>
<td style="background-color: #CCCCCC; color: #000000; border:1px solid #FFFFFF"></td>
</tr>
</table>
}
}
</table>
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Messbeginn)
</th>
<th>
@Html.DisplayNameFor(model => model.Messende)
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td width="8%">
@Html.ActionLink(item.Name, "MessungsDetail", "Messungen", new { id = item.ID },
null)
</td>
<!--<td width="35%">
@Html.DisplayFor(modelItem => item.Beschreibung)
</td>-->
<td width="10%">
@Html.DisplayFor(modelItem => item.Messbeginn)
</td>
<td width="8%">
@Html.DisplayFor(modelItem => item.Messende)
</td>
<!--<td width="10%">
@Html.ActionLink("Download", "ForDownload", "Messungen", new { id = item.ID },
null)
</td>-->
</tr>
}
</table>
我已经声明了一个变量total。但是当我将它用于增量时。它给出了错误。我需要一个变量来使用。
【问题讨论】:
-
不要把这样的逻辑放在视图中,它不属于那里。在模型上为总计创建一个属性,并在将总计传递给视图之前计算总计。还将脚本标签添加到页面底部,以便加载更快
-
可能重复:你试过这个:stackoverflow.com/questions/6069213/… ?
-
@{ c# code block } 所有变量都应该在这个块中声明 aur simple @total.
标签: c# html asp.net-mvc-4 razor