-
Jan 17th, 2019, 05:56 PM
#1
Thread Starter
Lively Member
Which model will select to be base of view Sales Order Entry Form ( SalesFooter Model
I work in project work in asp.net core 2.1 visual studio 2017 SQL Server 2012
I actually need to make entry form Sales Order consist from Sales Header Model and Sales Footer model
on Sales Order Entry Form
Sales Order Design Code:
on Header(represent by Sales Header Model) SalesOrderNO 1 SalesYear 2019 SalesDate 2019-01-17 BranchCode 1 SalesTypeNo 1 CustomerNo 2509 on Footer(represent by Sales Footer Model) SalesLineNo ItemCode Quantity Price Total 1 12929 5 10 50 2 17918 4 5 20
Result of Process above when Save the Order
One record added to Sales Header Table.
Two record added to Sales Footer Table.
so that what Model I will select it to be based of Sales Order (Sales Footer Or Sales Header) :
Code:
Sales Header Model
public int SalesOrderNo { get; set; }
public int SalesYear { get; set; }
public int BranchCode { get; set; }
public int SalesType { get; set; }
public DateTime SalesDate { get; set; }
public int CustomerID { get; set; }
public Customer Customers { get; set; }
public ICollection<SalesFooter> SalesFooters { get; set; }
Sales Footer model
public int SalesOrderNo { get; set; }
public int SalesLineNo { get; set; }
public int SalesType { get; set; }
public int BranchCode { get; set; }
public string ItemCode { get; set; }
public decimal Quantity { get; set; }
public decimal UnitPrice { get; set; }
public decimal Total { get; set; }
public Items Itemes { get; set; }
public SalesHeader SaleHeaders { get; set; }
Last edited by engbarbary; Jan 17th, 2019 at 05:58 PM.
Reason: some text not good display
-
Jan 17th, 2019, 07:27 PM
#2
Re: Which model will select to be base of view Sales Order Entry Form ( SalesFooter M
You don't have to use EF entity classes as models in your views. The model for each view should be exactly what that view needs. If that doesn't correspond directly to an entity class then create a new class and map the data from your entities to it and back again.
-
Jan 18th, 2019, 12:13 PM
#3
Thread Starter
Lively Member
Re: Which model will select to be base of view Sales Order Entry Form ( SalesFooter M
can you please tell me how to create a new class and map the data from your entities to it and back again
-
Jan 18th, 2019, 07:36 PM
#4
Re: Which model will select to be base of view Sales Order Entry Form ( SalesFooter M
Nope. If you don't know how to define a class then you haven't spent enough time learning the fundamentals, because that's one of the fundamentals. In that case, I suggest that you follow the tutorial link in my signature below and work through the entire thing. It will teach you, amongst other things, how to define a class. That said, I'm not sure how you can not know how to define your own classes. Those model classes are, after all, classes.
As for mapping the data, you already know how to do that. It's a simple matter of getting the data from the source properties and assigning it to the corresponding destination properties. You could use some sort of existing tool for the task to automate it somewhat, e.g. AutoMapper, but then you need to learn how to use that tool so that goes beyond the scope of this thread.
-
Jan 18th, 2019, 10:07 PM
#5
Thread Starter
Lively Member
Re: Which model will select to be base of view Sales Order Entry Form ( SalesFooter M
thank you for reply i already do sample i only need to save data of sales order
How to save sales order meaning
How to insert data of sales order on Tables Sales Header and Sales Footer to save order (according to data on original post)
===Actually what i need is what i write under Create Action Result on sales ordercontroller to save order
public class RepositoryTab<T> : IrepositoryTab<T> where T : class
{
protected TabDbContext db { get; set; }
// private TabDbContext db;
private DbSet<T> dbSet;
public RepositoryTab(TabDbContext Tabdb)
{
db = Tabdb;
dbSet = db.Set<T>();
}
public void Insert(T obj)
{
dbSet.Add(obj);
Save();
}
public void Save()
{
db.SaveChanges();
}
=======SalesOrderController============
public class SalesOrderController : Controller
{
private readonly IrepositoryTab<SalesHeader> _repository;
public SalesOrderController(IrepositoryTab<SalesHeader> SalesHeader)
{
this._repository = SalesHeader;
}
public IActionResult Create()
{
return View();
}
[HttpPost]
public IActionResult Create(SalesHeader sh)
{
if (ModelState.IsValid)
{
//are save header is ok i dont know
SalesHeader order = new SalesHeader { SalesOrderNo = sh.SalesOrderNo, SalesType = sh.SalesType, BranchCode = sh.BranchCode,SalesYear=sh.SalesYear,SalesDate=sh.SalesDate,CustomerID=sh.CustomerID };
foreach (var i in sh.SalesFooters)
{
//how to save footer
i.SalesLineNo=s
order.SalesFooters.Add(i);
}
_repository.Insert(order);
}
//if order saved return message success
}
}
}
my create view of sales order as following
@model TabDataAccess.Dto.SalesHeader
@{
ViewData["Title"] = "Create";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<link href="~/css/SalesO.css" rel="stylesheet" />
<script src="~/lib/jquery/dist/jquery.js"></script>
<h2>Create</h2>
<h4>SalesHeader</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="Create">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="SalesOrderNo" class="control-label"></label>
<input asp-for="SalesOrderNo" class="form-control" />
<span asp-validation-for="SalesOrderNo" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="SalesYear" class="control-label"></label>
<input asp-for="SalesYear" class="form-control" />
<span asp-validation-for="SalesYear" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="BranchCode" class="control-label"></label>
<input asp-for="BranchCode" class="form-control" />
<span asp-validation-for="BranchCode" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="SalesType" class="control-label"></label>
<input asp-for="SalesType" class="form-control" />
<span asp-validation-for="SalesType" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="SalesDate" class="control-label"></label>
<input asp-for="SalesDate" class="form-control" />
<span asp-validation-for="SalesDate" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="CustomerID" class="control-label"></label>
<select asp-for="CustomerID" class="form-control" asp-items="ViewBag.CustomerID"></select>
</div>
<div class="form-group">
<label asp-for="TotalPrice" class="control-label"></label>
<input asp-for="TotalPrice" class="form-control" />
<span asp-validation-for="TotalPrice" class="text-danger"></span>
</div>
<div class="details">
<h4>Order Items</h4>
<table width="100%">
<tr>
<td>SalesLineNo</td>
<td>ItemCode</td>
<td>Quantity</td>
<td>UnitPrice</td>
<td>Total</td>
<td> </td>
</tr>
<tr>
<td>
<input type="text" id="SalesLineNo" />
<span class="error">Item name required</span>
</td>
<td>
<input type="text" id="ItemCode" />
<span class="error">Valid quantity required</span>
</td>
<td>
<input type="text" id="Qunatity" />
<span class="error">Valid rate required</span>
</td>
<td>
<input type="text" id="UnitPrice" />
<span class="error">Valid rate required</span>
</td>
<td>
<input type="text" id="Total" />
<span class="error">Valid rate required</span>
</td>
<td>
<input type="button" id="add" value="add" />
</td>
</tr>
</table>
<div id="orderItems" class="tablecontainer">
</div>
@*<div style="padding:10px 0px; text-align:right">
<input id="submit" type="button" value="Save" style="padding:10px 20px" />
</div>*@
<div class="form-group">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</form>
<div>
<a asp-action="Index">Back to List</a>
</div>
</div>
</div>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
-
Jan 18th, 2019, 11:14 PM
#6
Re: Which model will select to be base of view Sales Order Entry Form ( SalesFooter M
Please use formatting tags to improve readability of code snippets.
Originally Posted by engbarbary
thank you for reply i already do sample i only need to save data of sales order
How to save sales order meaning
How to insert data of sales order on Tables Sales Header and Sales Footer to save order (according to data on original post)
===Actually what i need is what i write under Create Action Result on sales ordercontroller to save order
csharp Code:
public class RepositoryTab<T> : IrepositoryTab<T> where T : class
{
protected TabDbContext db { get; set; }
// private TabDbContext db;
private DbSet<T> dbSet;
public RepositoryTab(TabDbContext Tabdb)
{
db = Tabdb;
dbSet = db.Set<T>();
}
public void Insert(T obj)
{
dbSet.Add(obj);
Save();
}
public void Save()
{
db.SaveChanges();
}
=======SalesOrderController============
csharp Code:
public class SalesOrderController : Controller
{
private readonly IrepositoryTab<SalesHeader> _repository;
public SalesOrderController(IrepositoryTab<SalesHeader> SalesHeader)
{
this._repository = SalesHeader;
}
public IActionResult Create()
{
return View();
}
[HttpPost]
public IActionResult Create(SalesHeader sh)
{
if (ModelState.IsValid)
{
//are save header is ok i dont know
SalesHeader order = new SalesHeader { SalesOrderNo = sh.SalesOrderNo, SalesType = sh.SalesType, BranchCode = sh.BranchCode,SalesYear=sh.SalesYear,SalesDate=sh.SalesDate,CustomerID=sh.CustomerID };
foreach (var i in sh.SalesFooters)
{
//how to save footer
i.SalesLineNo=s
order.SalesFooters.Add(i);
}
_repository.Insert(order);
}
//if order saved return message success
}
}
}
my create view of sales order as following
HTML Code:
@model TabDataAccess.Dto.SalesHeader
@{
ViewData["Title"] = "Create";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<link href="~/css/SalesO.css" rel="stylesheet" />
<script src="~/lib/jquery/dist/jquery.js"></script>
<h2>Create</h2>
<h4>SalesHeader</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="Create">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="SalesOrderNo" class="control-label"></label>
<input asp-for="SalesOrderNo" class="form-control" />
<span asp-validation-for="SalesOrderNo" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="SalesYear" class="control-label"></label>
<input asp-for="SalesYear" class="form-control" />
<span asp-validation-for="SalesYear" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="BranchCode" class="control-label"></label>
<input asp-for="BranchCode" class="form-control" />
<span asp-validation-for="BranchCode" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="SalesType" class="control-label"></label>
<input asp-for="SalesType" class="form-control" />
<span asp-validation-for="SalesType" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="SalesDate" class="control-label"></label>
<input asp-for="SalesDate" class="form-control" />
<span asp-validation-for="SalesDate" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="CustomerID" class="control-label"></label>
<select asp-for="CustomerID" class="form-control" asp-items="ViewBag.CustomerID"></select>
</div>
<div class="form-group">
<label asp-for="TotalPrice" class="control-label"></label>
<input asp-for="TotalPrice" class="form-control" />
<span asp-validation-for="TotalPrice" class="text-danger"></span>
</div>
<div class="details">
<h4>Order Items</h4>
<table width="100%">
<tr>
<td>SalesLineNo</td>
<td>ItemCode</td>
<td>Quantity</td>
<td>UnitPrice</td>
<td>Total</td>
<td> </td>
</tr>
<tr>
<td>
<input type="text" id="SalesLineNo" />
<span class="error">Item name required</span>
</td>
<td>
<input type="text" id="ItemCode" />
<span class="error">Valid quantity required</span>
</td>
<td>
<input type="text" id="Qunatity" />
<span class="error">Valid rate required</span>
</td>
<td>
<input type="text" id="UnitPrice" />
<span class="error">Valid rate required</span>
</td>
<td>
<input type="text" id="Total" />
<span class="error">Valid rate required</span>
</td>
<td>
<input type="button" id="add" value="add" />
</td>
</tr>
</table>
<div id="orderItems" class="tablecontainer">
</div>
@*<div style="padding:10px 0px; text-align:right">
<input id="submit" type="button" value="Save" style="padding:10px 20px" />
</div>*@
<div class="form-group">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</form>
<div>
<a asp-action="Index">Back to List</a>
</div>
</div>
</div>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
-
Jan 18th, 2019, 11:20 PM
#7
Re: Which model will select to be base of view Sales Order Entry Form ( SalesFooter M
It sounds like you're just asking how to save data to your database. That's a topic that has been covered in many places already so I don't feel the need to repeat what has already been said so many times. If you try to implement something and strike a specific issue then I'll be happy to help with that but I tend not to spend my time explaining things that people can easily research for themselves. If the problem is that you want to save to two different tables then there's no problem, because it's just saving to one table twice. That said, you may want to use a transaction to ensure that either both succeed or both fail. Using transactions in ADO.NET is also something you can research.
-
Jan 19th, 2019, 08:08 AM
#8
Thread Starter
Lively Member
Re: Which model will select to be base of view Sales Order Entry Form ( SalesFooter M
thank you for reply
there are code written under create action
i need only some help in that to save data on two tables sales header and footer
-
Jan 19th, 2019, 08:51 AM
#9
Re: Which model will select to be base of view Sales Order Entry Form ( SalesFooter M
Like I said, if you know how to save data to one table then you know how to save data to two tables because it's just saving data to one table twice. The only difference is that you might want to use a transaction. What have you done and where EXACTLY are you stuck? If you haven't tried anything then there's nothing for me to help with. Like I said, there's no need for me to repeat what is already out there many times already.
-
Jan 19th, 2019, 10:20 AM
#10
Thread Starter
Lively Member
Re: Which model will select to be base of view Sales Order Entry Form ( SalesFooter M
see hereif possible
public IActionResult Create(SalesHeader sh)
{
if (ModelState.IsValid)
{
//are save header is ok i dont know
SalesHeader order = new SalesHeader { SalesOrderNo = sh.SalesOrderNo, SalesType = sh.SalesType, BranchCode = sh.BranchCode,SalesYear=sh.SalesYear,SalesDate=sh.SalesDate,CustomerID=sh.CustomerID };
foreach (var i in sh.SalesFooters)
{
//how to save footer
i.SalesLineNo=s
order.SalesFooters.Add(i);
}
_repository.Insert(order);
}
//if order saved return message success
}
}
}
this is correct
-
Jan 19th, 2019, 10:22 AM
#11
Thread Starter
Lively Member
Re: Which model will select to be base of view Sales Order Entry Form ( SalesFooter M
i can save data on sales header but main problem how to save data on sales footer
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|