How to access the view page from a controller? - asp.net-mvc

I've been tasked w/porting an older ASPX app to .NET MVC, and then doing some work on it.
This has been fraught with peril; not the least of my troubles is that the APSX events won't propagate in this framework (using ViewPage for the aspx.cs file - MVC 4 demands it)
I.E. - an asp:Button with a defined click event won't fire that click event anymore.
What prompted this particular question is this - I've got an asp:table that I want to reload when certain actions happen in the controller. In the code behind file, I've got a method that handles that, and it gets called from Page_Load, just fine.
But in the controller, I need to find a way to get a handle on that page itself and invoke that method. I can get a new instance of the page, but that instance won't have the table already instantiated.
How, in the controller, can I get a hold of the ViewPage created for that method?

You can't think about MVC that way. There are no click events like you think of them when you think ASP.NET.
read these
http://www.asp.net/mvc/tutorials/getting-started-with-aspnet-mvc3/cs/intro-to-aspnet-mvc-3
http://www.w3schools.com/aspnet/mvc_intro.asp
you might find some joy with them
these will help too
Migrating from ASP.NET WebForms to MVC
and Migrating legacy ASP.NET to MVC 2 (RC): HttpApplication events not firing, User principal is null

Related

How to see in ASP.NET from which page was a method called?

I have a ASP.NET MVC project in which I have a method on a controller which is being called from two separate pages, until now the behaviour was the same but now I need it to be slightly different I just need to see if I'm on page A or page B so I know which behaviour happens.
Is there any solution with the ViewData or View or something else with which I can check from which page this method was called?

Is there an equivalent of JSF #ViewScope in ASP MVC?

In the application I'm developing in ASP MVC 3 I have the following scenario. I have some properties of the model that I wanna use after the page makes a request, however when the post request is done they arrive as null since they are not bound to any control in the view form.
For what I've read this is the expected behaviour of ASP MVC and people recommend to use a #Html.HiddenFor() to be able to receive them, but I don't want this information to be available to user in case he selects "View source" from the browser.
In JSF I remember that you could use the #ViewScope annotation for this scenario, but is there something similar in ASP MVC? For what I've read saving them in the ViewData property will not work either and I do not want to use the Session because this properties will only be relevant in this particular view.
If I understand your question correctly, you may be looking for something like TempData.
You can read about it here:
http://blogs.teamb.com/craigstuntz/2009/01/23/37947/
A more recent post can be found here:
http://codeoverload.wordpress.com/2011/05/29/controller-tempdata-in-asp-net-mvc-3/

asp.net mvc Controller - View logic

I'm trying to learn the asp.net mvc. I have worked with most of the samples that MS have published.
Most of the samples is just about CRUD.
I'm pretty good at working with webforms, but now I kind of miss the old easy world.
But, my question is: I have a detail page that is connected to an order, I have the order details comming along all well. I got my order detail lines kind of work. But now I would like to do different things with this order, like add more order lines, change the order status with a button, email the order and so on. In webforms I just added eventhandlers on the click event, but here... Do I need multiple forms? How do I make serverside code for example when someone wants to change the orderstatus with a click of a button?
Hi i can recomend you Pro ASP.NET MVC 2 Framework book by Steven Sanderson
I'd recommend you go through the entire NerdDinner tutorial.
But to answer your question:
There really are no forms in asp.net MVC. There are views, which show your order, and there are controllers/actions which allow you to run code to generate views, update your database, etc. You ask if you need another form - the answer is No. You will, however, need at least a new action on a controller, and possibly a new view to confirm that the order was changed. An overview to a very simplified solution would be:
You want to allow an order to be marked as closed by clicking a button. Assume you have a controller called Order, and a view (in the order views folder) called Details. You'll have to add an action which takes an integer as a parameter to your order controller called "Close". In that action, you'll read the integer parameter (the order ID) and execute code to update that particular order to closed. You'll probably want to return the same view with the udpated status from this action. You'll have to add a link (probably using HTML.ActionLink function) to your view to call this new action.
In MVC everything is Post and Get actions. You can post to difference controllers. It seems you need to spent a bit more time reading about it. If you do not fancy reading - try some nice video tutorials for beginners. http://www.asp.net/mvc

Facebook canvas app ASP.NET MVC HttpException: Invalid Model

I am working on a Facebook tab application. I am using asp.net MVC 2 with the "official" Microsoft Facebook SDK.
Most of my views are working just fine.
However, I have one that is causing a huge mess. I am simply returning a List<Tags> to the view and as my ViewModel. This problematic view is simply displaying the list in a foreach loop. Whenever navigate to this view I get a "System.Web.HttpException: Invalid Model" exception.
The most puzzling part of all this is that the same code is running without fail on our regular(non-Facebook) app that works just fine with the same code.
Any insight into why Facebook and asp.net MVC 2 would be causing this behavior would be much appreciated.
So, I finally figured out what was causing this dang problem.
It turned out to not be related to the View Model, the View or even the Action.
We are using another library. It is the Rest For Asp.NET MVC SDK. This library lets you decorate your action with [WebApiEnabled]. This allows those actions to return JSON or XML if the request has a ?format=json or ?format=xml.
If all of the actions in the controller should be Rest enabled then you can decorate the class instead of each method. Well this is what we did. And it was causing issues with the way that facebook grabs data.
So we moved the decoration from the class to the actions and the error went away.

ASP.NET MVC Beta 1 - Will My Existing User Controls Work In My Views As-Is?

I understand that you can now create MVC-specific user controls, but will my existing standard ASCX user controls work in an MVC view?
I understand that MVC pages have a completely different lifecycle but will, for example, a Page_Load method be invoked as normal when the control is sitting in a view?
If your standard ASCX controls do not have control events. There is no viewstate in MVC so that'll have to change.
The normal page lifecycle is still executed. E.g. Page load, init, prerender, etc. The main thing is viewstate.
You can instantiate pre-built controls and call their RenderControl() method in order to use them in MVC views.
Stackoverflow does this for reCAPTCHA control rendering.
Also, the validation part is mapped to the route /captcha/post where the control is instantiated and the Validate() method is called.
So, essentially yes you can reuse your controls but you have to adapt to the architecture of MVC.

Resources