I have been working on this and have been searching for hours and still can not figure out a solution.
I am trying to pass the checked checkbox values from my view2 to my view1. Would appreciate any help I can get.
ViewModel1:
public class MenuItemViewModel
{
public int MenuId { get; set; }
public double ItemPrice { get; set; }
public string ItemName { get; set; }
public bool Selected { get; set; }
public virtual ICollection<IngredientViewModel> Ingredients { get; set;}
}
ViewModel2:
public class OrderViewModel
{
public int OrderId { get; set; }
public int TableNum { get; set; }
public string Notes { get; set; }
public double Discount { get; set; }
public virtual ICollection<MenuItemViewModel> MenuItem { get; set; }
}
View1:
@model IEnumerable<Final_POS.Models.Order>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Employee.EmpName)
</th>
<th>
@Html.DisplayNameFor(model => model.TableNum)
</th>
<th>
+
@Html.DisplayNameFor(model => model.Discount)
</th>
<th>
@Html.DisplayNameFor(model => model.MenuItems)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Employee.EmpName)
</td>
<td>
@Html.DisplayFor(modelItem => item.TableNum)
</td>
<td>
@Html.DisplayFor(modelItem => item.Discount)
</td>
<td>
@Html.EditorFor(modelItem => item.MenuItems)
</td>
<td>
@Html.ActionLink("Edit", "AsoociateMenuItems", new { id=item.OrderId }) |
@Html.ActionLink("Details", "Details", new { id=item.OrderId }) |
@Html.ActionLink("Delete", "Delete", new { id=item.OrderId })
</td>
</tr>
}
</table>
View2:
@model Final_POS.Models.ViewModel.OrderViewModel
@{
ViewBag.Title = "AsoociateMenuItems";
}
<h2>AsoociateMenuItems</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>OrderViewModel</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.OrderId, new { htmlAttributes = new { @class = "form-control" } })
<div class="form-group">
@Html.LabelFor(model => model.TableNum, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.TableNum, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.TableNum, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.HiddenFor(model => model.Notes, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.HiddenFor(model => model.Notes, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Notes, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Discount, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Discount, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Discount, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.EmployeeEmpId, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.EmployeeEmpId, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.EmployeeEmpId, "", new { @class = "text-danger" })
</div>
</div>
@Html.EditorFor(model => model.MenuItem)
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
This next code snippet is being used by my view2 in this line @Html.EditorFor(model => model.MenuItem) MenuItemViewModel:
@model Final_POS.Models.ViewModel.MenuItemViewModel
<fieldset>
@Html.HiddenFor(model => model.MenuId)
@Html.CheckBoxFor(model => model.Selected)
@Html.DisplayFor(model => model.ItemName)
@Html.DisplayFor(model => model.ItemPrice)
</fieldset>
Controller:
public ActionResult AsoociateMenuItems(int? id)
{
Order _order = db.Orders.Find(id);
if (_order == null)
{
return HttpNotFound();
}
OrderViewModel _orderViewModel = new OrderViewModel()
{
OrderId = _order.OrderId,
Discount = _order.Discount,
TableNum = _order.TableNum,
EmployeeEmpId = _order.EmployeeEmpId
};
List<MenuItemViewModel> _menuItemViewModel = new List<MenuItemViewModel>();
foreach (MenuItem menuItem in db.MenuItems)
{
_menuItemViewModel.Add(new MenuItemViewModel()
{
MenuId = menuItem.MenuId,
ItemName = menuItem.ItemName,
ItemPrice = menuItem.ItemPrice,
Selected = _order.MenuItems.Contains(menuItem)
});
}
_orderViewModel.MenuItem = _menuItemViewModel;
return View(_orderViewModel);
}
[HttpPost]
public ActionResult AsoociateMenuItems(OrderViewModel _orderViewModel)
{
Order _order = db.Orders.Find(_orderViewModel.OrderId);
_order.MenuItems.Clear();
foreach (MenuItemViewModel _menuItemViewModel in _orderViewModel.MenuItem)
{
if (_menuItemViewModel.Selected)
{
MenuItem _menuItem = db.MenuItems.Find(_menuItemViewModel.MenuId);
_order.MenuItems.Add(_menuItem);
}
}
db.SaveChanges();
return RedirectToAction("Index");
}
Aucun commentaire:
Enregistrer un commentaire