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

2 Answers 2

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.

1
  • Awesome! Thank you so much. This works. I appreciate your help.
    – Marshall
    Jul 4, 2017 at 0:45
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

2
  • 1
    Hi Karthik, Thank you for taking time for looking into my post. I appreciate your help in providing me with the detailed information. It helps.
    – Marshall
    Jul 5, 2017 at 3:09
  • My pleasure Marshall.Happy sharing knowledge with others.:) Jul 5, 2017 at 4:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.