Using Html BeginCollectionItem to insert multiple records in Database in ASP.Net MVC

Last Reply 16 days ago By dharmendr

Posted 16 days ago

I want to save multiple record for CustomerData model but the data I'm getting on submit is only single record even though I'm using html helper beginCollection.

Desired functionality is when click submit button both the records will go to controller along with other model details.

View: 

@using MyModel.Models;
@model CustomerViewModel

<div>
    <fieldset>
        <legend>Purchased Products Detail</legend>
        <div class="row">
            @Html.EditorFor(m => m.Transaction.Net, new { htmlAttributes = new { disabled = "disabled", id = "NetAmt", Type = "Text", placeholder = "Net" } })
        </div>
            @{ Html.RenderPartial("_Purchase");}
        <input type="submit" value="Save" />
    </fieldset>
</div>

 Partial View: 

@using HtmlHelpers.BeginCollectionItem;
@using SerbaantikCRM.Models;
@model CustomerViewModel


@using (Html.BeginCollectionItem("Product"))
{
<div id="ProData">
 <div id="RowData" class="Row">
 <div class="Cell">
 @Html.EditorFor(m => m.PurchasedItem.Product, new { htmlAttributes = new { id = "PCode", Type = "Text", placeholder = "ProductName" } })
 </div>
 <div class="Cell">
 @Html.EditorFor(m => m.PurchasedItem.Qty, new { htmlAttributes = new { @class = "Qty", id = "Qty", Type = "Text", placeholder = "Qty" } })
 </div>
 <div class="Cell">
 @Html.EditorFor(m => m.PurchasedItem.Price, new { htmlAttributes = new { @class = "Price", id = "Price", Type = "Text", placeholder = "Price" } })
 </div>
 <div class="Cell">
 @Html.EditorFor(m => m.PurchasedItem.Disc, new { htmlAttributes = new { @class = "Disc", id = "Disc", Type = "Text", placeholder = "Discount" } })
 </div>
 <div class="Cell">
 @Html.EditorFor(m => m.PurchasedItem.Total, new { htmlAttributes = new { @class = "TotalAmount", disabled = "disabled", id = "TotalAmount", Type = "Text", placeholder = "Total Amount" } })
 </div>
 <div class="Cell">
 @Html.EditorFor(m => m.PurchasedItem.ProductRemarks, new { htmlAttributes = new { @class = "Repeat", placeholder = "Remarks" } })
 </div>
 <div class="Cell">
 <a id="addRow"><i style="color:green;vertical-align:middle;padding:10px;font-size: 32px" class="fa fa-plus-square"></i></a>
 </div>
 </div>
 </div>
}

@Scripts.Render("~/wwwroot/js/customer.js")
@Scripts.Render("~/wwwroot/lib/jquery/dist/jquery.min.js")
<script>
 $('#addRow').on('click', function () {
 $('#ProData').append('<div id="RowData" class="Row"><div class="Cell">@Html.EditorFor(m => m.PurchasedItem.Product, new { htmlAttributes = new { id = "PCode", Type = "Text", placeholder = "ProductName" } })</div><div class="Cell">@Html.EditorFor(m => m.PurchasedItem.Qty, new { htmlAttributes = new { @class = "Qty", id = "Qty", Type = "Text", placeholder = "Qty" } })</div><div class="Cell">@Html.EditorFor(m => m.PurchasedItem.Price, new { htmlAttributes = new { @class = "Price", id = "Price", Type = "Text", placeholder = "Price" } })</div><div class="Cell">@Html.EditorFor(m => m.PurchasedItem.Disc, new { htmlAttributes = new { @class = "Disc", id = "Disc", Type = "Text", placeholder = "Discount" } })</div><div class="Cell">@Html.EditorFor(m => m.PurchasedItem.Total, new { htmlAttributes = new { @class = "TotalAmount", disabled = "disabled", id = "TotalAmount", Type = "Text", placeholder = "Total Amount" } })</div><div class="Cell">@Html.EditorFor(m => m.PurchasedItem.ProductRemarks, new { htmlAttributes = new { @class = "Repeat", placeholder = "Remarks" } })</div><a id="deleteRow"><i style="vertical-align:middle;padding:10px;font-size: 32px" class="fa fa-minus-square"></i></a></div> ');
 return false; //prevent form submission
 });
</script>
 ViewModel:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MyModel.Models
{
    public class CustomerViewModel
    {
        public CustomerModel CustomerModel { get; set; }
        public Transaction Transaction { get; set; }
        //public IEnumerable PurchasedItems { get; set; }
        public PurchasedItem PurchasedItem { get; set; }
    }
}

Models: 

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace MyModel.Models
{
    public class CustomerModel
    {
        public int Id { get; set; }

        [Required(ErrorMessage = "Contact Name required!", AllowEmptyStrings = false)]
        public string Name { get; set; }

        [Required(ErrorMessage = "Company Name required!", AllowEmptyStrings = false)]
        public string Company { get; set; }
        public string Email { get; set; }
        public string Address { get; set; }

        [Required(ErrorMessage = "Contact Number required!", AllowEmptyStrings = false)]
        public string Mb1 { get; set; }
        public string Mb2 { get; set; }
        public string Telp1 { get; set; }
        public Nullable FocusCode { get; set; }
        public Nullable Date_ { get; set; }
        public string Remarks { get; set; }

        [Required(ErrorMessage = "Contact Origin required!", AllowEmptyStrings = false)]
        public string Origin { get; set; }
        public string Type_ { get; set; }
        public Nullable TypeId_ { get; set; }
        public string Fax { get; set; }

        [Required(ErrorMessage = "Allotment of Sales Person is required!", AllowEmptyStrings = false)]

        public string Salesman { get; set; }
        public int SalesManId { get; set; }

        public virtual Transaction[] Transactions { get; set; }
    }
    public class Transaction
    {
        public int TransactionId { get; set; }

        public string TranSalesName { get; set; }
        public Nullable TranSalesMan { get; set; }
        public string voucherno { get; set; }

        public string Progress { get; set; }
        public int ProgressId { get; set; }

        public string Status { get; set; }
        public int StatusId { get; set; }

        public decimal Net { get; set; }

        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
        public Nullable NextAppointmentDate { get; set; }

        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
        public DateTime Date_ { get; set; }

        public string AppointmentRemarks { get; set; }
        public int CustomerId { get; set; }

        public virtual PurchasedItem[] PurchasedItems { get; set; }
    }
    public class PurchasedItem
    {
        public string Product { get; set; }
        public decimal Qty { get; set; }
        public decimal Price { get; set; }
        public decimal Disc { get; set; }
        public decimal Total { get; set; }
        public string ProductRemarks { get; set; }
    }
      
}

Kindly help me out of this problem.

Posted 16 days ago

I am checking. I will get back to you soon.


I agree, here is the link: https://www.e-iceblue.com/Introduce/spire-office-for-net-free.html