MVC C#视图不允许在tag中使用if语句

时间:2012-04-16 20:56:26

标签: c# html asp.net-mvc razor

我有一些代码:

<table class="invisibleforprint">
    <thead>
        <tr class="mainheader">
            <th>@Html.ActionLink("Invoice Number", "Index", new { sortOrder= ViewBag.NumberSortParm })</th>
        </tr>
    </thead>
    <tbody>
    @foreach (var item in Model) {
        <tr>
            <td class="invoiceActions">
                <input type="button" class="btnresetinvoice button" value="Reset" data-invoiceid="@item.InvoiceId" />
            </td>
        </tr>
    }</tbody>
</table>

哪个编好了。我去了输入中添加了一个if语句:

<table class="invisibleforprint">
    <thead>
        <tr class="mainheader">
            <th>@Html.ActionLink("Invoice Number", "Index", new { sortOrder= ViewBag.NumberSortParm })</th>
        </tr>
    </thead>
    <tbody>
    @foreach (var item in Model) {
        <tr>
            <td class="invoiceActions">
                <input type="button" class="btnresetinvoice button" value="Reset" data-invoiceid="@item.InvoiceId" @{ if(item.PMApproved != true) { @:disabled="disabled" } } />
            </td>
        </tr>
    }</tbody>
</table>

它给出了错误'}预期'

说什么?我添加了相同数量的左右括号。

任何人都知道我做错了什么?

4 个答案:

答案 0 :(得分:1)

<input type="button" class="btnresetinvoice button" value="Reset" data-invoiceid="@item.InvoiceId" @(item.PMApproved != true ? "disabled='disabled'" : "" ) />

答案 1 :(得分:1)

<input type="button" value="Reset" data-invoiceid="@item.InvoiceId" @(item.PMApproved ? "disabled=\"disabled\"" : null) />

或者您可以将@if<text>代码一起使用:

<input type="button" value="Reset" data-invoiceid="@item.InvoiceId" @if(item.PMApproved) { <text>disabled="disabled"</text>} />

答案 2 :(得分:0)

不应在if语句周围使用花括号,而应使用普通括号。

即:

@( if(item.PMApproved != true) { @:disabled="disabled" } )

你可以尝试一下吗?

答案 3 :(得分:0)

您可以执行以下色情内容(但请不要):

<input type="button" class="btnresetinvoice button" value="Reset" data-invoiceid="@item.InvoiceId" @Html.Raw(item.PMApproved ? "disabled=\"disabled\"" : "") />

或使用帮助suggested you

@Html.ApproveButton(item)