Can someone tell what is wrong in my code? I've been searching several days a solution for this, but can't find any. Values from Form doesn't pass to view, only values that are "hardcoded" like OrderDate, CompanyID and so on, is added to database. I'm making my first MVC onlineshop and struggling with it quite a lot.
EDIT: Data to view is coming from db.Selection but I need to save data to model db.Cart with additional parameters. Maybe FormCollection is not right way to do this, but what is?
NVCONTROLLER:
public ActionResult Index()
{
return View(db.Selection);
}
public ActionResult AddtoCart()
{
return View("AddtoCart");
}
[HttpPost]
[ValidateAntiForgeryToken()]
public ActionResult AddtoCart(FormCollection form)
{
string Author = form["Author"];
string ISBN = form["ISBN"];
string BookName = form["BookName"];
string Publisher = form["Publisher"];
string Price = form["Price"];
var cart = new Cart();
cart.Orderdate = DateTime.Now;
cart.CompanyID = 12345;
cart.ISBN = Convert.ToInt32(ISBN);
cart.BookName = BookName;
cart.Price = Convert.ToInt32(Price);
cart.IsInCart = true;
cart.Author = Author;
cart.Publisher = Publisher;
cart.SentToJvs = false;
cart.Reference = "NV kevät 1999";
db.Cart.Add(cart);
db.SaveChanges();
return RedirectToAction("Index");
}
VIEW
@model IEnumerable<STGchannelMVC.Models.Selection>
@using STGchannelMVC.Controllers
@{
ViewBag.Title = "Index";
}
@using (Html.BeginForm("AddtoCart", "NV", FormMethod.Post))
{
<p>
Lisää tuotteet ostoskoriin
</p>
@Html.AntiForgeryToken()
<div class="form-group">
<table class="table">
<tr>
<th hidden>
@Html.DisplayNameFor(model => model.BookID)
</th>
<th>
@Html.DisplayNameFor(model => model.ISBN)
</th>
<th>
@Html.DisplayNameFor(model => model.Author)
</th>
<th>
@Html.DisplayNameFor(model => model.BookName)
</th>
<th>
@Html.DisplayNameFor(model => model.Publisher)
</th>
<th>
@Html.DisplayNameFor(model => model.Price)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td hidden>
@Html.DisplayFor(modelItem => item.BookID)
</td>
<td>
@Html.DisplayFor(modelItem => item.ISBN)
</td>
<td>
@Html.DisplayFor(modelItem => item.Author)
</td>
<td>
@Html.DisplayFor(modelItem => item.BookName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Publisher)
</td>
<td>
@Html.DisplayFor(modelItem => item.Price)
</td>
<td>
<button type="submit" class="btn btn-primary">Add to Cart</button>
</td>
</tr>
}
</table>
</div>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
MODEL
public class Cart
{
public int OrderID { get; set; }
public Nullable<System.DateTime> Orderdate { get; set; }
[Display(Name = "CompanyID")]
public Nullable<long> CompanyID { get; set; }
[Display(Name = "ISBN")]
public Nullable<long> ISBN { get; set; }
[Display(Name = "BookName")]
public string BookName { get; set; }
[Display(Name = "Price")]
public Nullable<decimal> Price { get; set; }
public Nullable<bool> IsInCart { get; set; }
[Display(Name = "Author")]
public string Author { get; set; }
[Display(Name = "Publisher")]
public string Publisher { get; set; }
public Nullable<bool> SentToJvs { get; set; }
public string Reference { get; set; }
}