请求排查Razor模板中FDate系列变量的声明错误
问题分析与修正方案
首先,你的变量声明存在语法错误,获取日期字段的逻辑也有问题,我们一步步梳理解决:
1. 核心错误:变量声明的语法问题
你当前的FDate1声明代码:
@{ string FDate1 = var row in AME.GridF18.FDate1("SubmissionID='" + TknParams.SubmissionID + "'"); }
这完全不符合C#语法——var row in ...是foreach循环的迭代语法,不能直接用来赋值给字符串变量。而且从你后续的循环代码来看,FDate1-FDate8应该是AME.GridF18("SubmissionID=...")返回数据行里的字段,而非单独调用FDate1()方法。
2. 正确的变量声明方式
首先获取指定SubmissionID的数据集(按业务逻辑,这个查询大概率只返回一行数据),再从结果中安全提取各个日期字段:
@{ // 获取查询结果的第一行,避免无数据时抛出异常 var submissionRow = AME.GridF18("SubmissionID='" + TknParams.SubmissionID + "'").FirstOrDefault(); // 空值保护:如果无数据则赋值为空字符串,防止页面报错 string FDate1 = submissionRow?.FDate1 ?? string.Empty; string FDate2 = submissionRow?.FDate2 ?? string.Empty; string FDate3 = submissionRow?.FDate3 ?? string.Empty; string FDate4 = submissionRow?.FDate4 ?? string.Empty; string FDate5 = submissionRow?.FDate5 ?? string.Empty; string FDate6 = submissionRow?.FDate6 ?? string.Empty; string FDate7 = submissionRow?.FDate7 ?? string.Empty; string FDate8 = submissionRow?.FDate8 ?? string.Empty; }
FirstOrDefault()确保查询不到数据时返回null而非抛出异常;?.空传播运算符+??空合并运算符,保证即使数据为空,变量也能拿到合法的空字符串,避免页面渲染失败。
3. 额外的HTML语法错误
你的HTML代码里在@FDate8对应的</th>后面多了一个多余的},必须删除,否则会导致页面结构异常:
<!-- 错误代码 --> <th colspan="1" scope="col">@FDate8</th> } <!-- 修正后 --> <th colspan="1" scope="col">@FDate8</th>
修正后的完整代码片段
@{ var submissionRow = AME.GridF18("SubmissionID='" + TknParams.SubmissionID + "'").FirstOrDefault(); string FDate1 = submissionRow?.FDate1 ?? string.Empty; string FDate2 = submissionRow?.FDate2 ?? string.Empty; string FDate3 = submissionRow?.FDate3 ?? string.Empty; string FDate4 = submissionRow?.FDate4 ?? string.Empty; string FDate5 = submissionRow?.FDate5 ?? string.Empty; string FDate6 = submissionRow?.FDate6 ?? string.Empty; string FDate7 = submissionRow?.FDate7 ?? string.Empty; string FDate8 = submissionRow?.FDate8 ?? string.Empty; } <div class="table-responsive"> <table class="table table-striped table-condensed table-bordered" width="100%" border="0"> <tbody> <tr> <th colspan="7" scope="col">@FDate1</th> <th colspan="2" scope="col">@FDate2</th> <th colspan="2" scope="col">@FDate3</th> <th colspan="2" scope="col">@FDate4</th> <th colspan="2" scope="col">@FDate5</th> <th colspan="2" scope="col">@FDate6</th> <th colspan="1" scope="col">@FDate7</th> <th colspan="1" scope="col">@FDate8</th> </tr> <tr> <th rowspan="2" scope="col">Project #</th> <th rowspan="2" scope="col">PW</th> <th rowspan="2" scope="col">Project Name</th> <th rowspan="2" scope="col">Type of Work</th> <th rowspan="2" scope="col">Mile</th> <th rowspan="2" scope="col">Toll</th> <th rowspan="2" scope="col">Park</th> <th colspan="2" scope="col">Monday</th> <th colspan="2" scope="col">Tuesday</th> <th colspan="2" scope="col">Wednesday</th> <th colspan="2" scope="col">Thursday</th> <th colspan="2" scope="col">Friday</th> <th scope="col">Saturday</th> <th scope="col">Sunday</th> <th scope="col">Total</th> </tr> <tr> <th scope="col">RT</th> <th scope="col">OT</th> <th scope="col">RT</th> <th scope="col">OT</th> <th scope="col">RT</th> <th scope="col">OT</th> <th scope="col">RT</th> <th scope="col">OT</th> <th scope="col">RT</th> <th scope="col">OT</th> <th scope="col"> </th> <th scope="col"> </th> <th scope="col"> </th> </tr> @foreach (var row in AME.GridF18("SubmissionID='" + TknParams.SubmissionID + "'")) { <tr> <td>@row.ProjectNumber</td> <td><input id="checkBox" type="checkbox"></td> <td>@row.ProjectName</td> <td>@row.TypeofWork</td> <td>@row.Mile</td> <td>@row.Toll</td> <td>@row.Park</td> <td>@row.MonRT</td> <td>@row.MonOT</td> <td>@row.TuesRT</td> <td>@row.TuesOT</td> <td>@row.WedsRT</td> <td>@row.WedsOT</td> <td>@row.ThursRT</td> <td>@row.ThursOT</td> <td>@row.FriRT</td> <td>@row.FriOT</td> <td>@row.Saturday</td> <td>@row.Sunday</td> <td>@row.Total</td> </tr> } </tbody> </table> </div>
内容的提问来源于stack exchange,提问作者UserSN
相关产品推荐
相关产品推荐

