2sxc - 不能使用c#vars

时间:2017-10-15 10:30:01

标签: c# linq razor 2sxc

我尝试使用2sxc视图使用linq创建一些统计信息。但是,我似乎无法调用任何c#var。这是一个例子:

@{
    if (@Request.QueryString["ano"] == "" || @Request.QueryString["ano"] == null) {
        <p>ano not set, using current year (@DateTime.Now.Year)</p>
        var year = @DateTime.Now.Year;
        } else {
        <p>ano set (@Request.QueryString["ano"])</p>
        var year = @Request.QueryString["ano"];
    }

    var items = AsDynamic(App.Data["entity"]);
    items = items.Where(p => Convert.ToDateTime(p.data_a).Year == year);
}

这导致

error CS0103: The name 'year' does not exist in the current context

我再做一些非常愚蠢的事吗?

1 个答案:

答案 0 :(得分:2)

您在if语句中声明变量并在@{ int year = 0; if (@Request.QueryString["ano"] == "" || @Request.QueryString["ano"] == null) { <p>ano not set, using current year (@DateTime.Now.Year)</p> year = @DateTime.Now.Year; // no "var" keyword here! } else { <p>ano set (@Request.QueryString["ano"])</p> year = int.Parse(Request.QueryString["ano"]); } var items = AsDynamic(App.Data["entity"]); items = items.Where(p => Convert.ToDateTime(p.data_a).Year == year); } 之外引用它 现在你可以像这样重写你的代码:

var d = new Date();
console.log(d.toLocaleTimeString());
console.log(d.toLocaleString());
console.log(d.toLocaleDateString());