2013-03-06 77 views
5

我想有條件地禁用此按鈕或隱藏它,如果Model.BicycleSellerListingId不大於0.不知道該怎麼做。MVC 4 - 如何有條件地禁用此按鈕?

<div style="position:absolute; left:300px; top:633px;"> 
    @using (Html.BeginForm("Delete", null, new { id = Model.BicycleSellerListingId }, FormMethod.Post)) 
    { 
     <button type="submit">Delete Listing</button> 
    } 
</div> 

回答

22
<div style="position:absolute; left:300px; top:633px;"> 
@using (Html.BeginForm("Delete", null, new { id = Model.BicycleSellerListingId }, FormMethod.Post)) 
{ 
    if(Model.BicycleSellerListingId < 0){ 
     <button type="submit">Delete Listing</button> 
    } 
} 
</div> 

OR

@if(Model.BicycleSellerListingId < 0){ 
    <div style="position:absolute; left:300px; top:633px;"> 
    @using (Html.BeginForm("Delete", null, new { id = Model.BicycleSellerListingId }, FormMethod.Post)) 
    { 
     <button type="submit">Delete Listing</button> 
    } 
    </div> 
} 

OR

<div style="position:absolute; left:300px; top:633px;"> 
@using (Html.BeginForm("Delete", null, new { id = Model.BicycleSellerListingId }, FormMethod.Post)) 
{ 
    <button type="submit" @((Model.BicycleSellerListingId < 0) ? "disabled" : "")>Delete Listing</button> 
} 
</div> 
+1

非常好,非常簡單。比我想象的更簡單。 – Hosea146 2013-03-06 13:15:46