I've seen this question asked in a couple of places and generally the answer has been "use the ViewBag" which I don't think fits our scenario.
We have a membership site which has common properties (e.g. Account -which contains the user's account settings) and I would like to use that in both the Views and _layout.cshtml (e.g. to allow the user to change the colour scheme).
In the views, we're inheriting from a base view model but how can we get access to this data in the standard pages e.g. About Us etc which don't have any associated view model? I've tried creating a partial view which passes the data through in it's controller but that didn't work
Your main options are:
1. Consider implementing a profile provider - the settings are then available everywhere, use an Action Filter
2. use your own custom context assigned to the current request/user
3. use a base controller/base viewmodel for the pages that don't have them
4. use a global action filter
5. viewdata/session
See some details at:
ASP.NET MVC 3 layout ViewBag data across all child views
if you already have this working and need access in an "About Us" page and don't want another view model (why not?) then an action filter would be the way to go - unless this is a typical aspx page in that case you are left without options 3/4 above.
Related
I'm trying to get my head around Joomla 3.2's new framework and develop a capability/feature in a custom component (lets call it com_custom) that can load com_content's category blog view within com_custom's view, using the category blog's menu item id!
There are quite a few codes that I saw here and around the web that only load the default view of the 'imported' component, rather than the view specified in the imported component's assigned menu parameters. Also, most of these codes use deprecated functions, so I'm quite lost now.
So what I need to be able to add is:
Include a category blog view (say from menu item id 223) within com_custom's current view.
It should import all of the parameter setting of the category blog's menu item.
It should not be in an iFrame
Any help is much appreciated!!!
You can't include com_content because it makes use of the JPATH_COMPONENT constant in 14 different places, most notably in content.php (the main file that's loaded, even before the controller) and in most views, in order to load helpers.
These are the first two ways that come to mind to overcome this limitation:
Instantiate the articles model, load the data, and use your view to display it; you'll need to dig into the menu table to find the params related to your menuid; Bewre: Search engines might consider the duplicate content as spam.
Insert a jquery ajax call to get the url of the menuid adding &tmpl=component i.e.
index.php?option=com_content&view=category&id=13&Itemid=522&tmpl=component
The latter will be much easier to implement. Search Engines will NOT see the different content under different urls, since it's ajax.
Regarding the limitation, see also this question:
Joomla how can i create model from other component
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.
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.
Being a relative newcomer to MVC I'm musing over a little problem. I'm developing a page which relies on a collection of my viewmodel objects. All good. But, as the page has relatively complex functionality I need to get at other objects or collections of the main domain model to display in the UI. Admitedly rather more light weight objects. What's the best way to achieve this?
What I do is create a custom class to encapsulate ANY data I need to display in any certain view / action. So for my Admin areas User controller, I have a AdminUserIndexViewModel class that holds my list of user business objects. I also put any other data I need to display into this class.
For my AdminUserEditViewModel (that corresponds to my Admin Area, User Controller, Edit Action) I might have a single User class, which itself has its Roles attached to modify if need be. If you need to put other information like user settings, or preferences, etc...
If they are not part of the viewmodel you are currently working with then you can use Html.Action to call into another controller method to render another inline view through a controllers method.
I am new to ASP.NET MVC and I have a question regarding viewing entity relationships.
Say I have an entity called 'Person'. This holds the usual data relating to a person (Name, Email, etc). I also have a 'Notes' entity. Under EF, a 'Person' can have many 'Notes'.
I have a Person controller where I can view and preform CRUD operations on a Person object.
I can show the notes in the view easily but what is the best way to allow a user to add/edit/delete these notes from the Person view? I am hoping to do this using AJAX and not have the user move to a completely different page to add/edit/delete a note.
Thanks in advance,
ViperMAN.
When they edit a note, popup a jQuery dialog pointing to your URL to edit or have a separate Ajax.BeginForm() on the page that the details go into. When they finish the edit call a method to refresh the notes.
So:
1. In your Notes grid (or whatever)
you have an edit link for each note called "edit"
this link looks something like the following:
This one actually uses 'notes' : )
http://www.iwantmymvc.com/dialog-form-with-jqueryui-and-mvc-3
ASP.NET MVC | Problem about showing modal dialog using jQuery dialog widget
ASP.NET MVC modal dialog/popup best practice
Also beware of this scenario for multiple links:
MVC3 - Only first row link works well with Jquery Modal Dialog
Now the urls you use to populate the dialogs would be for example
/Note/Edit/10
One thing to note - jQuery validation needs to know about these new items that are being loaded via ajax into the DOM , so in your partial view you need to tell jQuery validation to include the new items - I'll edit in a bit to add this, have to grab it from another machine.