将模型从视图传递到controller-action时,返回的所有属性都是0。请参阅附图。 Returned model
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";
}
查看文件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>
}
在OrderDetailViewModel.cs中查看模型
using NorthwindDAL;
namespace ADO_Fan.Models
{
public class OrderDetailViewModel
{
public Order_Detail OrderDetail { get; set; }
}
}
在参考类
中定义的Order_Detailnamespace 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; }
}
}
感谢任何建议/建议!
答案 0 :(得分:0)
你必须理解LabelFor
和DisplayFor
只做一次旅行意味着他们只会显示数据而不会在提交时返回数据,这样你就可以
TextBoxFor
用于返回数据,或者您可以使用HiddenFor
之类的
@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>
}
或者您可以将@Html.LabelFor
更改为@Html.TextBoxFor