Using MVC How Do Get the C# Controller to Know Which Button You Clicked?

I am working on a MVC project.  When I click the New Button.  I want the controller to recognize the button I clicked.  I have both a New Button and a Save Button.  How do you pass back to the controller the button id that was clicked on?

Thanks,

Dan
danielolorenzAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Duy PhamFreelance IT ConsultantCommented:
Why don't you simply pass an extra parameter to determine the source button?
If both your New Button and Save Button are form submitting buttons, add an extra form field, then set value for it before submitting form.
If your buttons are Link button, when generate the link using Html.Action, add extra routeValues
0
Snarf0001Commented:
Just give the buttons a name attribute.  It will automatically post back to the server like any other field.
If you're using "input" buttons, then it will send the text of the button.

If you're using <button> elements, then put the value to whatever you want.

If you set both of them to "name='buttonPressed'" for example, then you'll have that in the controller like you would any other part of the model or std form field.
0
SwapnilSoftware ArchitectCommented:
Hi Daniel,

In your view put both the control with same "name" attribute, in below case it is action
<input name="Action" type="submit" id="btnNew" value="New" />
<input name="Action" type="submit" id="btnSave" value="Save" />

Open in new window


Then in your controller get the value of Action. Only the button clicked will pass its value.

public ActionResult Index(string Action)
{
    switch (Action)
    {
        case "New":
            // To Do
            break;
        case "Save":
            // To Do
            break;
        default:
            break;
    }
        //To Do...
}

Open in new window


Thanks,
NetSwap
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.