I have a pretty standard scenario where I have a model which in this case is a product.
The product model has a load of properties such as price, desc etc which I want to display on a view.
I have this working fine.
The product has a list of accessories objects which get displayed on the view and gives the user the ability to enter a qty for that accessory.
When the user clicks add to basket I only actually need the list of accessories and the id of the parent product.
Should I be creating a view model that contains all the product info I need or do I just pass my view the product and then on submit just get a view model for the few fields that I need in the signature of the function?
I'm new to mvc in case you hadn't guessed!! and I am using MVC 3.
Using a separate view model is better practice as it means you can add extra information outside of the Product which may be relevant such as Basket and User info. If you have many fields on the product then you can use Automapper to take the relevant fields and map them to the view model. Also you can flatten the model if necessary and make serverside queries which fecth all of the model's related items such as Category info etc.
Take a look at Automapper here:
http://lostechies.com/jimmybogard/2009/01/23/automapper-the-object-object-mapper/
once you start using it you won't look back.
The academic way would be to crete a separate ViewModel class with just the data you need in the view.
But when you are comfortable with ignoring the extra fields in your class i think its absolutely ok to use your Product model class in the view.
Related
I have one resource (User table) for which I need to perform some action.
However when that action selected I want to pass too many additional fields and selection is dynamic.
For example, if I select any user and click on action then in action I should select a category from DB and based on that category I need to get subcategory and based on that subcategory I want to get some seller on action page in Nova...
Is there any way i can do this.
I think Action Fields might help you with that. You can add additional nova fields to your collection of models before handling them in the action.
Hope that this can help: nova action fields
you can use this great NovaPackage
https://github.com/epartment/nova-dependency-container
I have custom entity which has lookup to the Products entity record.
I want this field to show only active(status) Products.
How can I do this? am I supposed to make new lookup view in Products, change existing one, or something else?
You should pick the view you'd like to render the records from. Navigate to form customizations, and edit the field property to look like below:
I'm using a design update XML applied to all products under specific categories. The update is applied successfully to those products when browsed to them from those categories, but not when those products are opened from search results. How can I make the design update affect those products when opened from search results?
You need to add a layout handle that you can "grab" for each one of these products and modify the layout through layout xml files.
The key to this process is in the initProductLayout method of Mage_Catalog_Helper_Product_View. This method is where custom layout handles are added based on the product model. You can grab the layout update object from the controller and call addHandle() on it with a string to add that handle. So you'll want to rewrite this method and do something like this:
$update = $controller->getLayout()->getUpdate();
foreach ($product->getCategoryIds() as $categoryId) {
$update->addHandle('PRODUCT_IN_CATEGORY_' . $categoryId);
}
Now, in a layout xml file you can target the <PRODUCT_IN_CATEGORY_##> handle for the ID of your category(ies) and any layout updates you put here will be applied to the product view page no matter how it is accessed.
Depending on the specifics of your installation, it may make more sense to key the handle with some other category identifier, like the name or URL key, instead of the numeric ID. For this, use $product->getCategoryCollection() and iterate through the collection to grab what you need. You may also want to use $product->getAvailableinCategories() if you want to include only category IDs that the product belongs to directly (instead of including categories of higher parentage).
We are planning to develop furniture shop with PrestaShop. I am a newbie with PrestaShop and am interested to know more about it. The following is the scenario that we have:
In the shop, we would like the top navigation to list the areas in the house (e.g. dining, bedroom, etc). When we hover this, there will be a dropdown menu with a list of furniture type (e.g. dining chairs, dining tables, and so forth).
In addition to that, the left side will be a list of "collections". Each collection can contain items that are of different furniture types. E.g. Collection A can have a dining chair, living room chair, and so forth.
In the list of collections, there is a possibility to have a "special" collection. This collection will have a different layout page compared to the other collections.
Hence, I am wondering if:
Is this possible to use Prestashop? I can see that #1 is possible using the categories feature (1st level category as the areas in the house and 2nd level category as the furniture types). Also, #2 might be possible using "Suppliers" or "Manufacturers". What I am concerned, is there a way to actually separate "suppliers" / "manufactures" as "specials"?
If it is possible, can we actually create a specialized page for these?
Thank you so much for your help everyone! Any kind of suggestions would be great! I look forward to hear from you!
For each of your 3 needs:
You are correct that PS's categories will allow you to do this. The category system allows you to have a hierarchy of categories that is as deep as you need, and what you described is exactly how it's meant to be used.
As you hinted, you can use either suppliers or manufacturers for this by simply changing the name to "Collections" and associating products with the correct suppliers (if you choose supplier instead of collection). You can add a description to a supplier or manufacturer that will appear by default before the list of products.
You can add a custom description to each supplier or manufacturer page, but I think you're looking for even more variation. If the layout change you need for "special" collections isn't too extensive, you could achieve it by doing something tricky in the page template which checks what page is currently being displayed, and shows or hides HTML or executes Javascript based on the result. This would provide quite a bit of flexibility, even though it's "dirty" in the sense that you'd have to hardcode information in a template. A better way to achieve this would be to override the manufacturer controller (assuming you chose to use the manufacturer to represent collections) to use a different template for certain manufacturer IDs. I haven't tested this code, but I think it's pretty close to what you'd need to put in the override directoryalong with a new template called manufacturer-custom.tpl in your theme:
class ManufacturerController extends ManufacturerControllerCore {
// array with the selected manufacturers
private $customManufacturers = array(1, 2, 3);
public function init() {
parent::init();
if (in_array($this->manufacturer->id, $this->customManufacturers)) {
$this->setTemplate(_PS_THEME_DIR_.'manufacturer-custom.tpl');
}
}
}
What I am trying to do using asp.net MVC3:
-User selects a Category from a list of Categories.
-The SubCategories page then displays a list of images found to represent that Parent Category as well as the list of SubCategories.
I run into the need for multiple models because the "Category Images" are associated with Category and not the "SubCategory model". Yes I realize this seems like it has been asked many times before but the syntax is still foreign to a noob like me. What goes where?
I got the Category -> Html.Actionlink to the controller to return a list of SubCategories part... but even if I did some left join to get category and list of images as well, how would I get those into the same view as the list of subcategories?
Update
Found this posting:
http://francorobles.wordpress.com/2011/05/03/returning-multiple-models-in-1-view-in-asp-net-mvc3/
Question: Is that a valid approach? My intellisense suggests no.
In cases like this you need "view models", i.e. view-specific models that have all the data your view needs.