1

I am new to asp.net, razor and bootstrap and very familiar with winforms and older code.

I am trying to understand one thing: button click event in MVC?

MVC has no design view, no properties view to go right to the events. I have been trying to understand the relationship between jquery, c# and a button in a view, where does the logic live? i have created functions, tried to set a debug breakpoint, says code will never get here...

The other examples seem to be that while c# has a single onbutton click() event; in the mvc there could be several files that must line up with no intellisense to make sure its got the right handler???

2
  • 1
    Can you post some code to show what you are trying to do? Maybe post the form code and the .NET controller code you are trying to post to? Commented Jun 4, 2015 at 19:58
  • ok i will start over from scratch, just trying to find how the click is connected to code, any code Commented Jun 4, 2015 at 23:25

1 Answer 1

1

You are confusing client-side JavaScript with server-side C#.

In WebForms, you create a "OnClick" event that gets called on the server, but there is no such thing in MVC. The only "onclick" is a javascript client-side event that can only do things in javascript in the browser (or make ajax calls to the server).

Button clicks, if they are set as "submit" buttons trigger form posts, which call Action Methods on the server, these are not events. They are just your MVC action methods.

So the short answer is.. there is no "OnClick" event that calls a Server side handler.

Sign up to request clarification or add additional context in comments.

7 Comments

ok thanks for clarifying, i thought it was like this, but had put breakpoints on the server side and catch the wrong thing. auto generated create page has a create button. input type="submit" value="Create". so i copy this code and change values, the type = "submit" and value="testvalue" but it causes the Create method to run...?
@MaxPower - It only causes the create method to run if your original action is called Create and you have a Form with default action, or you have configured your form to post to a create action.
i just got symbols loaded to unwind and see the stack; but what does it mean, form with default action? and the form itself posts to a create action? <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" value="Create" class="btn btn-default" /> </div> </div> this is what was auto generated at the bottom of the Create.cshtml form. are you saying that if any method on the page generates "submit" that the server side controller model.cs "Create" method is invoked?
is there a more reliable way to have a sql data input form page where the button starts a transaction to safely upload the data, blocking out any other inputs until its done? and adding other buttons on the form, i added scripts and tried the onclick() inline with an alert dialog? just sits there.
@MaxPower - In you Create.cshtml you should see something that looks like @using(BeginForm()), that is the form definition.. because there is nothing in the parens, it means its using the default action, which is to submit to the same action as it was called from. In this case, Create (your original Get action is Create)
|

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.