I'm working on a simple project. Following is the code. The thing is I'm unable to get all the information from the view to the controller. So I already have book ID, Name and price (which is coming from separate Productcontroller) in the Homecontroller and I'm able to view it as well and I want to post the quantity of books when I make a post request. So, when I try to make a post request and debug it, I can only see the quantity and I see null for book ID, Name and price.
Iām not sure what is going wrong. Any help would be really appreciated. Thank You!
1.Product.cs
public class Product
{
public int ProductID { get; set; }
public string ProductName { get; set; }
public decimal ProductPrice { get; set; }
public int Quantity { get; set; }
}
2.Cart.cshtml
@model DAL.Models.Product
@{
ViewBag.Title = "Purchase";
}
@ViewBag.bookName
<h2>Purchase</h2>
<div class="row">
<div class="col-md-3"></div>
<div class="col-md-6">
@using (Html.BeginForm("Cart", "Home", FormMethod.Post))
{
<div class="panel panel-primary">
<div class="panel-heading">Purchase Book</div>
<div class="panel-body">
<div>
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.ProductName)
</dt>
<dd>
@Html.DisplayFor(model => model.ProductName)
</dd>
<dt>
@Html.DisplayNameFor(model => model.ProductPrice)
</dt>
<dd>
@Html.DisplayFor(model => model.ProductPrice)
</dd>
</dl>
</div>
@Html.LabelFor(model => model.Quantity, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Quantity, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Quantity, "", new { @class = "text-danger" })
</div><br />
<div class="col-md-10">
<input type="submit" value="Submit" class="btn btn-default"/>
</div>
</div>
</div>
}
</div>
<div class="col-md-3"></div>
</div>
Following is the screen shot: