0

Not sure if the title is clear enough. I have Products and Categories entities in an MVC-EF app. when adding a new product, I select the category from a drop down list. I would like to be able to create a new category if it doesn't exist in the drop down list from within the product/create page. maybe having a button like "Add New Category" next to the drop down list.

For example: if I want to add a book that belongs to the Cooking department (which is not in the list), I want to be able to add the cooking department at the same time I'm adding the book without going to the Department/Create view

enter image description here

2
  • 2
    Have you tried anything yet? If so, please post your code from your attempt. Commented May 20, 2015 at 18:16
  • No. Your question is not clear. What do you know to do and what don't you? Commented May 20, 2015 at 18:16

2 Answers 2

1

Well, there's three primary approaches:

  1. Handle the category creation through a modal window or similar that pulls in an iframe. This is the easiest method as the category creation is completely segrated with its own actions and views. However, you'll still need some JavaScript to update your select list choices to include the newly created item after its saved. That's relatively easy, though. You just issue an AJAX request to some endpoint that returns all the available categories and use that to rebuild the select list after the modal is closed.

  2. Use AJAX for category creation. This is a little more complicated than #1, but still relatively straight-forward. You simply have a form with the fields for a new category embedded somewhere in your page. You can also use AJAX to request the form HTML from an action that returns a PartialViewResult. Either way, you can present this to the user through a modal or whatever you like. Then, when they submit, instead of a traditional form post, you simply issue an AJAX request with the serialized form data to some endpoint responsible for actually saving it. Like #1, you'll need additional JavaScript to update your select list accordingly after saving the category.

  3. Include the new category with the post data of the rest of the product form. This is the most complicated of the three choices as you have to take special care in a few ways. You'll need a separate collection on your view model to hold the posted category or categories, and you'll need to manually add these new categories to your product entity before saving it. You'll also need to synchronize it with categories associated via the select list.

Giving you anything more specific is beyond the scope of StackOverflow. Whichever of the above three methods you choose, a little bit of research will turn up enough articles/tutorials to get you rolling. If you have specific issues in the process of building your solution, you can come back here to ask specific questions about those.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks, do you have any example of the first method?
0

I have had a similar requirement on a project. Make something like this:

public class Category 
{
    public int Id { get; set; }
    public string Name { get; set; }
}

Then you populate the dropdown with all your categories, and you can make more as you go along.

On your product you just make a virtual property of category to access it.

public class Product
{
    public int Id { get; set; }
    //Rest of your fields
    public int CategoryId { get; set; }

    public virtual Category Category { get; set; }
}

Doing this will allow you to lazy load the related entities if you wish.

4 Comments

Wouldn't this break though if the category drop down was also was a separate table linked by a foreign key?
Not at all. By using the virtual and the Category Id it links it all up nicely, and is one of the key features of EF. I do stuff like this all the time as it's quite a common requirement. edit: Also worth noting that as long as you used the DropDown as having values of Ids, when it was posted back you just pass that into the CategoryId property and EF will link it up appropriately. Ofcourse you might want to check if that Id exists ;)
@Giltch100, this already works for me. all the categories are in the dropdown. I want to create a new category when creating new product without leaving the product/create view
Then Mike just have the add new category enable another textbox and see if the model has that when its posted and use that. Or create another ActionResult on a controller or make an api call, and just have the 'Add New Category' ajax post to that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.