2011-05-04 140 views
1

我想清理我的代碼剃刀語法問題

@foreach (System.Data.DataRowView c in (System.Data.DataView)ViewBag.Folders) 
{ 
<tr> 
    @if ((Boolean)c.Row["VF_Visible"]) { 
     <td><a href="@Url.Action("Index", "page", new { parent = c.Row["SMF_VF_ID"], granParent = c.Row["VF_Parent_ID"] })">@c.Row["SMF_Name"]</a></td> 
    } else { 
     <td class="itemUnVisible"><a href="@Url.Action("Index", "page", new { parent = c.Row["SMF_VF_ID"], granParent = c.Row["VF_Parent_ID"] })">@c.Row["SMF_Name"]</a></td> 
    } 

    <td>@Html.ActionLink("Edit", "edit", new { id = c.Row["SMF_VF_ID"] })</td> 
    <td>@Html.ActionLink("Delete", "Delete", new { id = c.Row["SMF_VF_ID"] })</td> 
    <td>@Html.ActionLink("Preview", "Preview", new { id = c.Row["SMF_VF_ID"] })</td> 
</tr> 
} 

是內部的,如果被複制的代碼。
當我離開的時候,如果只是沒有標籤和結束標籤的td,它會給我一個錯誤。

@foreach (System.Data.DataRowView c in (System.Data.DataView)ViewBag.Folders) 
{ 
<tr> 
    @if ((Boolean)c.Row["VF_Visible"]) { 
     <td> 
    } else { 
     <td class="itemUnVisible"> 
    } 
    <a href="@Url.Action("Index", "page", new { parent = c.Row["SMF_VF_ID"], granParent = c.Row["VF_Parent_ID"] })">@c.Row["SMF_Name"]</a></td> 
    <td>@Html.ActionLink("Edit", "edit", new { id = c.Row["SMF_VF_ID"] })</td> 
    <td>@Html.ActionLink("Delete", "Delete", new { id = c.Row["SMF_VF_ID"] })</td> 
    <td>@Html.ActionLink("Preview", "Preview", new { id = c.Row["SMF_VF_ID"] })</td> 
</tr> 
} 
+0

ugh..code湯。使用ViewModel和顯示模板來擺脫foreach循環。 – RPM1984 2011-05-04 12:22:41

回答

2

你不能把非結構良好的HTML塊內部(如您的<if>),因爲剃鬚刀將不知道在哪裏結束該塊..

您需要force the HTML to be treated as literal text

@if ((Boolean)c.Row["VF_Visible"]) { 
    @:<td> 
} else { 
    @:<td class="itemUnVisible"> 
} 
0

有一些不成文的規則,指出每次你有你的觀點的if,寫一個幫手:

public static HtmlString DetermineCellAction(this HtmlHelper helper, 
    object visibleColumnValue) 
{ 
    // All code below could be much cleaner, this is just an example! 

    var visible = (bool)visibleColumnValue; 
    var td = ""; 

    td = (visible) ? "<td if visible>" : "<td if not visible>"; 

    return new HtmlString(td); 
} 

然後,你可以使用它像:

@foreach (System.Data.DataRowView c in (System.Data.DataView)ViewBag.Folders) 
{ 
<tr> 
    @Html.DetermineCellAction(c.Row["VF_Visible"]) 
    <!-- mode html here --> 
</tr> 
} 

請注意,該幫手是一個extension method,在這種情況下延伸HtmlHelper,但你也可以使它成爲一個擴展方法DataRowView如果那裏更方便。

0

你可以做這樣的事情(未測試)...

<td @if((bool)c.Row["VF_Visible"]) { <text>class="itemUnVisible"</text> } > 

更新您的視圖模型比使用DataRowView的集合清潔。我會創建一個模型類,您可以在控制器中進行所有解析,包括calc可見性,然後將其傳遞給您的視圖。

public class MyViewRecord 
{ 
    public int Id {get;set;} 
    public int ParentId {get;set;} 
    // etc. 
    public bool Visible {get;set;} 
} 

然後,您可以這樣做......

@foreach (MyViewRecord item in ViewBag.Folders) 
{ 
<tr> 
    @if (item.Visible) { 
     <text><td></text> 
    } else { 
     <text><td class="itemUnVisible"></text> 
    } 
    <a href="@Url.Action("Index", "page", new { parent = item.ParentId, granParent = item.GrandParentId })">@item.Name</a></td> 
    <td>@Html.ActionLink("Edit", "edit", new { id = item.Id })</td> 
    <td>@Html.ActionLink("Delete", "Delete", new { id = item.Id })</td> 
    <td>@Html.ActionLink("Preview", "Preview", new { id = item.Id })</td> 
</tr> 
} 

希望這有助於