0

I am very new to MVC and need help how I should go about to create an application with the following features.

What I want to do

I have a form that asks some questions to users, and for each question users must be able to add as many items as they want as their answers.

For example, say the application is a recipe book management (Not my actual project), and a user enters the following information:

  • Recipe Name (e.g. "Orange Lemonade")
  • Description (e.g. "The best lemonade...")
  • Fruit Ingredients
    • Select fruit from an existing list or type fruit (e.g. lemon)
    • Enter quantity (e.g. 3)

NOTE: There are many other ingredients or lists such as Vegetable Ingredient list, etc, and they have to be entered the same way as Fruit Ingredient

The picture below simply describes the idea...

enter image description here

Here's the code I used: JSFIDDLE (I referred to this site)

My Question

How should I go about to store the list of ingredients like Fruit, Vegetable, and the actual application would have many more lists in one form.

Is there a way to call my repository methods to add/delete a list item every time an item is added/deleted?

Or should I save each list to an array and store them when the "Save" button for the form was clicked?

Edit

My question probably wasn't clear but I would like to know if there is a standard way or recommended way to achieve this. If there's none and whichever the method is fine, then I would like to call the repository every time user makes a change to the list. (Users must be able to save the recipe and come back to edit it until they submit the final version of the recipe.) And I'd like to know is how to call the method to save list items to DB from the client side in MVC (not using web controls like ScriptManager).

Community
  • 1
  • 1
kabichan
  • 1,063
  • 4
  • 19
  • 49
  • The key to binding to collections is to name the controls corretly with indexers. [Refer this article](http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx/) for further information. You have not shown you models so impossible to give you an answer. [This answer](http://stackoverflow.com/questions/29837547/set-class-validation-for-dynamic-textbox-in-a-table/29838689#29838689) may also help –  Apr 28 '15 at 00:05
  • Stephen's links both show an important item, that you can have non-standard indexes if you also explicitly supply the index (Say . While this works for any string index, it also still works for a numeric index. And can help a lot after a deletion to the list. It you have a numeric index list and don't specify the indexes, you can have the binding stop when it hits a "gap" because an item was deleted. But specifying the index values keeps these items from dropping out. – Greg Nov 21 '16 at 17:11

1 Answers1

0

This question is hard to answer because it depends on your application. Which way you implement it is completely based on how your application would be used and the expectations from both you and the user.

Consider the following questions:

  1. Do they have to finish the entire recipe before saving it and having persisted?
  2. Are you willing to live with incomplete recipes?
  3. Do you expect it to take days to complete the task and thus will allow the user to save and continue later?
  4. Is there a draft, in progress, complete, published type status that the task will progress thru?

Based on the answers to these questions I think you will be able to answer your own question. The answers will elucidate your requirements and which way to go as far as architecture.

Tony Basallo
  • 3,000
  • 2
  • 29
  • 47