MVC with View Model - Create an object optionally - asp.net-mvc

I use MVC with View Models (I create separate View Model for each View).
I have 2 objects; Product and Category; Product can have a category.
I have separate view models (productCreateVM, CategoryCreateVM)
I can create product or category;and user can choose one of current categories while creating a new product, no problem.
What I need to do is that ; users can choose one of current categories or create a new category than choose it, while creating a new product.
If I add CategoryCreateVM in ProductCreateVM, then CategoryCreateVM's mandatory fields need to be filled in, in order the model state to be valid.
but if user already found a category for their product and didnt create a new one; I can not fill all the mandatory fields of CategoryCreateVM.
On the other hand; if user is creating a new category from product create view; then all the validations should be applied (category name lenght etc).
Any suggestions how to approach to this?

If you want to use more complex validations see if http://foolproof.codeplex.com/ will be sufficient.
It it should provide you with the conditional validation you need

You should consider rendering the Create Category View as a partial view within the Create Product View.
Its visibility can be toggled by e.g. the selection of Create Category within a Category drop-down menu.
For example:
#Html.Partial("Category/Create")

Related

Generate dropdown with scaffolding in MVC View

i am new in mvc and just reading a article from this link http://weblogs.asp.net/scottgu/ef-code-first-and-data-scaffolding-with-the-asp-net-mvc-3-tools-update
view the below image there is two model class one is product and one is category and category class has one property called Products which return ICollection
and when generate UI with scaffolding pointing to product model class then UI generate with product details and a dropdown appear for category.
i just do not understand how scaffolding incorporate a drodown for category when generate a UI page for product. product has no link for category but how category related drodown comes in product page. just help me to understand it. thanks
here are few screen shot
i just like to know when we generate product view page using scaffolding then we point our product model class and product model class has no property for category then why and how category related drop down appear in product view page. if u know the reason then share the concept in detail. thanks

How to create a multi step wizard to dynamically add/edit objects in Navigation properties in MVC

I am working on my first MVC 4 application. We are using Model first approach for this application. I am working on the add employee module which has multistep wizard.
Employee class looks like below, it has couple of navigation properties for other tables,
Employee
{
Employee Properties;
.
.
ICollection <ASSET>;
ICollection <TEAMS>;
}
I am using a viewmodel having an object of employee entity and other Select list objects for the dropdown lists required during add process.
public class EmployeeViewModel
{
Employee NewEmp;
.
.
List<Country> Dropdown1;
List<City> DropDown2;
.
.
}
I have created a strongly typed view to pass this viewmodel in add screen.
Add wizard is divided into 8 steps and each step is rendered through partial view. Everything works fine for the properties of Employee entity. But I have steps where,
User can select from assets from dropdown list and tag them to employee currently being added.
User can tag employee to the team, select employee role,etc.
I want to re-use these partial views in edit mode also, so I believe the navigation properties will have list of valid objects in case of edit mode. The idea is to add/edit all the information in viewmodel and save it to database on finish button.
I want to understand how can I create and add the objects for navigation properties in current viewmodel. Also how can I re-use these 2 steps in edit mode ? Can I create new team objects at client side and add it into a employee object collection list? Is there any other way (like using Ajax) to implement this functionality? Please help me because I am really stuck with this from long time.

in asp.net mvc what's a clean way to create an entity from the 'create' view of another entity?

I am trying to come up with a clean design for this -
I am using MVC to process orders, so I have an 'order' entity, with its own controller and views.
From the Create Order view I would like the user to add a 'Customer' entity. I have a controller and CRUD operations for 'customer'.
When someone creates a new Order I would like them to either
1) enter a customer name to see if that customer already exists, and if so, add that Customer to the Order, or
2) Create a new Customer then add that new Customer to the Order.
My problem is I am not sure of a good way to access the Customers from within the Order.
-do I create a partial view for Create Customer, then use that view in the Customer Create AND Order Create?
-then would I create a partial view 'SearchCustomers' that passes params to an action on Customer controller and that returns results? Would I be able to reuse this across the site?
You can see I am not sure about a few things - are partial views the way to reuse things? can partial views be reused across controllers and access different controllers from the ones theyre in?
I have gone through an MVC book and online tutorials but they all seem to use beginner examples, where Model objects don't contain other Model objects.
Thanks for help
Views never "access controllers".
The flow is:
Controller Action Method -> Creates View Model -> Hands it off to a View -> View displays it -> View can pass sub-models on to other partial views
Decomposing everything into smaller models and partial views (or using Editor templates) is a good approach. You can then assemble those smaller models into a larger view model for a complete page, or you can use the ViewBag to assemble them in a non-strongly typed manner.

Using a dynamic list of checkboxes in a view, how to create the model

I have an asp mvc 2 app lication where I want to display a list of check boxes that a user can select, based on a list of records in a database. To display the list my model contains a List object and the view has a foreach, and outputs Html.CheckBox for each item in the list.
Is there a way to get the model populated with the selected checkboxes, given that the model can't have specific properties for each checkbox, because the list is dynamic? Or do I have to manually iterate through the forms variables myself?
Edit: Extra details as per sabanito's comment
So in a simple view/model scenario, if my model had a property called Property1, then my view outputted a Textbox for Property1, when the form is posted via a submit button, the mvc framework will automatically populate a model with Property1 containing the text that was entered into the textbox and pass that model to the Controllers action.
Because I am dealing with a dynamic list of options the user could check, I can't write explicit boolean properties in my model and explicitly create the checkboxes in my view. Given that my list is dynamic, I'm wondering if there are ways to create my model and view so that the mvc framework is able to populate the model correctly when the form is posted.
Here's what I would do:
Are you having any issues generating the checkbox's dynamically?
If not, create a property on your ViewModel that is a:
public List<string> CheckboxResults { get; set; }
When you generate your checkbox's in the view make sure they all share the name = "CheckboxResults". When MVC see's your ViewModel as a parameter on the action method it will automatically bind and put all the "CheckboxResults" results in the List (as well as your other ViewModel properties). Now you have a dynamic List based on which checkbox's your user checked that you can send to your DomainModel or wherever.
Pretty cool stuff. Let me know if you're having issues generating the checkbox's dynamically, that's kind of a seperate issue than model binding to a list.
Use a ViewModel that reflects your view exactly, and map your domain model(s) to the viewmodel.
At first it often seems appropriate to use domain models directly in the view, for no better reason than that they're simple to use. However, as the view gets more complex over time, you end up putting a TON of conditional logic in your view, and end up with spaghetti. To alleviate this, we typically create a ViewModel that correlates 1:1 with the view.

asp.net mvc custom modelbinder - how to perfom updates with assosciated entities

I am trying to understand how associated entities are updated when using custom modelbinders.
If you have a Product entity with a relationship to a Category entity and you are displaying a list of category choice for a product in a dropdown on a form.
The the user assigns a new category and that change needs to be persisted with the Product. How is the binding implemented to assign the updated category? The properties f the Product are easy enough, but how do you set the Product.Category = category?
Hope that is clear :-)
Sounds like your custom model binding is there, you're just trying to setup up your relationship between your Product and Category.
To do this you would do something like this:
product.CategoryReference.EntityKey =
new EntityKey("Context.Category", "ID", categoryID);
That will simply update the foreign key relationship in your entity.

Resources