1

When passing a model from view to controller-action, all the properties returned are 0s. Please see image attached. Returned model

Action defined in OrderController.cs

[HttpPost]
public ActionResult Update(Order_Detail m)
{
    return View();
    //OrderUpdateViewModel model = new OrderUpdateViewModel();
    //model.hasError = true;
    //model.Title = m.OrderDetail.Title + " Edit Status";
}

View file Edit.cshtml:

@model ADO_Fan.Models.OrderDetailViewModel
@using (Html.BeginForm("Update", "Order", FormMethod.Post))
{
    <div>
    @Html.LabelFor(m => m.OrderDetail.OrderID) @Html.DisplayFor(m => m.OrderDetail.OrderID)<br />
    @Html.LabelFor(m => m.OrderDetail.Product.ProductName)  @Html.DisplayFor(m => m.OrderDetail.Product.ProductName)<br />
    @Html.LabelFor(m => m.OrderDetail.UnitPrice) @Html.DisplayFor(m => m.OrderDetail.UnitPrice)<br />
    @Html.LabelFor(m => m.OrderDetail.Quantity) @Html.TextBoxFor(m => m.OrderDetail.Quantity)<br />
    @Html.LabelFor(m => m.OrderDetail.Discount) @Html.DisplayFor(m => m.OrderDetail.Discount)<br />
    <input type="submit" value="Update" />
    </div>
}

View Model in OrderDetailViewModel.cs

using NorthwindDAL;
namespace ADO_Fan.Models
{
    public class OrderDetailViewModel
    {
        public Order_Detail OrderDetail { get; set; }
    }
}

Order_Detail defined in reference class

namespace NorthwindDAL
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Data.Entity.Spatial;

    [Table("Order Details")]
    public partial class Order_Detail
    {
        [Key]
        [Column(Order = 0)]
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        public int OrderID { get; set; }

        [Key]
        [Column(Order = 1)]
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        public int ProductID { get; set; }

        [Column(TypeName = "money")]
        public decimal UnitPrice { get; set; }

        public short Quantity { get; set; }
        public float Discount { get; set; }
        public virtual Order Order { get; set; }
        public virtual Product Product { get; set; }
    }
}

Any advice/suggestion is appreciated!

J.Fan
  • 11
  • 1
  • 1
    Because you not generating any inputs for properties other than `OrderDetail.Quantity`. (a form only posts back the name/value pairs of successful form controls, and `DisplayFor()` does not generate a form control). –  Mar 15 '17 at 02:55
  • 1
    And the model in your view is `OrderDetailViewModel` so the parameter in the POST method needs to be the same - `public ActionResult Update(OrderDetailViewModel m)` - but a view model should never contain a property which is a data model - refer [What is ViewModel in MVC?](http://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc) –  Mar 15 '17 at 02:57
  • Thanks Stephen! You were absolutely right about both issues. – J.Fan Mar 16 '17 at 01:53

1 Answers1

0

you have to understand LabelFor and DisplayFor does only one way trip means they will only display the data and do not return data on submit so you can either user TextBoxFor for returning the data or you can use HiddenFor like

@model ADO_Fan.Models.OrderDetailViewModel
@using (Html.BeginForm("Update", "Order", FormMethod.Post))
{ 
   @Html.HiddenFor(m => m.OrderDetail.OrderID);
   @Html.HiddenFor(m => m.OrderDetail.Product.ProductName);
   @Html.HiddenFor(m => m.OrderDetail.UnitPrice);
   @Html.HiddenFor(m => m.OrderDetail.Quantity);
   @Html.HiddenFor(m => m.OrderDetail.Discount);

    <div>
    @Html.LabelFor(m => m.OrderDetail.OrderID) @Html.DisplayFor(m => m.OrderDetail.OrderID)<br />
    @Html.LabelFor(m => m.OrderDetail.Product.ProductName)  @Html.DisplayFor(m => m.OrderDetail.Product.ProductName)<br />
    @Html.LabelFor(m => m.OrderDetail.UnitPrice) @Html.DisplayFor(m => m.OrderDetail.UnitPrice)<br />
    @Html.LabelFor(m => m.OrderDetail.Quantity) @Html.TextBoxFor(m => m.OrderDetail.Quantity)<br />
    @Html.LabelFor(m => m.OrderDetail.Discount) @Html.DisplayFor(m => m.OrderDetail.Discount)<br />
    <input type="submit" value="Update" />
    </div>
}

or you can change @Html.LabelFor to @Html.TextBoxFor

Usman
  • 4,615
  • 2
  • 17
  • 33
  • Thanks Usman! You were right about the Html.LabelFor and Html.TextBoxFor. I also had a problem with the parameter type in my POST action as Stephen pointed out above. My first MVC project. Thanks for the input!!! – J.Fan Mar 16 '17 at 01:58
  • you can accept answer if it helped. if you dont know how you can check this https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – Usman Mar 16 '17 at 02:43