English 中文(简体)
不要更新POST的观点
原标题:ViewModel not updating on POST

我有主计长和观点,使用户能够检查购物车的内容。

当我的主计长将命令模式退回到检查结果时,所有人员都做了罚款。 然而,我现在要把购物单的内容显示在检查前,因此修改了主计长和观点,以使用含有订单和智能系统的观点。

我建立了“Model”号报,并修改了“GET:and POST: ActionResults to use this viewmodel”。

问题是,在《刑法》期间,《婚姻法》中没有适当的人种:

我正在拿不反对的字句,不要在此行文中打字。 Order.Username =user.Identity. 姓名;在《行动计划》中:行动结果,但我肯定不是确切的,什么是没有被遗忘的。

如果需要的话,我可以张贴使用Oreder模式的原工作编码,而不是“ReoutViewModel”。

我确信,我有一些简单的动乱或yn子问题,我可以告诉我们,因为我仍然试图以最佳方式在多国师委员会做事。

GET: ActionResult

    //GET: /Checkout/AddressAndPayment
    public ActionResult AddressAndPayment()
    {
        //To pre-populate the form, create a new Order object and get the ShoppingCart, populate the ViewModel and pass it to the view
        var order = new Order();
        order.Username = User.Identity.Name;
        MembershipUser currentUser = Membership.GetUser(User.Identity.Name, true /* userIsOnline */);
        storeDB.SaveChanges();

        var cart = ShoppingCart.GetCart(this.HttpContext);

        // Set up our ViewModel
        var viewModel = new CheckoutViewModel
        {
            CartItems = cart.GetCartItems(),
            CartTotal = cart.GetTotal(),
            Order = order
        };

        // Return the view
        return View(viewModel);
    }  

www.un.org/spanish/ecosoc POST: ActionResult

        //POST: /Checkout/AddressAndPayment
    [HttpPost]
    public ActionResult AddressAndPayment(FormCollection values)
    {
        var checkoutViewModel = new CheckoutViewModel();
        TryValidateModel(checkoutViewModel);

        try
        {
            checkoutViewModel.Order.Username = User.Identity.Name;
            checkoutViewModel.Order.OrderDate = DateTime.Now;
            //Save Order
            storeDB.Orders.Add(checkoutViewModel.Order);
            storeDB.SaveChanges();
            //Process the order
            var cart = ShoppingCart.GetCart(this.HttpContext);
            cart.CreateOrder(checkoutViewModel.Order);
            storeDB.SaveChanges();
            //Send  Order Confirmation  email
            ViewData["order"] = checkoutViewModel.Order;
            UserMailer.OrderConfirmation(checkoutViewModel.Order).SendAsync();

            return RedirectToAction("Complete", new { id = checkoutViewModel.Order.OrderID });
        }
        catch
        {
            //Invalid - redisplay with errors
            return View(checkoutViewModel);
        }
   }  

@model OrderUp.ViewModels.CheckoutViewModel
@{
    ViewBag.Title = "AddressAndPayment";
}
<script src="../../Scripts/jquery-1.5.1.min.js" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm()) { 
@Html.ValidationSummary(true)
<h2>Pick Up Details</h2>
<fieldset>
    <legend>When are you gonna be hungry?</legend>

    <div class="editor-label">
        @Html.LabelFor(model => model.Order.Phone)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Order.Phone)
        @Html.ValidationMessageFor(model => model.Order.Phone)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Order.PickUpDateTime)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Order.PickUpDateTime)
        @Html.ValidationMessageFor(model => model.Order.PickUpDateTime)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Order.Notes)
    </div>
    <div class="editor-multiline-field">
        @Html.EditorFor(model => model.Order.Notes)
        @Html.ValidationMessageFor(model => model.Order.Notes)
    </div>
</fieldset>
<input type="submit" value="Submit Order" />
}
<div style="height:30px;"></div>
<h3>
    <em>Review</em> your cart:
 </h3>
<table>
<tr>
    <th>
        Menu Item
    </th>

    <th>
        Price (each)
    </th>
    <th>
        Notes
    </th>
    <th>
        Quantity
    </th>
    <th></th>
</tr>
@foreach (var item in Model.CartItems)
{
    <tr id="[email protected]">
        <td>
            @Html.ActionLink(item.MenuItem.Name, "Details", "Store", new { id = item.MenuItemID }, null)
        </td>

        <td>
            @Html.DisplayFor(modelItem => item.MenuItem.Price)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Notes )
        </td>
        <td id="[email protected]">
            @item.Count
        </td>
        <td>
        </td>
    </tr>
}
<tr>
    <td >
        Total
    </td>
    <td  id="cart-total">
        @String.Format("${0:F2}", Model.CartTotal)
    </td>
    <td>
    </td>
    <td>
    </td>
    <td>
    </td>
</tr>
</table>
最佳回答

您应使用“Model”号作为对你的“HttpPost”方法的一种投入参数,然后尝试进行欺骗,看看看你是否仍然没有在表格上填满该模型。

[HttpPost]
    public ActionResult AddressAndPayment(CheckOutViewModel checkoutViewModel)
    {
        TryValidateModel(checkoutViewModel);

        try
        {
            checkoutViewModel.Order.Username = User.Identity.Name;
            checkoutViewModel.Order.OrderDate = DateTime.Now;
            //Save Order
            storeDB.Orders.Add(checkoutViewModel.Order);
            storeDB.SaveChanges();
            //Process the order
            var cart = ShoppingCart.GetCart(this.HttpContext);
            cart.CreateOrder(checkoutViewModel.Order);
            storeDB.SaveChanges();
            //Send  Order Confirmation  email
            ViewData["order"] = checkoutViewModel.Order;
            UserMailer.OrderConfirmation(checkoutViewModel.Order).SendAsync();

            return RedirectToAction("Complete", new { id = checkoutViewModel.Order.OrderID });
        }
        catch
        {
            //Invalid - redisplay with errors
            return View(checkoutViewModel);
        }
   }  
问题回答

暂无回答




相关问题
Entity Framework with MySQL connector in c#

I have been trying to get the Entity Framework to work in my web application using MySQL. It works fine on my local pc, but doesn t work when I put it on the server. Since the server is a shared ...

How Do I Create And Update A Many To Many Relationship With EF

I am using the Entity Framework with SQL Server. I have a many to many relationship between 2 tables. I have created a join table with just the primary key fields of the 2 tables. In the designer, the ...

Entity Framework with File-Based Database

I am in the process of developing a desktop application that needs a database. The application is currently targeted to SQL Express 2005 and works wonderfully. However, I m not crazy about having ...

Linq to enties, insert foreign keys

I am using the ADO entity framework for the first time and am not sure of the best way of inserting db recored that contain foreign keys. this is the code that i am using, I would appreciate any ...

Entity Framework - Many to many question

I have a table called ASB and a table called PeopleInvolved. There is a junction table called PeopleInvolved_ASB which simply contains an ASBID and a PeopleInvolvedID column. The columns act as a ...

Post back complex object from client side

I m using ASP.NET MVC and Entity Framework. I m going to pass a complex entity to the client side and allow the user to modify it, and post it back to the controller. But I don t know how to do that ...

ADO.NET Entity Data Model are not precise enough

I run this code: var cos = from k in _db.klienci_do_trasy where k.klient_id == 5 select k; but the query send to database is: SELECT * FROM `klienci_do_trasy` LIMIT 0, 30 why is it for, there ...

热门标签