c# - How to showhide HTML Buttons via MVC Action Method - Stack Overflow

In my web page, I need to populate button according to parameter value called ButtonType.let's sa

In my web page, I need to populate button according to parameter value called ButtonType.
let's say that If ButtonType == "Edit" then I need to hide every buttons but butUpdate.

I want to know how to show/hide html buttons via MVC Action method.

[AcceptVerbs(HttpVerbs.Post)]
    public ActionResult SupplierDetail(int SupplierID, string ButtonType)
    {   
        var Supplier = supplierListRepository.Supplier_SelectByID(SupplierID);
        return View(Supplier);
    }

I am using Asp Mvc Razor form.

@using (Html.BeginForm("SupplierDetail_SubmitClick", "Supplier", FormMethod.Post, new { id = "frmSupplierDetail" }))
{   
@Html.ValidationSummary(true)
<table cellpadding="0" cellspacing="0" border="0" style="width:450px; height:auto">
.....
<tr>
    <td>@Html.LabelFor(model => model.Phone)</td>
    <td>@Html.EditorFor(model => model.Phone)
        @Html.ValidationMessageFor(model => model.Phone)</td>
</tr>
<tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
</tr>
<tr>
    <td>&nbsp;</td>
    <td>
        <input type="submit" id="butSave" name="butSave" value="Save" style="width:100px; height:auto" />
        <input type="submit" id="butUpdate" name="butUpdate" value="Update" style="width:100px; height:auto" />
        <input type="submit" id="butDelete" name="butDelete" value="Delete" style="width:100px; height:auto" />
        <input type="submit" id="butReset" name="butReset" value="Reset" style="width:100px; height:auto" />
    </td>
</tr>
</table>
</div>

<div id="content">
@Html.ActionLink("Back to List", "Index")
</div>

}

Every Suggestions will be appreciated.

In my web page, I need to populate button according to parameter value called ButtonType.
let's say that If ButtonType == "Edit" then I need to hide every buttons but butUpdate.

I want to know how to show/hide html buttons via MVC Action method.

[AcceptVerbs(HttpVerbs.Post)]
    public ActionResult SupplierDetail(int SupplierID, string ButtonType)
    {   
        var Supplier = supplierListRepository.Supplier_SelectByID(SupplierID);
        return View(Supplier);
    }

I am using Asp Mvc Razor form.

@using (Html.BeginForm("SupplierDetail_SubmitClick", "Supplier", FormMethod.Post, new { id = "frmSupplierDetail" }))
{   
@Html.ValidationSummary(true)
<table cellpadding="0" cellspacing="0" border="0" style="width:450px; height:auto">
.....
<tr>
    <td>@Html.LabelFor(model => model.Phone)</td>
    <td>@Html.EditorFor(model => model.Phone)
        @Html.ValidationMessageFor(model => model.Phone)</td>
</tr>
<tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
</tr>
<tr>
    <td>&nbsp;</td>
    <td>
        <input type="submit" id="butSave" name="butSave" value="Save" style="width:100px; height:auto" />
        <input type="submit" id="butUpdate" name="butUpdate" value="Update" style="width:100px; height:auto" />
        <input type="submit" id="butDelete" name="butDelete" value="Delete" style="width:100px; height:auto" />
        <input type="submit" id="butReset" name="butReset" value="Reset" style="width:100px; height:auto" />
    </td>
</tr>
</table>
</div>

<div id="content">
@Html.ActionLink("Back to List", "Index")
</div>

}

Every Suggestions will be appreciated.

Share asked May 23, 2012 at 6:43 Frank Myat ThuFrank Myat Thu 4,4749 gold badges70 silver badges114 bronze badges 1
  • 2 Appricated in The same sense that you have appreciated the answers for the 20+ question where you didn't bother to accept an answer? – Rune FS Commented May 23, 2012 at 6:49
Add a ment  | 

2 Answers 2

Reset to default 2

in your controller add the buttontype to viewdata:

ViewData["ButtonType"] = ButtonType

Then, in the view itself, you can add if/else statements, or any other logic that suits all of ur cases, to decide what to render:

@if (ViewData["ButtonType"].ToString() == "Edit")
{
    <input type="submit" id="butUpdate" name="butUpdate" value="Update"    
     style="width:100px; height:auto" />      
}

Of course, this is but a demo of what can be done, Yuo should adapt the code to ur buisness logics

It is not a controller action responsibility to show/hide buttons. A controller action doesn't/shouldn't even know what a button means. That's a concept that exists on the view. A controller action on the other hand is supposed to municate with the model and prepare a view model that it passes to the view for displaying. So you could define a view model that will contain properties defining the visibility of the buttons and based on the value of the ButtonType parameter set those properties accordingly. Then the controller action will pas this view model to the view instead of the supplier object that you are currently passing. Obviously the view model will also have a property to hold this supplier. Now all that's left for the view is based on the values of the view model properties decide how to display the buttons.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744913010a4600665.html

相关推荐

  • c# - How to showhide HTML Buttons via MVC Action Method - Stack Overflow

    In my web page, I need to populate button according to parameter value called ButtonType.let's sa

    2天前
    60

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信