How is button connected in asp.net MVC app? - c#

I have an asp.net MVC app. There is a <button> in a .cshtml file. The button only has an id. There isn't any javascript associated with it. I don't see this button name in any of the controllers.
Yet, when the button is clicked, it doesn't perform an action. How do I trace down what is triggering the button action once it is clicked?

I think you are a little confused on how ASP.NET WebForm and MVC works.
While WebForm is very much similar to WinForms , in a sense that controls can have events that gives it a sense of Stateful appearance, MVC is not the same, it embraces the stateless nature of HTTP.
In MVC when a button is clicked, it is usually associated with a controllerand a action to which it posts the data. So, a button click is translated to action on a control same like method invocation in a WebForm.
Q. The controller still needs to know about the button to handle the click?
there is no existence of control on server side, its the downside (+side from my view) of being stateless. In MVC there is no click, but only method invocation on the controllers.
A same action can be invoked using 10 different buttons. If u can share the code of <view>.cshtml then may be I can help a little bit more.

There could be java script in a separate file. In that JavaScript file there must be click handler attached that must be calling your action in controller via whatever mechanism.
To begin with .. Search in your project where else that button id is used.
That could be the point where you can start tracing...

By virtue of the button being defined within the Html.BeginForm braces, the button click is wired to perform a POST submission with the form data.

Related

How to call a C# method from asp.net side?

This seems to be a common question but I could not find a relevant answer
I have a aspx page and in it,
I've made a list of "a href" tags that is generated from my C# code behind.
Now i want to send data based on which of the "a href" tag i have selected and pass it into my C# code which will then populate my popup with required information:
The process
"a href click" -> call c# method -> populate popup with data
I am stuck at how to pass data from the "a href click" to the c#.
I tried using .OnServerClick but my popup didn't even pop up.
For starters, i would like to try: When i Click a "a href" tag it would call my changeTitle() method from c# which will change the title of the pop up. If i can get this working, i should be able to manage the rest
c# method:
public void changeTitle()
{
this.modalTitle.InnerHtml = "Goodbye world";
}
Do tell me if you need more information please, I really hope to get this working
You should really look into learning more about how jQuery and ASP.NET can work together. I highly recommend starting at Dave Ward's site.
Here is a blog entry about using jQuery to call ASP.NET server methods (page methods are a great way to get quick hooks into server-side logic that can pass data back to the client-side):
Using jQuery To Directly Call ASP.NET AJAX Page Methods
I have used this technique in many projects over the years and I think once you start learning the power of jQuery you will want to use this approach over strictly server-side controls.
If ASP.NET WebForms is being used (remember to specify which "flavor" of ASP.NET), a LinkButton control may be a suitable approach1.
A LinkButton works like a a normal Button that looks like a hyperlink and causes a PostBack when clicked. (This is different from "normal" hyperlinks that change the browser location.)
The OnClick attrtibute specifies the server callback handler and the Command and CommandArguments can be used to associate specific data with the control that are available on the server during the LinkButton's click callback.
1 While my current preferred form of development is a thick client with a thin backend (e.g. not WebForms), switching to use "Page Methods" or "AJAX" requires rewriting the front-end/HTML to act on the response as appropriate. A LinkButton on the other hand, simply "works" with the normal ASP.NET WebForms lifecycle/infrastructure without any additional work.

ASP.Net Only 1 Form Restriction: Should I be using a webpage for everything?

