My main point is to add multiple item rows at the same time in a View. I have tried alot of different ways and I would like some help.
I appreciate suggestions on how to fix this issue. I am grateful for everything. Code-reviews, feedback, links, guides, information, help, messages. Anything related to this issue.
Here is my final code.
public class Order
{
public int OrderID { get; set; }
public int EmployeeID { get; set; }
public virtual Employee Employee { get; set; }
public virtual ICollection<OrderRowDetails> OrderRowDetails { get; set; }
}
Employee Model:
public class Employee
{
public int EmployeeID { get; set; }
public string EmployeeName { get; set; }
}
OrderRowDetails:
public class OrderRowDetails
{
public int OrderRowDetailsID { get; set; }
public int OrderID { get; set; }
public int ProductID { get; set; }
public virtual Product Product { get; set; }
public int Quantity { get; set; }
}
public class Product
{
public int ProductID { get; set; }
public string ProductName { get; set; }
public int ProductTypeID { get; set; }
public virtual ProductType ProductType { get; set; }
}
public class ProductType
{
public int ProductTypeID { get; set; }
public string ProductTypeName { get; set; }
}
Now I use a View Model:
public class OrderViewModel
{
public int OrderID { get; set; }
public int EmployeeID { get; set; }
public IEnumerable<OrderRowDetails> OrderRowDetails { get; set; }
public IEnumerable<Employee> Employees { get; set; }
public int OrderRowDetailsID { get; set; }
public int ProductID1 { get; set; }
public IEnumerable<Product> Products { get; set; }
public int Quantity1 { get; set; }
public int ProductID2 { get; set; }
public int Quantity2 { get; set; }
public int ProductID3 { get; set; }
public int Quantity3 { get; set; }
public OrderViewModel()
{
OrderRowDetails = new List<OrderRowDetails>();
}
This is my Create View:
@model TheProject1.ViewModel.OrderViewModel
<table>
<tr>
<th>Employee ID: (1)</th>
<td>@Html.TextBoxFor(m => m.EmployeeID)</td>
</tr>
<tr>
<th>Product ID: (1-2)</th>
<td>@Html.EditorFor(m => m.ProductID1)</td>
</tr>
<tr>
<th>Quantity</th>
<td>@Html.EditorFor(m => m.Quantity1)</td>
</tr>
<tr>
<th>Product ID: (1-2)</th>
<td>@Html.EditorFor(m => m.ProductID2)</td>
</tr>
<tr>
<th>Quantity</th>
<td>@Html.EditorFor(m => m.Quantity2)</td>
</tr>
<tr>
<th>Product ID: (1-2)</th>
<td>@Html.EditorFor(m => m.ProductID3)</td>
</tr>
<tr>
<th>Quantity</th>
<td>@Html.EditorFor(m => m.Quantity3)</td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Save" /></td>
</tr>
</table>
Controller:
public ActionResult Create()
{
return View();
}
public ActionResult Create(OrderViewModel orderView, Order order, OrderRowDetails orderRowDetails)
{
var theorder = new Order()
{
EmployeeID = orderView.EmployeeID,
OrderID = orderView.OrderID,
OrderRowDetails = new List<OrderRowDetails>()
{
new OrderRowDetails()
{
OrderID = orderView.OrderID,
ProductID = orderView.ProductID1,
Quantity = orderView.Quantity1
},
new OrderRowDetails()
{
OrderID = orderView.OrderID,
ProductID = orderView.ProductID2,
Quantity = orderView.Quantity2
},
new OrderRowDetails()
{
OrderID = orderView.OrderID,
ProductID = orderView.ProductID3,
Quantity = orderView.Quantity3
},
}
};
db.Orders.Add(theorder);
db.SaveChanges();
// OrderID == OrderID
return RedirectToAction("Index", "Order");
I have lost is o much so my idea was to create 5 rows and hence I cant have nulls. My idea was to create an alternative that product ID = 1 is "No Value" And before Adding to Database I would delete row where ProductID 1 from OrderRowDetails table.
<tr>
<th>Product ID: (1-2)</th>
<td>@Html.EditorFor(m => m.ProductID)</td>
</tr>
<tr>
<th>Quantity</th>
<td>@Html.EditorFor(m => m.Quantity)</td>
</tr>
<tr>
<th>Product ID: (1-2)</th>
<td>@Html.EditorFor(m => m.ProductID)</td>
</tr>
<tr>
<th>Quantity</th>
<td>@Html.EditorFor(m => m.Quantity)</td>
</tr>
If I do this I get the values from the first OrderRow, 2 times.
My goal is to add multiple items in the list(OrderRowDetails) with the relationshop to Order (with OrderID).
I would really love if somebody could give me any information or teach me how to solve this.