【问题标题】:How to declare a variable in view如何在视图中声明变量
【发布时间】:2014-06-03 21:40:24
【问题描述】:

我在asp.net-mvc4 应用程序中有一个view,它可以访问来自controllermodel 的数据。

@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


【解决方案1】:

你需要声明一个代码块:

@{ var total = 0; }

因为您在此之前有“HTML 标记”,所以它不像 total = total + item.t_Gesamtzeit_s_msg;@foreach 之后那样“自动检测”。

但是正如 cmets 中已经说过的:这应该由模型而不是视图来处理。

AND我还建议只使用英文名称,因为如果以后“非德语使用者”应该维护代码,可能会导致可维护性问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-04
    • 1970-01-01
    • 1970-01-01
    • 2017-04-10
    • 2021-03-15
    • 2018-11-08
    • 2011-02-09
    相关资源
    最近更新 更多