pius joshua

pius joshua

  • 1.4k
  • 29
  • 5.7k

How to display two models with single view using asp.net core 6.0

May 30 2022 9:16 AM

Hi, Good morning all, I am new to asp.net core 6.0 and i have this issues...

I have this model Called OrderModel, inside it  I have order class and orderDetails Class

public class Order
{
    [Key]
    public int OrderId { get; set; }



    [Display(Name = "Customer Name")]
    [Required(ErrorMessage = "Please Select Customer Name")]
    public String CustomerName { get; set; }

    public ICollection<OrderDetail>? OrderDetails { get; set; }

}

public class OrderDetail
{
    [Key]
    public int DetailId { get; set; }
    public int OrderId { get; set; }
    [Display(Name = "Service")]
    public string ServiceName { get; set; }

    [Display(Name = "Quantity")]
    public int Quantity { get; set; }

    [Display(Name ="Unit Price")]
    public float UnitPrice { get; set; }

    [ForeignKey("OrderId")]
    public Order? Order { get; set; }
}

//On my view, I want to be able to display the Order Class as a form and the OrderDetails as an editable table with 10 rows ..

public IActionResult Customer_Order()
{
    // I want to be able to display upto 10 rows..
    List<OrderDetail> od = new List<OrderDetail>();
    od.Add(new OrderDetail  { DetailId = 0, Discount = 0, Quantity = 0, ServiceName = "Select Service", UnitPrice = 0, TotalAmount = 0 });
    od.Add(new OrderDetail { DetailId = 0, Discount = 0, Quantity = 0, ServiceName = "Select Service", UnitPrice = 0, TotalAmount = 0 });
    return View(od);
}

On Post, I want to be able to save the data to both order table and orderdetails table

<!---- My Customer_Order View-->
@model Customer.Models.Order


<form asp-action="Customer_Order">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
             
            <div class="form-group row">
               
                <div class="col-md-5">
                 
                <label asp-for="CustomerName" class="control-label"></label>
                <select asp-for="CustomerName"  class="dropdown form-control" asp-items="ViewBag.CustomerID">
                     <option value="" selected disabled>---Customer---</option>
                </select>
             <span asp-validation-for="CustomerID" class="text-danger"></span>
                </div>
                
          
            
            <div class="form-group">
             <table class="table table-striped table-bordered display">
    <thead>
        <tr>
            <th>
                SN
            </th>
            <th>
                Services
            </th>
            <th>
                Qty/Hrs
            </th>
            <th>
                Unit Price
            </th>
          
            <th>
                Total
            </th>
        </tr>
    </thead>
    <tbody>
                                        
                   int j = 0;
                 @foreach (var item in Model.OrderDetails)
                 {
                   
                     <tr>
                         <td>

                         </td>
                         <td>
                             @Html.DropDownListFor(model=>item.ServiceName, ViewBag.ServiceID as List<SelectListItem>, "Please Select", new{@class ="form-control dropdownlist select2"})

                         </td>
                         <td>
                             @Html.EditorFor(model=>item.Quantity) 
                         </td>
                         <td>
                             @Html.EditorFor(model=>item.UnitPrice)
                         </td>
                        
                         <td>
                             @Html.EditorFor(model=>item.TotalAmount)
                         </td>
                     </tr>
                 }
             
         </tbody>
    </table>
            </div>
            
            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-primary" />
            </div>
        </form>

How do i display data into

Thank you very much for your lovely time..


Answers (3)