1

How To Send List Of Data From View To Controller With Ajax Form In Mvc4 :
My Model Sample :

public class Food
    {
        public string name { get; set; }
        public int quantity { get; set; }
    }

    public class CustomerModel {
        public string CustomerName { get; set; }
        public List<Food> ListFoods { get; set; }
    }

    [HttpPost]
    public ActionResult AddCustomer(CustomerModel model)
    {   
        //Add New Person
        return Json(new { Result = "OK" });
    } 

My View :

View Sample

 @using (Ajax.BeginForm("AddCustomer", "Resturant", new AjaxOptions
         {
        InsertionMode = InsertionMode.Replace,
        HttpMethod = "POST",
        OnSuccess = "Success"

         }, new { @id = "frmaddCustomer" }))
{

    <p>
        <label>Customer Name </label>        
    </p>
    <p>
        @Html.CH_TextBox("CustomerName").size(TextBoxSize.Medium)
    </p>
    <p>
        Food List
    </p>
<table id="FoodList" class="static-table" data-bind='visible: gifts().length > 0'>
    <thead>
        <tr>
            <td style="width: 250px;">Name</td>
            <td>Quantity</td>
            <td />
        </tr>
    </thead>
    <tbody data-bind='foreach: gifts'>
        <tr>
            <td>
                @Html.CH_TextBox("Food.name").size(TextBoxSize.Medium)
            </td>
            <td>
                @Html.CH_TextBox("Food.quantity").size(TextBoxSize.Medium)
            <td>
                <a id="deleteRow">Delete</a>
        </tr>
    </tbody>
</table>


    <button data-bind='click: addGift' type="submit">New Food</button>
    <button data-bind='enable: gifts().length > 0' type="submit">Send Data</button>


}
3
  • See my answer here stackoverflow.com/questions/19980232/… Commented Nov 30, 2013 at 16:44
  • Thanks . but I've used @ajax.beginform() and i did not use Jquery.ajax Commented Dec 1, 2013 at 7:04
  • The only way I have been able to send a list was go manually build it and send it. The only way for data to be included in a postback is for it to be included in a for helper (any except display) Commented Dec 1, 2013 at 15:22

1 Answer 1

1

I use the following method to post data to controller model :

    <tr>
        <td>
            @Html.CH_TextBox("Food[0].name").size(TextBoxSize.Medium)
        </td>
        <td>
            @Html.CH_TextBox("Food[0].quantity").size(TextBoxSize.Medium)
         </td>
        <td>
            <a id="deleteRow">Delete</a>
         </td>
    </tr>
    <tr>
        <td>
            @Html.CH_TextBox("Food[1].name").size(TextBoxSize.Medium)
        </td>
        <td>
            @Html.CH_TextBox("Food[1].quantity").size(TextBoxSize.Medium)
         </td>
        <td>
            <a id="deleteRow">Delete</a>
         </td>
    </tr>
    <tr>
        <td>
            @Html.CH_TextBox("Food[2].name").size(TextBoxSize.Medium)
        </td>
        <td>
            @Html.CH_TextBox("Food[2].quantity").size(TextBoxSize.Medium)
         </td>
        <td>
            <a id="deleteRow">Delete</a>
         </td>
    </tr>

by using this method, my problem was solved. Thanks .

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.