当前位置 博文首页 > 文章内容

    Razor语法 if嵌套foreach再嵌套if

    作者: 栏目:未分类 时间:2020-06-30 10:03:07

    本站于2023年9月4日。收到“大连君*****咨询有限公司”通知
    说我们IIS7站长博客,有一篇博文用了他们的图片。
    要求我们给他们一张图片6000元。要不然法院告我们

    为避免不必要的麻烦,IIS7站长博客,全站内容图片下架、并积极应诉
    博文内容全部不再显示,请需要相关资讯的站长朋友到必应搜索。谢谢!

    另祝:版权碰瓷诈骗团伙,早日弃暗投明。

    相关新闻:借版权之名、行诈骗之实,周某因犯诈骗罪被判处有期徒刑十一年六个月

    叹!百花齐放的时代,渐行渐远!



     

     

    @{Html.RenderAction("CorpsChengPinYouEnterprise", "WechatVote", new { categoryid = "160" });}

    黄色部分的页面指的就是下面的页面内容 这个是.net framework4.6.1框架 此项目最开始使用的是.net framework4框架

    @if (Model != null)
    {
    IList<Entity.m_vote_item> list = Model;
    int i = 0;
    foreach (var item in list)
    {
    i++;
    string voteid = "votecount_" + @item.id;
    string briefs = @item.brief + "";
    if (!String.IsNullOrEmpty(briefs) && briefs != "")
    briefs = briefs.Replace("\n", "<br>");
    <li>
    <div class="mingci_7" style="width:20%">@i</div>
    <div class="mingzi_zong" style="width:25%">

    @if (string.IsNullOrEmpty(item.brief))
    {
    <div class="mingzi_10" style="line-height:33px;">@item.title</div>
    }
    else
    {
    <div class="mingzi_10">@item.title</div>
    <a href="###" class="jianjie" style="float: left;margin-left: 40px;" onclick="javascript:m_brief('@item.title','@briefs')">企业简介</a>
    }
    </div>
    <div class="piaoshu_7" style="width:25%"><span id='@voteid'>@item.vote_count</span></div>
    <div class="input7" style="width:25%"><a href="javascript:void(0);" onclick="javascript:m_yanzheng('@item.id','@item.Categoryid')" class="date_2">投票</a> </div>
    </li>
    }
    }

    .net framework4框架里的代码为

    @if (Model != null)
    {
    IList<Entity.m_vote_item> List = Model;
    int i = 0;
    foreach (var item in List)
    {
    i++;
    string voteid = "votecount_" + @item.id;
    string briefs = @item.brief + "";
    if (!String.IsNullOrEmpty(briefs) && briefs != "")
    {
    briefs = briefs.Replace("\n", "<br>");
    }
    <li>
    <div class="mingci_7" style="width:20%">@i</div>
    <div class="mingzi_zong" style="width:25%">

    @if (string.IsNullOrEmpty(item.brief))
    {
    <div class="mingzi_10" style="line-height:33px;">@item.title</div>
    }
    else
    {
    <div class="mingzi_10">@item.title</div>
    <a href="###" class="jianjie" style="float: left;margin-left: 40px;" onclick="javascript:m_brief('@item.title','@briefs')">企业简介</a>
    }
    @*<a href="" class="jianjie">企业简介</a>*@
    </div>
    <div class="piaoshu_7" style="width:25%"><span id='@voteid'>@item.vote_count</span></div>
    <div class="input7" style="width:25%"><a href="javascript:void(0);" onclick="javascript:m_yanzheng('@item.id','@item.Categoryid')" class="date_2">投票</a> </div>
    </li>
    }
    }

     区别为

    if (!String.IsNullOrEmpty(briefs) && briefs != "")
    {
    briefs = briefs.Replace("\n", "<br>");
    }

    里面if条件里有大括号{ } 这样切换到框架.net framework4.6.1 并且配置文件和程序集修改正确后 编译会报错 缺少 } 的错误 所以把括号去掉 编译正确

    而查资料显示 if语句包含的一行HTML代码必须加括号 即:

    报错:

    @foreach (var item in ViewBag.TopList)
    {
      if (!string.IsNullOrWhiteSpace(item.LogoPic_Mobile))
        <a class="item" href="@item.ServiceLink"><img src="@item.LogoPic_Mobile" alt="@item.Title"></a>
    }

     

    正确(if语句包含的一行HTML代码必须加括号):

    @foreach (var item in ViewBag.TopList)
    {
      if (!string.IsNullOrWhiteSpace(item.LogoPic_Mobile))
      {
        <a class="item" href="@item.ServiceLink"><img src="@item.LogoPic_Mobile" alt="@item.Title"></a>
      }
    }

    所以个人猜测加不加括号区别是里面的代码是html代码还是服务器代码。html加{}, 否则不加。