I am forced to use Web Forms in my project, and sadly, Web Forms only allow - If I may - "Strict" Websites to be created.
Whenever you need a Button you need to put it in a form, and then you need another button which has nothing to do with the previous button, and you can't have 2 forms,
And the idea of putting a DIV that fires a server side (C#) method is kind of difficult, okay it may be easy but all I have found are "tricks", not an "official" clean way.
So I have this idea of making a webpage for each action in my websites.
For Example:
Let's say I wanna click on the ARROW that raises the rating of this question, I would put something like this.
HTML
Rate Up
And Some CSS Codes to make it look like a beautiful button...
Okay now this will take me to a page called Rating.aspx with 2 parameters, the first parameter is the ID of the question that I would like to raise its rating, and the second parameter is either UP (+rating) or DOWN(-rating).
On the Page Load method of Rating.aspx, I would update the database, then redirect to the question page.
This will work perfectly, BUT, is it a good approach? is it professional? (put in mind that there will be many actions to preform like that...)
With ASP.NET you better use server controls. Better way of implementing that is using or , that actually renders your anchor tag. But you can attach OnClick event handler to this control (link button) so after clicking there would be automatic POST to server. The same page cycle for the current page will take place (this is called PostBack) and your attached event handler will fire, where you can actually make changes to the database. So you don't even need to create any other pages for tasks like this. Every server control has specific set of events like OnClick for buttons or OnSelectedIndexChanged for dropdown lists. You can even create your own controls or derive from existing ones and create your own events.
Take a look on following links for more information:
Button Click
Event Handling in ASP.NET
ASP.NET Page Life Cycle

what is the best way to design a wizard form in ASP.NET MVC

i want to design a form in asp.net in Wizard style. do something in click next.
the form have 3 step
fill your information
add element [here if you type something wrong then you can edit or delete them before going to next step]
finish
what is the best practise to design this in ASP.NET MVC with a power of ajax.
are anyone show me the best way i can use to do this in MVC
Here's how you could proceed: the elements of each step of the wizard could go into a separate div. The Next and Previous buttons will show/hide the corresponding div. On the last step there will be a submit button which would send the entire form to the server.
You might also take a look at the jquery form wizard plugin.
One of the ways that I have implemented a wizard is to have a separate database table that contains all of the information you are required to store and to save/retrieve data to that table in each step of your wizard - obviously depending on the size and purpose of the wizard this may not be sensible with the number of database calls but I was implementing only a 5 page wizard with maximum 5-10 fields on each page. So when you land on a page you query the database and retrieve the information from the database or if it doesn't exist load a blank page where the user can then enter the information and it is saved when they click either Next or Previous.
For navigating between pages I simply built a helper class that accepted the page name and button type (Next/Previous) and had a simple switch statement which would return the page to navigate to and then used that in a RedirectToAction statement. Again this may not suit a larger application but you could also look at using Windows Workflow (touched on in this article http://www.devx.com/dotnet/Article/29992) as I know that it can be used to create wizard style applications.
It is not particularly an MVC solution but I advise a client-side implementation using JQuery LightBox.
You don't need any client side stuff to achieve this, it's also bad practise to use javascript for anything other than user convenience.
You have 2 problems with a wisard:
1: maintaining state. ie saving data between requests.
2: figuring out which action (usually next or previous) to take.
Maintaining state.
You can use the session object but ideally (and so you can unit test them) all actions should be pure functions. I use hidden inputs to save data between requests.
User actions.
For a next / previous view. Add 2 submit buttons to your form and give them names. When you
POST the form, the button with the none null value was the button pressed. Then redirect to the appropriate action.

Form POST in ASP.NET

I'm trying to convert a classic ASP page to ASP.NET 3.5. The page has several forms on it for several different things.
In ASP.NET, there's a server form control wrapping the entire page, and form controls don't work within a server form control, and you can't have more than one server form control on a page.
So in order to keep this functionality, I can either:
Remove the server form control that's wrapping the page, and leaving the html forms on the page.
Create button click events for every form and perform the POST in the code-behind.
What's the preferred method here?
I wonder if converting to vanilla asp.net (aka webforms) is a bad idea here. Personally I'd go to MVC instead - allows multiple forms etc, and the views are much closer to he HTML, a lot like ASP.
I guess I'm saying there are some glitches vanilla asp.net introduces that you don't have to suffer.
I would go with the second option, any button click is going to post the whole page back anyway so you're not saving any bandwidth. Simply handle each button appropriately.
Check the answer I provided to a similar question here :-)
How to get past embedding a html form for paypal buttons asp.net
If you're going to use different button clicks, you still need to use this override to disable the non-related buttons in each handler, otherwise it won't work. You can only have one form tag at a time - this way you can toggle/disable the ones you're not using as appropriate.
Better still, refactor your application to use a single form. While MVC would be a closer match to the model you're using right now, it wouldn't make sense to go that route unless you were experienced enough with it; Web Forms is an easier jump.

ASP.NET MVC and ModalPopupExtender

I am developing an application with ASP.NET MVC and wish to make use of Modal style windows.
Here is one example. On my "Login" view, I have my login details, but also have a button to create an Account for the application. I have a separate "CreateAccount" view, but would like it to pop up inside a ModalPopupWindow.
Is this possible usng just ASP MVC and the ASP.NET Ajax Library, or will I have to use a different Ajax library?
(Another way to ask this question: can I redirect the output of a view into a ModalPopupExtender?)
Jason
I would suggest you returning a PartialView from your controller and loading the result in some jQuery plugin to show popups in the same page context like this
The usage is something like
Boxy.load('/Account/Create'); // You can post additional data if you need to
This way you will have a beautiful Box (facebook like) with your form in it. You just have to be aware that if you post something in it, you should do it using AJAX to update the response in the same Box.
Good Luck!!

Categories