2

I Have a Strongly typed user control which i use for Searching a certain list of objects. the following code shows the user control,

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<PeercoreCRM.ViewModels.CustomerFilterViewModel>" %>
<div style="width: 100%;vertical-align:top;background-color:White">    
    <fieldset>
        <legend>Criteria</legend>
        <table cellspacing="0">
            <tr>
                <td style="width: 100px">
                    <div class="editor-label">
                        <%: Html.LabelFor(m => m.LeadName) %>
                    </div>
                </td>
                <td>
                    <div class="editor-field">
                        <%: Html.TextBoxFor(m => m.LeadName) %>
                    </div>
                </td>
            </tr>

            <tr>
                <td style="width: 60px">
                    <div class="editor-label">
                        <%: Html.LabelFor(m => m.CustomerCode) %>
                    </div>
                </td>
                <td>
                    <div class="editor-field">
                        <%: Html.TextBoxFor(m => m.CustomerCode)%>                         
                    </div>
                </td>
            </tr>                            

            <tr>
                <td>
                    <input type="submit" name="btnSearch" value="Search" />&nbsp;
                    <input type="submit" name="btnCancel" value="Cancel" />
                </td>
                <td>
                </td>
            </tr>
        </table>

    </fieldset>
</div>

In the View, I show this user control conditionally using the following code snippet,

<% using (Html.BeginForm("CustomerList", "Customer", new { isFiltered = Model.FilterViewModel.IsFiltered }, FormMethod.Post))
   {
    %>
<% if (Model.FilterViewModel.IsVisible) Html.RenderPartial("ListFilterUserControl", Model.FilterViewModel); %>
<% } %>

I have put the Form in the rendering page as this control is used in other views and thereby calling other action methods in different controllers.

I have the following method signature in my controller action method,

[HttpPost]
public ActionResult CustomerList(CustomerFilterViewModel filterModel)
{

    bool filtered = filterModel.IsDirty? FilterCustomers(filterModel):false;
    Session["CurrentPageNumber"] = null;

    return RedirectToAction("CustomerList", new { isFiltered = filtered || filterModel.IsFiltered });
}

My problem is, with this implementation, how can i separately identify which button is clicked ("Search" or "Cancel") and to write the code depending on that.

1 Answer 1

8

To identify the button that has been passed in, you can group your buttons by adding a name attribute to them:

<input name="button" type="submit" value="Search" />&nbsp;
<input name="button" type="submit" value="Cancel" />

then, add a variable that is passed into your post method with the same name of the buttons (in this case "button") like this:

[HttpPost]
public ActionResult CustomerList(string button, CustomerFilterViewModel filterModel)
{
    if(button.Equals("Search")) 
    {
     bool filtered = filterModel.IsDirty? FilterCustomers(filterModel):false;
    Session["CurrentPageNumber"] = null;

    return RedirectToAction("CustomerList", new { isFiltered = filtered || filterModel.IsFiltered });
    } else {
       if(button.Equals("Cancel")) {
            //perform cancel
       }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

In your html you've got the "name" attribute mentioned twice. This is not required. Only the first name attribute is used by the browser and submitted as the "name" part of the name-value collection item. So basically, you don't need to have the second name attributes. That is name="btnSearch" and name="btnCancel" are not required and in fact should not be there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.