0

hi: Anyone how can i handle server side button event in MVC view ? or set session or profile variables ?

asif
  • 43
  • 3
  • 10
  • possible duplicate of [server side code in MVC view ](http://stackoverflow.com/questions/5019756/server-side-code-in-mvc-view) – LukLed Feb 19 '11 at 20:10
  • You created 4 questions about MVC and answer is the same: Go through tutorial and learn. You have no knowledge of MVC, so there is no short answer. – LukLed Feb 19 '11 at 20:12

2 Answers2

4

There are no server side buttons and events in ASP.NET MVC. There are Models, Controllers and Views. Views usually contain standard HTML markup such as links and anchors and HTML forms that could post back to a controller and the lifecycle repeats: a controller receives a request, it queries a repository to fetch a model, maps this model to a view model and passes the view model to the view.

The following web site is a good place to start with ASP.NET MVC: http://www.asp.net/mvc

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • I have two href on my view and want to change image as well show different data from db on href click. how can I do this ? – asif Feb 19 '11 at 19:46
  • @asif, those href usually point to controller actions. Those controller actions query repositories to fetch models (from database or wherever), map those models to view models and pass those view models to the view for rendering. – Darin Dimitrov Feb 19 '11 at 19:48
  • Hi Darin:Can you please write some pesudo code how the view and controller code will be and do I need to use ajax, jquery ? – asif Feb 19 '11 at 19:51
  • @asif, you haven't provided enough details for your scenario so that I can show you with any sample code. – Darin Dimitrov Feb 19 '11 at 19:54
  • Lots of folks moving from WebForms to MVC go through this. You are trying to overlay WebForms models and conventions onto MVC. Take a step back and go through the Nerd Dinner tutorial (Google it). That will help a lot. Also, as a general rule, you really need to avoid Session. – BJ Safdie Feb 19 '11 at 20:01
  • I have a Model and a controller action with name index which passed data to index view. In index view, I have two hrefs and a div. on click of href1, I want to make image1 as background image of div, onclick of href2, I want to make image2 as background of div. On click of href1, one tr will be show and other will be become hidden and on click of href2 other tr will show and first one will disapper. both tr show data from model. – asif Feb 19 '11 at 20:01
  • Hi @. BJ Safdie. I have very short time. Please suggest how to implement my scenario. Thanks. – asif Feb 19 '11 at 20:04
  • Hi Darid: How to handle my scenario that I mentioned above ? – asif Feb 19 '11 at 20:08
  • @asif, your scenario seems to be properly client side related and has nothing to do with ASP.NET MVC. Swapping image backgrounds could be done with jquery. Maybe you could [consult the documentation](http://docs.jquery.com/Main_Page) and if you encounter some problems implementing it provide the code you have tried so far so that we can help you but don't expect anyone writing the code at your place. – Darin Dimitrov Feb 19 '11 at 20:10
  • @Darin. and how to fetch data from db on href clicks ? – asif Feb 19 '11 at 20:13
  • @asif, by querying the repository in the underlying controller action. – Darin Dimitrov Feb 19 '11 at 20:16
0

As Darin mentioned, there aren't server side buttons in ASP.NET MVC. Actually to be totally honest, you can make them work, but they will not do what you are expecting.

If you want to have a button call back to the server, you'll need to use: AJAX and then return the ActionResult you want. This question should help - ASP.NET MVC controller actions that return JSON or partial html.

One question you'll need to figure out is if you want to use the Microsoft AJAX library or jQuery. If you are used to ASP.NET, I would first look at the Microsoft one. A quick search should give you a bunch of resources.

Erick

Community
  • 1
  • 1
Erick T
  • 7,009
  • 9
  • 50
  • 85
  • Oh, no, don't look at Microsoft Ajax. That's crap. How can something like this be ever recommended against jQuery? I consider this legacy code that should be buried in a coffin. – Darin Dimitrov Feb 19 '11 at 19:55
  • Agreed with Darin. The ASP.NET team is de-emphasizing Microsoft Ajax and focusing future development and support on jQuery scenarios. – marcind Feb 19 '11 at 20:15