0

UPDATE: Here is my .html page looks like and withing the form tag I have couple of buttons (page size, save to db, remote items)

<div class="col-lg-12">
    <div class="panel panel-info">
        <div class="panel-heading">
            <h3 class="panel-title">Records</h3>
        </div>
        @using (Html.BeginForm("Index", "MyController", FormMethod.Post, new { @id = "form_header" }))
        {
            <div class="panel-body">        
                <div class="row">
                        <div class="col-xs-4">
                            <div class="form-group">
                                <label for="" class="col-xs-5 control-label">Rows per page:</label>
                                <div class="col-xs-4">
                                    @Html.DropDownListFor(m => m.PageSize,
                                    new List<SelectListItem>
                                    {
                                        new SelectListItem() { Text = "10", Value = "10" },
                                        new SelectListItem() { Text = "25", Value = "25" },
                                        new SelectListItem() { Text = "50", Value = "50" },
                                        new SelectListItem() { Text = "ALL", Value = "ALL" }
                                    }, new { @class = "form-control", Name = "pageSizeDDL" })
                                </div>
                            </div>
                        </div>

                        <div class="row text-right">
                            <div class="col-xs-12">
                                <button class="btn btn-success" type="submit" id="btnSavetoDB" name="saveToDB" value="Save to DB">
                                    <span> <i class="fa fa-save"></i>  Save to Database </span>
                                </button>
                                &nbsp;&nbsp;&nbsp;
                                <button class="btn btn-danger" type="submit" id="btnRemoveFromGrid" name="removeItems" value="Remove Item From Grid">
                                    <span> <i class="fa fa-remove"></i>  Remove </span>
                                </button>
                                &nbsp;&nbsp;&nbsp;
                            </div>
                        </div>
                </div>

                <div class="col-lg-14">
                    <div class="panel-body">
                        <div class="row">
                            <div class="test" style="overflow: scroll; width: 100%">
                                <div style="width: 1000px">
                                    @if (Model != null)
                                    {
                                       //grid   
                                    }
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

           </div>
       }
    </div>

I have seen all sorts of approach but still not convinced the best way to handle and in my situation I have multiple buttons or events that fire based on the user action: for an example in my case: in the form:

I have page size dropdown where user can select number of rows to display I have a import button to import the doc I have save button - save to db I have a remove button - remove from grid

currently this is how I have laid out:

[HttpPost]
public ActionResult Index(HttpPostedFileBase file, FormCollection formCollection, string pageSizeDDL, string saveToDB, string removeItems, string PageList)
{ 
     if (file upload) {....}
     if (formCollection) { .... }
     if (pageSizeDDl {....}
     ..........
}

my question is:

What if i need the form values for all buttons or perhaps is that right approach the way I'm doing?

I have seen wrapping each button in it's own form in view which I things its not the correct approach and the reason is if I have 10 button in a single page does it mean I have to have 10 form?

Nick Kahn
  • 19,652
  • 91
  • 275
  • 406
  • 1
    Sounds like some of these should be links rather than buttons. If you need more than one submit button for a single form you're probably doing something wrong. It's difficult to be more explicit with almost no information to go on. – Ant P Apr 07 '15 at 17:46
  • @AntP: Can you elaborate what do you mean by links rather than buttons? – Nick Kahn Apr 07 '15 at 17:47
  • Let me know what information you need and I can provide you. – Nick Kahn Apr 07 '15 at 17:49
  • 1
    Duplicate? http://stackoverflow.com/questions/442704/how-do-you-handle-multiple-submit-buttons-in-asp-net-mvc-framework?rq=1 – AaronS Apr 07 '15 at 17:49
  • A "button to select the number of rows to display" should not be a button - it should be a link that issues a GET request and passes a querystring parameter specifying the number of rows. Forms and buttons and POST requests should be reserved when the user is actually submitting information that will change something. – Ant P Apr 07 '15 at 17:50
  • in regards to the number of rows to display, I'm doing server side and that's why I'm using POST – Nick Kahn Apr 07 '15 at 17:54
  • I have updated my question for more clarity – Nick Kahn Apr 07 '15 at 18:26

0 Answers0