Results 1 to 11 of 11

Thread: Which model will select to be base of view Sales Order Entry Form ( SalesFooter Model

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2018
    Posts
    67

    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:
    1. on Header(represent by Sales Header Model)
    2. SalesOrderNO   1
    3. SalesYear      2019
    4. SalesDate      2019-01-17
    5. BranchCode     1
    6. SalesTypeNo    1
    7. CustomerNo     2509
    8. on Footer(represent by Sales Footer Model)
    9. SalesLineNo ItemCode  Quantity  Price Total
    10. 1                  12929         5          10       50
    11. 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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    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.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Apr 2018
    Posts
    67

    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

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    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.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Apr 2018
    Posts
    67

    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>&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>





    <div>
    <a asp-action="Index">Back to List</a>
    </div>
    </div>
    </div>
    @section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
    }

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    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.
    Quote Originally Posted by engbarbary View Post
    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:
    1. public class RepositoryTab<T> : IrepositoryTab<T> where T : class
    2.     {
    3.         protected TabDbContext db { get; set; }
    4.        // private TabDbContext db;
    5.         private DbSet<T> dbSet;
    6.  
    7.         public RepositoryTab(TabDbContext Tabdb)
    8.         {
    9.             db = Tabdb;
    10.             dbSet = db.Set<T>();
    11.         }
    12.  public void Insert(T obj)
    13.         {
    14.             dbSet.Add(obj);
    15.             Save();
    16.         }
    17.  public void Save()
    18.         {
    19.             db.SaveChanges();
    20.         }

    =======SalesOrderController============

    csharp Code:
    1. public class SalesOrderController : Controller
    2.     {
    3.         private readonly IrepositoryTab<SalesHeader> _repository;
    4.         public  SalesOrderController(IrepositoryTab<SalesHeader> SalesHeader)
    5.         {
    6.             this._repository = SalesHeader;
    7.         }
    8.        
    9.         public IActionResult Create()
    10.         {
    11.             return View();
    12.         }
    13.         [HttpPost]
    14.         public IActionResult Create(SalesHeader sh)
    15.         {
    16.            
    17.            
    18.             if (ModelState.IsValid)
    19.             {
    20.               //are save header is ok i dont know
    21.                     SalesHeader order = new SalesHeader { SalesOrderNo = sh.SalesOrderNo, SalesType = sh.SalesType, BranchCode = sh.BranchCode,SalesYear=sh.SalesYear,SalesDate=sh.SalesDate,CustomerID=sh.CustomerID };
    22.                     foreach (var i in sh.SalesFooters)
    23.                     {
    24. //how to save footer
    25.                    i.SalesLineNo=s
    26.                         order.SalesFooters.Add(i);
    27.                     }
    28.                 _repository.Insert(order);
    29.                    
    30.                    
    31.                
    32.             }
    33.            
    34.            //if order saved return message success
    35.         }
    36.     }
    37. }
    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");}
    }

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    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.

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Apr 2018
    Posts
    67

    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

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    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.

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Apr 2018
    Posts
    67

    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

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Apr 2018
    Posts
    67

    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
  •  



Click Here to Expand Forum to Full Width