0

I'm a bit lost and getting real short in time.
I need to create something like this script

   $(function () {
            var i = 0;
            $('#addButton').click(function () {
                $('#form1').append
                
                ('<div  class="clearfix">Ingredient Item <div id="editor2"><input style="float:left;"  type="text" name="Ingredient[' + i + '].IngredientItem"/></div><div id="editor3"> Item Amount<input style="float:left;" type="text" name="Ingredient[' + i + '].ItemAmount"/></div>');
                //Dif table..
                ('<div  class="clearfix1">Instructions <div id="editor3"><input style="float:left;"  type="text" name="Instructions[' + i + '].IntrusionStep"/></div><div id="editor4"> Cooking Time<input style="float:left;" type="text" name="Instructions[' + i + '].CookingTime"/></div>');
           //& one more diff table here 
                i++;
            });
        });

I know this is not a good approach and far from best practice I didn’t find any example of using any better way to do it ( I'm complete novice as far as JavaScript or any scripting for this matter).

6
  • What do you want to insert to db?. Nothing is clear from your code snippet Commented May 9, 2013 at 10:28
  • Soory , For example in the first line I have recipe IngredientItem and the ItemAmount (salt , one spoon , sugar 2 spoon) hope its clear now Commented May 9, 2013 at 13:06
  • What all things you want to insert to db. What action will trigger the insert? Commented May 9, 2013 at 14:00
  • I have put my controller here so maybe it will be more clear , I need to insert strings jsfiddle.net/dueUe Commented May 13, 2013 at 9:24
  • I've posted an answer. Tell if its not the case. Commented May 13, 2013 at 9:56

1 Answer 1

4

What i have understood is that you need to post your form from java-script code. You can make use of a jQuery post or an ajax post method here.

$.ajax({
  url: '<%=Url.Action("Create","YourController") %>',
  type: 'post',
  data: $(form).serialize(),
  datatype: 'json',
  success: function (result) {
    // Handle code for success post
   },
  error : function (result) {
   // Error condition
   }
});

Now make your controller action accordingly to return json value back to your java-script code in the view

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

Comments

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.