So yeah im trying to make a Create view for my "Product" and Im using a Collection model to get both Product and Customer in the same view. The only way i really know how to get the value is to do a foreach but that doesnt really work here :P The only search ive found are people saying to use model => model.product.Name etc, but that doesnt work for me maybe ive missed something?
What im going for is that i want to add a product to the customer thats selected in the dropdownlist. Will this code be enough for it or what do i need to have in mind?
EDIT Forgot to say that im trying to do like @Html.EditorFor(model => model.product.Name) but a working one and that handles Create well :s
@model Co56_Invoice_.Models.Collection
<h2>Skapa ny produkt</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
<div class="col-md-10">
@Html.DropDownList("Kunder",
Model.customer.Select(x => new SelectListItem()
{ Text = x.Name.ToString(), Value = x.CustomerID.ToString() }).ToList()
, "Välj kund")
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
public ActionResult Create()
{
col.customer = (from o in db.Customers select o).ToList();
col.product = (from o in db.Products select o).ToList();
return View(col);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "CustomerID,ProductID,Name,StartDate,Interval,Price,YearPrice,TotalPrice,Category,Paid")] Product product)
{
try
{
if (ModelState.IsValid)
{
db.Products.Add(product);
db.SaveChanges();
return RedirectToAction("Index");
}
}
catch(Exception e)
{
throw e.InnerException;
}
return View(product);
}