Call ActionResult of Another View from form - asp.net-mvc-2

I am trying to create a search form in my MVC application in View 1. The form was working very well when I used to submit the form to same page (View1). That way I can have two ActionResults - one of which accepts HttpPost requests. Everything is cool here
Now things have become slighly complex and I wish to separate the views. So in View1 there is a form and I wanted the results to be displayed in View2. So how do I call ActionResult of View2 from a form in View1?
In short - User enters keyword in View1. Hits Enter. Form in View 1 calls View2. ActionResults in View2 calls some logic to search and return View2 as the view and then I can display the results.
I tried some basic things like action="/View2" but I was pretty sure it would fail. It says 'the resource cannot be found'. Is it even possible to do this? Kindly advice.
*UPDATE*
It can be solved as answered below.
use Html.BeginForm(Name of the ActionResult,Name of the controller)

You need to specify the URL of the other action in the form, preferably by calling the Html.Form("ActionName", "ControllerName") helper.

Related

Xcode iPhone - Class that asks for multiple text inputs and then returns them

I wan't to creata a "CatchNames" class which I can import into a view Controller that shows a text which asks for text input. I would like to be able to add an instance of CatchNames to my view, have it ask the user for three names in a row and return them in an array.
[self.view addSubview:[catchNames view]];
NSArray *myNamesArray = [catchNames namesArray];
The best way would be to have the application freeze kind of the way it does when you are prompted to enter a password in iOS and continue when the user entered 3 names so I can immediately catch the array in the next line.
While this might not be the best description I still hope you understand my problem.
How can I approach this?
Thank you in advance
I guess you appear to be looking to implement a simple form which gets the user input, retrieves and stores it in an array? Hopefully I haven't misunderstood the question, but this seems to be a simple task you can accomplish with one or more UITextField's and a UIButton as a 'Add' or 'Done' call to action.
Are you looking for some general UI coding level help regarding implementing such a view? If so, I would encourage taking a look at the XCode documentations of UITextField (for capturing text), UIButton (for handling actions) and UIView (for view hierarchy and animation implementation).
Some quick notes;
Looks like 3 names are compulsory, so, you may verify whether a UITextField is empty at the button's click action.
Have the array declared in the view controller, not the view
The 'freezing' you require should take care of itself as long as the view offers no other way out for the user other than clicking the button.
Do excuse me if I am oversimplifying the problem. Let me know if you need me to drill down into anything further.
Cheers!

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.

Zend passing data from action in controller to view in another controler

How can I pass data from action in controller to view in another controller?
From action in controler to view in the same controller is easy:
I simply write in action's function :
$this->view->assign('error', 'Wrong login');
and in view I recieve it in this way:
<?=$this->escape($this->error);?>
but how can I do it to receive it in view of another controller?
I might be wrong but my guess is that for every request there is only one Zend_View, so if you set something on ControllerA and forward execution to ControllerB you could access that data in the same way.
It´ll not work if you use the action helper _redirect because it´s a browser redirection, to just forward execution to another "place" use the _forward helper instead.
Another option is the flashMessenger helper, that a look at the docs
http://framework.zend.com/manual/en/zend.controller.actionhelpers.html

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.

iPhone App - If I want to load different views based on actions on first view, what design pattern I should use?

My app has a main login/password screen where user enters credentials and submits the info to a web service. Once authenticated, web-service will also tell me what kind of user is loggin in. So user can be type "A", "B" or "C".
Now depending on kind of user I need to load different views (having functionalities) only that particular type of user can use. So I have home screens for "A-HOME" "B-HOME" and "C-HOME" which will then be a Nav Controller or a Tab Controller.
So basically if A logged in - next view loaded should be "A-HOME"
My layman's guess is to load view programmatically. However, I prefer to not do it as you can forget few steps and leak or screw the app. Is there any common design pattern that is used in such cases. The scenario seems pretty regular case to me. Are there any apps with sample code on apple's developer website that does the same thing?
Please give your suggestions.
Thanks.
If the views are "sufficient distinct" use different view controllers for each one, and load the approriate one. You can load the whole thing from a nib if you want, just wire this up in code, after your login procedure, i.e.
-(void) doLogin:(LoginType)loginType
{
if (loginType == LoginTypeA)
{
TypeAViewController *viewController = .... // Load from i.e. "TypeAViewController.nib"
// Add its view
}
}