MVC 4 View不会将Model传递给Controller

时间:2013-12-24 13:05:04

标签: asp.net-mvc asp.net-mvc-4

这是视图:

@model CSP1225.Models.BestOrder
@{
    ViewBag.Title = "BestOfferOrder";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@{
    var payment = ViewBag.payment;
    var convert= ViewBag.Convert;
    var imgsrc = ViewBag.source;
    imgsrc = imgsrc+ Model.Best.Image;
    var priceLE = convert * Model.Best.Price;
}

@using(@Html.BeginForm("BestOfferNewOrder","Order",Model,FormMethod.Post,null))
{
    <label>Item Name:</label>
    @Html.ValueFor(r=>r.Best.Name)

    <br />
    <label>Price:</label>
    @Html.ValueFor(r=>r.Best.Price)<p>$</p>

    <br />
    <label>Price in LE:</label> 
    <p>@priceLE LE</p>  
    @Html.DropDownListFor(r => r.Payment, new SelectList(payment, "PaymentMethodId", "Name"));
    <button type="submit">Save Order</button>
}

这是控制器:

[HttpPost]
public ActionResult BestOfferNewOrder(BestOrder model)
{
    var order = new Order();
    order.BestOfferID = model.Best.ID;
    order.DateTime = DateTime.Now;
    order.Fees = 0;
    order.TotalPrice = model.Best.Price;
    MembershipUser user = Membership.GetUser();
    var UserId = _db.UserProfiles.Where(u => u.Email == user.Email).FirstOrDefault().ID;
    order.UserID = UserId;
    order.PaymentMethod = model.Payment;
    _db.Orders.Add(order);
    _db.SaveChanges();
    return View("ViewOrders");
}

这是型号:

public class BestOrder
{
    public BestOfferModel Best { get; set; }
    public byte Payment { get; set; }
}
public class BestOfferModel
{
    public int ID { get; set; }
    public string ItemName { get; set; }
    public decimal price { get; set; }
    public string image { get; set; }
}

我不知道出了什么问题,View只将模型中的付款传递给控制器​​,而不是整个模型。有人可以告诉我我的代码有什么问题吗?

1 个答案:

答案 0 :(得分:0)

改变这个:

@using(@Html.BeginForm("BestOfferNewOrder","Order",Model,FormMethod.Post,null))
{

到此:

@using(Html.BeginForm("BestOfferNewOrder","Order",FormMethod.Post))
{