I'm making a MVC Application & i want to ask how to insert multiple record on a table from single view.
In My Model there are tables like Customer,Login,CustomerData and etc.
Only CustomerData Tables are having Multiple rows other tables are single rows at single time.
Below is my Code Section.
From this Code i can get the single/First row in Customerdata but i want this to modified for multiple Rows insert in CustomerData.
Any Help would be appreciated.
Thanks.
View Model:
public class MultipleModel
{
public Customer Customer { get; set; }
public CustomerData CustomerData { get; set; }
public SalesMan SalesMan { get; set; }
public Login Login { get; set; }
public AccountMaster AccountMaster { get; set; }
public CustomerProgress CustomerProgress { get; set; }
public CustomerStatu CustomerStatus { get; set; }
public List<CustomerData> CData { get; set;}
}
Razor:
@model SerbaantikCRM.Models.MultipleModel
FYI In This View there are other Controls in which I'm using other model (like Salesman and Customer and etc)
<div id="ProData" class="Table">
<div id="RowData" class="Row">
<div class="Cell">
@Html.EditorFor(m => m.CustomerData.ProductCode, new { htmlAttributes = new { @class = "Repeat", id = "PCode", Type = "Text", placeholder = "ProductName" } })
</div>
<div class="Cell">
@Html.EditorFor(m => m.CustomerData.Qty, new { htmlAttributes = new { @class = "Qty", id = "Qty", Type = "Text", placeholder = "Qty" } })
</div>
<div class="Cell">
@Html.EditorFor(m => m.CustomerData.Price, new { htmlAttributes = new { @class = "Price", id = "Price", Type = "Text", placeholder = "Price" } })
</div>
<div class="Cell">
@Html.EditorFor(m => m.CustomerData.Disc, new { htmlAttributes = new { @class = "Disc", id = "Disc", Type = "Text", placeholder = "Discount" } })
</div>
<div class="Cell">
@Html.EditorFor(m => m.CustomerData.Amount, new { htmlAttributes = new { @class = "TotalAmount", disabled = "disabled", id = "TotalAmount", Type = "Text", placeholder = "Total Amount" } })
</div>
<div class="Cell">
@Html.EditorFor(m => m.CustomerData.Remarks, new { htmlAttributes = new { @class = "Repeat", placeholder = "Remarks" } })
</div>
<div class="Cell">
<a id="addRow">
<i style="vertical-align:middle;color:green;padding:10px; font-size: 32px" class="fa fa-plus-square"></i>
</a>
</div>
</div>
Controller
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult NewEntry(MultipleModel md)
{
using (ScriptEntities db = new ScriptEntities())
{
Customer cust = new Customer();
cust.Address = md.Customer.Address;
cust.Company = md.Customer.Company;
cust.Date_ = DateTime.Today;
cust.Email = md.Customer.Email;
cust.FocusCode = md.Customer.FocusCode;
cust.Mb1 = md.Customer.Mb1;
cust.Mb2 = md.Customer.Mb2;
cust.Name = md.Customer.Name;
cust.Remarks = md.Customer.Remarks;
cust.Telp1 = md.Customer.Telp1;
cust.NextDate = md.Customer.Date_;
CustomerData custd= new CustomerData();
custd.Amount = md.CustomerData.Amount;
custd.Date_ = md.CustomerData.Date_;
custd.Disc = md.CustomerData.Disc;
custd.Flag1 = md.CustomerData.Flag1;
custd.Flag2 = md.CustomerData.Flag2;
custd.Net = md.CustomerData.Net;
custd.Notes = md.CustomerData.Notes;
custd.Price = md.CustomerData.Price;
custd.ProductCode = md.CustomerData.ProductCode;
custd.Qty = md.CustomerData.Qty;
custd.Remarks = md.CustomerData.Remarks;
custd.Salesman = md.CustomerData.Salesman;
try
{
db.Customers.Add(cust);
db.SaveChanges();
custd.CustomerId = cust.Id;
db.CustomerDatas.Add(custd);
db.SaveChanges();
}
catch(Exception ex)
{
throw ex;
}
finally
{
var SalesVar = db.SalesMen.ToList();
SelectList SalesList = new SelectList(SalesVar, "MasterId", "Name");
ViewBag.SalesBG = SalesList;
var StatusVar = db.CustomerStatus.ToList();
SelectList StatusList = new SelectList(StatusVar, "Id", "Status");
ViewBag.StatusBG = StatusList;
var ProgVar = db.CustomerProgresses.ToList();
SelectList ProgList = new SelectList(ProgVar, "Id", "Status");
ViewBag.ProgBG = ProgList;
}
}
return View();
}
Jquery
<script>
$('#addRow').on('click', function () {
$('#ProData').append('<div id="RowData" class="Row"><div class="Cell">@Html.EditorFor(m => m.CustomerData.ProductCode,new { htmlAttributes = new {id="PCode", Type = "Text", placeholder = "ProductName" } })</div><div class="Cell">@Html.EditorFor(m => m.CustomerData.Qty, new { htmlAttributes = new { @class = "Qty", id = "Qty", Type = "Text", placeholder = "Qty" } })</div><div class="Cell">@Html.EditorFor(m => m.CustomerData.Price, new { htmlAttributes = new { @class = "Price", id = "Price", Type = "Text", placeholder = "Price" } })</div><div class="Cell">@Html.EditorFor(m => m.CustomerData.Disc, new { htmlAttributes = new { @class = "Disc", id = "Disc", Type = "Text", placeholder = "Discount" } })</div><div class="Cell">@Html.EditorFor(m => m.CustomerData.Amount, new { htmlAttributes = new { @class = "TotalAmount", disabled = "disabled", id = "TotalAmount", Type = "Text", placeholder = "Total Amount" } })</div><div class="Cell">@Html.EditorFor(m => m.CustomerData.Remarks, 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> ');
$('#ProData').addClass();
return false; //prevent form submission
});
$('#ProData').on('click', '#deleteRow', function () {
$(this).parent().remove();
return false; //prevent form submission
});
</script>