Mutually exclusive search options on MVC 3 view - asp.net-mvc-3

I'm working up a view that will use 2 sets of search options and would like to know if there is better way than I'm headed.
on right hand side it will search by username, lastname
on left hand side it will search by an entity type or entity name
the results should be returned as a grid below the search.
I've considered that I may need 2 forms on the view but not sure if that is the right direction. Would 2 partial views each with their own form be better? If so, how would data be returned to the main view?
I'm trying to keep this simple by posting to controller actions and returning views instead of a bunch of confusing jquery.
Currently I have a model with 2 sub-models that each define the search fields that will allow a user to enter data into the textboxes.
What is the proper way to handle this in MVC 3?

You can use two partial views if you are going to re-use the forms on other views or if you just want to encapsulate the view code. I would suggest two forms that post to different controller actions 'SearchPerson(model), SearchEntity(model). Both actions can return a common 'Results' view model to a 'SearchResults' view.

Related

MVC 3 Combinging multiple controls in one sort control template

I'm working on some forms, and often I can reuse the same amount and combination of fields together, is it possible to group them all as a template and then call it from the page?
For example:
2 radio buttons with labels and 2 texboxes under.
How is it called so i can do a proper research?
You can use Partial View, its like a reusable component.
These links will elaborate more:
http://www.devcurry.com/2012/04/partial-views-in-aspnet-mvc-3.html
http://www.dotnet-tricks.com/Tutorial/mvc/2IKW160912-Partial-View-in-Asp.net-MVC3-Razor.html
MVC3 Partial Views
Create a Partial View and keep your markup which you want to reuse in that. You can use these Partial views in other views as needed then.
You can use the Html.Partial helper method to call the partial views in other views.
#Html.Partial("RecentItems")
You can also pass some model to your partial view as well
#Html.Partial("RecentItems", Model.RecentItems)
Assuming Your partial view is strongly typed to a class which is of the same type as the type of Model.RecentItems, of the caller view.

ASP.NET MVC 3: Reuse the same form in all the web site

I have a Razor ASP.NET MVC 3 web site.
I have a web site with this structure for all pages:
- header with search text box in top-right
- body
- footer
I would like to have two distinct views with their own specific model.
I would like to have two distinct method: one for the search and one for the body actions.
How to organize this?
If I use two partial views I have to pass SearchModel around all pages for all controllers for all methods.
How to deal with this?
Thanks
There are 2 ways to reuse your search form:
First is to use #Html.RenderAction() at _Layout.cshtml that will render your search form to view.
Second is to use #Html.RenderPartial() at _Layout.cshtml, and model will pass to view throught ViewBag object or ViewData dictionary from global action flter.
For the search box in your view you can use #Html.Action to call a child action allowing it to build the SearchModel / search view independently from the current action.
http://haacked.com/archive/2009/11/18/aspnetmvc2-render-action.aspx

How to Handle Mutiple Model Bound Forms

I am buiding a UI screen for editing the details of an Ecommerce Order. The model for my view (OrderModel) has everything I need (in properties that are also ViewModels), but the UI isn't designed to be able to edit all of it at once.
For example, one part of the UI is for customer data..another for order details, and another for tracking information, each having their own "Save" buttons.
I realize that I could use one giant form and use hidden form fields to populate the non-editable fields, making each "Save" button post all the data, but that smells bad.
I'd like to segment the editable chunks into smaller ViewModels that are posted and validated individually while retaining the strong typing but I'm unsure of how to achieve this in MVC3. Will I need partial views that are called from the primary view?
FYI, I'm using ASP.NET MVC 3 with Razor syntax and client side FluentValidation.
Partial Views are a good solution. You can pass different ViewModels to each partial view. But if only sections of the overall view are updated at a time I would not do a post back on the whole page. Instead I would use Ajax calls using JQuery/Javascript to update the individual information back to the controller. I would also look into something like Knockout.js to handle the data binding on the page.

MVC3 : How to call A view from another controller from a different controller

I have to make a form consisting of several sub forms which may or may not contain grids.
For simplicity lets say i am using 2 models
1. Applicant Detail : where the applicants basic information (Name, DOB, Gender) is placed.
2. Applicant Experience : where the applicants prev work experience details are being placed.
Now for organizing purpose i am thinking of making two different controllers Namely ApplicantDetail and ApplicantExperience and both will contain their respective functionalities. But as soon as the user enters his basic information i want the same view to call in the index view of ApplicantDetail which will have a grid and the user can enter his/her prev working details in the form of rows.
My basic question is, is there anyway that can i create single razor view which will contain the razor view of my ApplicantDetail as well??
I believe what you are looking for is RenderAction.
Using RenderAction you can invoke controller/view within any view.
So if you had some razor view and second Controller and View combination, you could invoke it like so within the first view:
#{ Html.RenderAction("Action", "Controller2"); }
This can be useful when trying to modularize functionality.
See this blog post from Phil Haack for more details:
Html.RenderAction and Html.Action

MVC 3 - using reflection to choose a partial view - abuse?

I think I'm finally starting to get MVC 3, but if someone can validate this approach I'd feel better about it.
I have a website, let's say, and I have models for NormalPage and EventPage. EventPage has an EventDate, but that's the only difference, and let's just say that EventPage inherits from NormalPage if that makes life easier.
Two views handle these two (slightly) different models, one just showing the page, and the other displaying the date and showing a registration form. They have different designs, so different views are in order.
All the tutorials will say "yup, now write two Controllers: Events and Pages". That seems silly - both are just passing the model to a (appropriate) view. I can use a single "Page" controller and choose the appropriate view using reflection, right? typeof(Model), once I've pulled the data from the database, can tell me whether or not I should pull the Event view or the Page view.
Is that dumb, or asking for trouble, or misusing the framework? Thanks.
What do you intend to do with reflection? You don't need to do anything like that to return views dynamically. From any controller action, you can return View("EventView", eventModel) or View("NormalView", normalModel) and it will return that view.
On a different note, I'm not sure what tutorials suggested that it's typical to have one view per controller but it's not. It's typical to have several views and actions in one controller.

Resources