[RESOLVED]Determine if button was pressed on page

I have an issue with telling if the user has pressed submit due to the fact the page is automatically posting back when a dropdown value is changed.

Bit of history regarding the form, I have three dropdown’s called ParentCategories, SubCategories and ChildCategories

When the user chooses a ParentCategory the page auto postback to the controller to obtain the sub categories associated with the ParentCategorie and same for when the user chooses a SubCategory. my issue is this when the categories are chosen I’m trying
to display a button so the user can press it and create a new item but when I click that it posts back to the same controller which the dropdowns do.

How can I determine if the button was pressed, below is my form

 @using (Html.BeginForm("New", "Advert", FormMethod.Post, new { @class = "form-horizontal", @id = "ListingForm" }))
                                {
                                    <div class="step-content pos-rel" id="step-container">
                                        <div class="col-md-4 catergoiesLayout">
                                            <div class="col-lg-10">
                                                @Html.DropDownListFor(x => x.SelectedParentCategoryId, Model.ParentCategory, new { @ID = "drpParentCategories", @class = "form-control", size = "7", @style = "width:300px", onchange = "this.form.submit();" })
                                            </div>
                                        </div>
                                        <div class="col-md-4 catergoiesLayout" style="visibility: @Model.SubCategory != null ? true : false">
                                            <div class="col-lg-10">
                                                @if (Model.SubCategory != null)
                                                {
                                                    @Html.DropDownListFor(x => x.SelectedSubCategoryId, Model.SubCategory, new { @ID = "drpSubCategories", @class = "form-control", size = "7", @style = "width:300px", onchange = "this.form.submit();" })
                                                }
                                            </div>
                                        </div>
                                        <div class="col-md-4 catergoiesLayout" style="visibility: @Model.ChildCategory != null ? true : false">
                                            <div class="col-lg-10">
                                                @if (Model.ChildCategory != null)
                                                {
                                                    @Html.DropDownListFor(x => x.SelectedChildCategoryId, Model.ChildCategory, new { @ID = "drpChildCategories", @class = "form-control", size = "7", @style = "width:300px" })
                                                }
                                            </div>
                                        </div>
                                    </div>
                                    <hr />
  
                                        <div class="wizard-actions">
                                            <button class="btn btn-success btn-next" data-last="Finish">
                                                Create
                                                <i class="ace-icon fa fa-arrow-right icon-on-right"></i>
                                            </button>
                                        </div>
                                }

this is my controller

  [HttpPost]
        public ActionResult New(ChooseCategoryViewModel model)
        {
            try
            {
            if(buttonPressed){ // How can I check? 

                  //Go somewhere else if its pressed

             }
           else{
                AddressCompleted();

                model = BuildCategoryViewModel(model.SelectedParentCategoryId, model.SelectedSubCategoryId);
              }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            return View(model);
        }

Add name and value attributes to your button:

<button class="btn btn-success btn-next" name="ButtonPressed" value="true" data-last="Finish">
    Create
    <i class="ace-icon fa fa-arrow-right icon-on-right"></i>
</button>

Then add a matching bool property to your ChooseCategoryViewmodel

public bool ButtonPressed {get;set;}

Then check the value in your controller:

try
{ 
    if(model.ButtonPressed)
    {
         // it was clicked

Leave a Reply