MVC 4 - 如何有条件地禁用此按钮?

时间:2013-03-06 12:51:39

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

如果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>

1 个答案:

答案 0 :(得分:29)

<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>