State management in MVC 2 - asp.net-mvc-2

I want to show a Session Variable data on View.
How can i achieve it.
I am storing some data in Session varaibles and noew i want to display it on View please help me.
I am new Asp.Net MVC 2
Please suggest.

use the ViewData and pass it to the view
in your controller set up something like ViewData["namehere"] = value
then in your View you can call the value of Viewdata["name here"] - think of the ViewData as a bag that you can put properties and their values into and not need to explicitly pass the viewdata to the view for the view to use it

view data is just like session in asp.net so in your controller you can create more than one view data then create you view ,if you want to create a partial control for the view it will alse see the result of the view data but do not forget to cast the result.

Related

Pick value from IPhone tableview

I have just started developing iphone applications, but now i'm stuck with this problem, my problem is,
how to pick a data from table view and bring it back to previous view.
if you can give me a sample code that would be really appreciated.
Thank in advance,
prasad.
I don't think altering AppDelegate will be a good practice although it will work. You should prefer creating a Model class where you can create a variable and set its value in your table. When you go back to previous view you can use the same variable which contains the selected table value.
Good luck..!!
You can make an object in App Delegate class.set its value as the value picked from tableview in the table view class.In the previous class you can make an object of app delegate class & access that particular object whose value was set with the value picked from table view

Passing data from controller to master page - based on currently logged in user

Using MVC2
Have a master-page that needs to hide certain menus if currently logged in user does not have correct flags set.
Seems like a common problem. Found examples that require that all controllers inherit from a base controller (I have that) and where in constructor of the base controller, the passing of certain parameters to ViewData can occur. This is great and would be easy for me to do, but User.Identity and Request objects are NULL during the construction of base controller.
How do I get to User.Identity of the currently logged in user so that I can query database & modify the ViewData collection accordingly before Master Page view is rendered?
Thanks
You could use child actions along with the Html.Action and Html.RenderAction helpers. So you could have a controller action which returns a view model indicating the currently logged in user info:
public MenuController: Controller
{
public ActionResult Index()
{
// populate a view model based on the currently logged in user
// User.Identity.Name
MenuViewModel model = ...
return View(model);
}
}
and have a corresponding strongly typed partial view which will render or not the menus. And finally inside the master page include the menus:
<%= Html.Action("Index", "Menu") %>
This way you could have a completely separate view model, repository and controller for the menu. You could still use constructor injection for this controller and everything stays strongly typed. Of course there will be a completely different view model for the main controller based on the current page. You don't need to have base controllers or some base view model that all your action should return.

calling methods across view controllers

My question is about the best way to implement passing of information between UIViewControllers.
If I have an application with 2 ViewControllers and for example a user selects an item in ViewControllerA which should then show the item and more details in ViewControllerB.
What would be the best way to implement this? via the appdelegate? or by passing a reference to ViewControllerA into ViewControllerB? Appreciate any help or examples of the best way to do this.
ViewControllerA (VCA) would maintain a reference to ViewControllerB (VCB). VCB would maintain a reference to the selected object as an ivar. When the user chooses an object in VCA, VCA instantiates VCB (if not already instantiated), sets VCB's selectedObject property to that object, and then pushes VCB. VCB reads from the object assigned to its selectedObject property to draw its information into the view.
In VCA, for every one of the "items" that the user can select, there needs to be an underlying object instance backing that item. For example, a UITableView might be backed by an NSArray of Vegetable objects if the user is selecting from a list of vegetables.
In general, try to keep data sharing between controllers to a minimum. Have them refer to model objects instead to get their data.
Try using the MVC design pattern. Put all shared state information into a Model object (M of MVC) created at a higher or the top level of your app. When creating your two view controllers, give them access to the model object (by setting a property in each view controller). Then the view controllers can store and access any shared state required, and you will have it nicely centralized for debugging, storing, extensibility, reuse, etc.

iPhone MVC. Problems with the model

I'm new to programming, iphone application programming in specific. After reading a bunch about MVC I decided to give it a try in a small application. As to my understanding, MVC works like this:
Model: data, manipulating data, retrieving data.
ViewController: formats data from the model (NSDate to specific style), etc.
View: the actual gui.
If this is indeed a correct formulation of basic MVC theory, my confusion lies in how data is passed between the model, VC, and view. Example: if I make calls to twitter and get the data in the model, how do I (correctly) pass this information to the VC for further work. I know that between the VC and View one mostly uses IBOutlets. The Model is my real problem.
In my last app I made an NSString variable in the app delegate so I could access that data from any class. However, I read that this is not the best approach when the app becomes complex because the delegate is in charge of starting, ending the app, not holding data.
I've read of delegation methods, singleton's, NSNotification (which I've used to call methods in other classes). The problem is that I don't really understand how to use these techniques to pass data from the model to other views.
Please let me know if my question is unclear.
If you think about reusability, the main components that can be reused again are your model objects and view objects. They can be moved to different apps and still used properly. Your view controller is what is really specific to your app and where most of the app logic lies.
So in your example, you could have a twitter object that stores information and tweets from a user perhaps. You would create that class with all its functions separately within its own .h and .m file. Then in your view controller, instantiate the twitter class with data that is retrieved and begin using it from within the view controller.
Your view controller is actually retrieving the data but your model object is the one maintaining the data. In this way, you can pass on the model data with your twitter object to other view controllers.
Control over the application resides in the controller, so it is the object that will retrieve or save persisted data, update views with that data, and handle various events. Consider it the glue between the model and the view!
For example, if you were to click on a button to open a new modal view, you'd handle that event in your view controller. In the method that responds to the clicked button, you will create or access the new view controller and present it using presentModalViewController:animated:. If that new view and controller needs data that your current controller has access to, you could set a property in the new controller to refer to the object.

iPhone/UIViewController: when navigating away from a view, what is the best way to handle data entries

I should know this by now, but I am still a bit confused. When my app navigates from one view controller to the next (via the navigation controller) I want to "finalize" the data for the current VC before going to the next VC. The only way I see to intercept the "page swap" is in the [old view viewWillDisappear] -> [newView viewWillAppear] transition. It seems weird, though I guess it works okay.
Is that really the right way to handle navigation transitions? My app is a bunch of VCs which collectively build a database file. Each VC deals with a different aspect of the data.
I don't know your exact setup, so this may not be useful to you, but I have good experiences with saving data in -(void)textFieldDidEndEditing:(UITextField*)tf, using tf.tag to index the fields. From there I commit the data to a storage class, and have no worries about what happens in the UI.
What exactly is involved in the "finalizing" part? I assume you are storing some state in the view controller for various fields and then you want to write that to the database file before going on to the next view?
When it comes to "edit view controllers" I find a nice way to do it is to have the view controller directly write to a simple model object which is injected through a property before pushing it to the nav controller.
So something like:
/* Somewhere in the app delegate, like application:didFinishLaunching */
DatabaseFileModel *model = ...;
viewController1.model = model;
viewController2.model = model;
/* ... */
[self.window makeKeyAndVisible];
Then each view controller writes to this model by setting properties etc. when a text field ends editing or whatever. Having the view controller write straight to the object means you don't need to handle viewWillDisappear etc.
If you do still need to do this however you can add a delegate to the navigation controller and handle these two methods:
– navigationController:willShowViewController:animated:
– navigationController:didShowViewController:animated:
See the UINavigationControllerDelegate documentation for more information.
This will let you keep the logic in one place rather than spread out in each view controller.