Shashank Kumar
Shashank Kumar

Reputation: 1220

Submit Partial View Data with main form in asp.net MVC

How Do I submit Data in Partial View on submit on Main View.the partial view doesn't have a separate submit button

I'm trying to Add Multiple Products to in a single Order.Each Product is Partial View. User can add multiple Products on click of "plus sign". I want all Products to submitted when I submit the Order. My Place Order View is

@model MyApp.Models.Order_Detail
       @section scriptsection {
        <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
        <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
        <script src="../../Scripts/jquery-1.4.4.min.js" type="text/javascript"></script>
        <script type="text/javascript">
            $(document).ready(function () {
                $(".addProduct").live("click", function () {
                    $.get("AddProduct",
                    function (data) {
                        $("#Products").append(data);
                        HideshowMinusButton();
                    });
                });
        </script>
    }
    @using (Html.BeginForm()) {
        @Html.ValidationSummary(true)
        <fieldset>
            <legend>Order</legend>

            <div class="editor-label">
                Customer
            </div>
            <div class="editor-field">
                @Html.DropDownListFor(c=>c.Order.CustomerID,(List<SelectListItem>)ViewBag.Customers)
            </div>
            <div>
                <fieldset id="Products">
                    <legend>Products</legend>
                    @Html.Partial("AddProduct")
                </fieldset>
            </div>
            <div class="editor-label">
                @Html.LabelFor(model => model.Order.OrderDate)
            </div>
            <p>
               <input type="submit" value="Create" />
            </p>
        </fieldset>

On Submit Button Click How Do I get all Products in my Order?

[HttpPost]
public ActionResult PlaceOrder(Order_detail Ord) //Ord Doesn't Contain Products ??
{
//Add Order to Db
return View("Success");

}

Upvotes: 1

Views: 7182

Answers (2)

KNC
KNC

Reputation: 104

You can use form collection in the post method to get the data in the partial view.

[HttpPost]
public ActionResult PlaceOrder(Order_detail Ord, FormCollection fc)
{
  string value = fc.getValue("textBoxName");
}

You will get the value in form collection only if the control has name property.

Ex:

<input name="users" type="text"/>

then

String Value = fc.getvalue("users");

Upvotes: 0

Matt Tabor
Matt Tabor

Reputation: 1053

have a look at this page, i think it will be able to help you.

http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx/

you can change your action method to

public ActionResult PlaceOrder(Order_detail Ord, Products[] products)

or use http://www.nuget.org/packages/BeginCollectionItem/ to model bind into the Order_detail model.

Upvotes: 2

Related Questions