2

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:

This is the screenshot of what I'm trying to do

Marshall
  • 143
  • 4
  • 13

2 Answers2

1

Only editable values will be included in the POST (where you are using @Html.EditorFor etc). To include other values in the POST, simply use the @Html.HiddenFor() helper.

For example:

@Html.HiddenFor(model => model.ProductID)
@Html.HiddenFor(model => model.ProductName)
@Html.HiddenFor(model => model.ProductPrice)

These will then be passed back and available in the controller.

Doug Knudsen
  • 935
  • 9
  • 15
1

Hi Marshall I hope you got the solution from doug knudsen answer above, so here i would like show you the way ,Why DisplayFor does not post values to Action Method?

Short Answer: DisplayFor does not render an input field for the property,whereas editorfor and hiddenfor render an input field. This is the reason you didn't get the value in the post.

Hope it helped you to understand more.

Thanks

Karthik

The_Outsider
  • 1,875
  • 2
  • 24
  • 42
Karthik Elumalai
  • 1,574
  • 1
  • 11
  • 12