2012-04-02 71 views
0

我试图在索引文件中显示价格。 但是,它显示空白字段。我的编码有什么问题? 你能帮我吗?这应该是容易的问题。但我找不到它。 如果您提供一些反馈,我真的很感谢你!MVC3 ViewBag不起作用

public ActionResult Index(decimal priceValue) 
    { 
     var cart = ShoppingCart.GetCart(this.HttpContext); 

     // Set up our ViewModel 
     decimal price = priceValue; 
     ViewBag.eachPrice = price; 
     var viewModel = new ShoppingCartViewModel 
     { 
      CartItems = cart.GetCartItems(), 
      CartTotal = cart.GetTotal(price) 
     }; 

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

Index.cshtml

@foreach (var item in Model.CartItems) 
{ 
    <tr id="[email protected]"> 
     <td> 
      @Html.ActionLink(item.Product.model, "Details", "Store", new { id = item.productId }, null) 
     </td> 
     <td> 
      //here!! is it wrong? 
      @ViewBag.eachPrice 
     </td> 
     <td id="[email protected]"> 
      @item.count 
     </td> 
     <td> 
      @item.dateCreated.Date.ToString("dd MMM yyyy") 
     </td> 
     <td> 
      @*how about using some model instead of number?*@ 
      @item.dateCreated.Date.AddDays(2).ToString("dd MMM yyyy") 
     </td> 
     <td> 
     </td> 
     <td> 
      <a href="#" class="RemoveLink" data-id="@item.recordId"> 
      Remove from cart</a> 
     </td> 
    </tr> 
} 
+1

它看起来的代码,它应该工作,你有没有尝试调试视图,看看值是否存在? – Yasser 2012-04-02 07:41:43

回答

-1

解决了错误我不知道为什么之前它不工作.. 无论如何,谢谢你们的回复!

0

在VB中,我这样做这样的:

@code 
    Dim price = viewbag.price 
@End code 

,然后,我显示的价值,我想

@Html.Raw(price) 

在CS中,我认为你必须把“var”代替“Dim”和finis h带有“;”的指示

+1

在C#中(不确定VB,但我想它们的工作方式是相同的),如果使用@ ViewBag.price或者先将它存储在变量中,通常无关紧要。 – 2012-04-02 07:41:39

+0

它从来没有为我描述过你的方式。但我也许做了错误的事情(我几乎没有.NET的使用经验) – 2012-04-02 07:49:06

+0

asp.net mvc将标题存储在viewbag中,通常我希望页面顶部有相同的标题,所以我使用'

@ViewBag。标题

'。这里的问题不应该有所不同。 – 2012-04-02 08:11:01