none
Which model will select to be base of view Sales Order Entry Form ( SalesFooter Model OR Sales Header Model) ? RRS feed

  • Question


  • 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

    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) :

    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; }

    Thursday, January 17, 2019 11:17 PM

All replies

  • Hi engahmedbarbary,

    According to your description and related code, it seems that it is one to many relationship, if you want to insert records into database, please refer to the following code.

    using (var db = new MyDbContext())
                {
                    SalesHeader salesHeader = new SalesHeader()
                    {
                        SalesOrderNo = 1,
                        SalesYear = 2009,
                        SalesDate = new DateTime(2019, 1, 17),
                        BranchCode = 1,
                        CustomerID = 2509
                    };
    
                    SalesFooter salesFooter1 = new SalesFooter()
                    {
                        SalesLineNo = 1,
                        ItemCode = "12929",
                        Quantity = 5,
                        UnitPrice = 10,
                        Total = 50
                    };
                    SalesFooter salesFooter2 = new SalesFooter()
                    {
                        SalesLineNo = 2,
                        ItemCode = "17918",
                        Quantity = 4,
                        UnitPrice = 5,
                        Total = 20
                    };
    
                    salesHeader.SalesFooters.Add(salesFooter1);
                    salesHeader.SalesFooters.Add(salesFooter2);
    
                    db.SalesHeaders.Add(salesHeader);
                    db.SaveChanges();
                }

    If I misunderstand your issue, please describe it in detailed and feel free let me know.

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, January 18, 2019 8:58 AM
  • thank you for reply

    this is exactly what i need

    but you write static data 

    1- I need to write above code dynamically using forloop or foreach 

    2- also from which place i will get data assigned 

    SalesLineNo = 2

    How to get 2 or how to get this data from controller

    3- from your answer no need to create new class collect data from two models

    and my requirement can help me in that 

    are this correct or not ?


    Friday, January 18, 2019 5:39 PM
  • Actually i Need to modify code under create action result to save sales order

    save sales order meaning insert record on sales header and many records to sales footer according number of lines

    user will write

    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 code as below

    @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>&nbsp;</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>

    Saturday, January 19, 2019 3:27 AM
  • Hi engahmedbarbary,


    >>are this correct or not ?

    if you want to get data from view page to control, it is a asp.net mvc issue, I would suggest that you could post your issue on asp.net forum for suitable support.

    https://forums.asp.net/1146.aspx/1?MVC

    Thanks for your understanding.

    Best regards,

    Zhanglong 


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, January 22, 2019 8:33 AM