Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 会话传递为null的MVC 4购物车_C#_Asp.net Mvc 4_Session_Shopping Cart - Fatal编程技术网

C# 会话传递为null的MVC 4购物车

C# 会话传递为null的MVC 4购物车,c#,asp.net-mvc-4,session,shopping-cart,C#,Asp.net Mvc 4,Session,Shopping Cart,我正在尝试创建购物车。 我遇到一个问题,当我调用会话时,它被传递为null。 任何帮助都将不胜感激。 多谢各位 Courses.cshtml @model IEnumerable<ANON_Capstone.Models.Course> @if (!User.Identity.IsAuthenticated) { @section featured { <section class="featured"> <div class="conte

我正在尝试创建购物车。
我遇到一个问题,当我调用会话时,它被传递为null。
任何帮助都将不胜感激。 多谢各位

Courses.cshtml

@model IEnumerable<ANON_Capstone.Models.Course>

@if (!User.Identity.IsAuthenticated)
{
@section featured
{
    <section class="featured">
        <div class="content-wrapper">
            <h2 class="text-center">Please Login or Create an Account to make a Purchase!</h2>
        </div>
    </section>
}
}

<div class="row">
<div class="col-lg-9">
    <h2><strong>Courses</strong></h2><br />
    @foreach (var item in Model)
    {
        <div class="col-md-4 col-xs-6 col-s-8 col-lg-4">
            @using (Html.BeginForm("ShoppingCart", "Home", FormMethod.Post))
            {
                <img src="~/Images/party.gif" style="width: 175px" class="img-responsive" />
                <h2>@item.className</h2>
                <p>$@item.classPrice -- @item.maxStudent spots left! </p>
                <input type="text" name="className" value="@item.className" hidden="hidden" />
                <input type="text" name="classPrice" value="@item.classPrice" hidden="hidden"  />
                <input type="text" name="classID" value="@item.ClassID" hidden="hidden" />
                <input type="hidden" name="classDesc" value="@item.classDesc" />
                if (User.Identity.IsAuthenticated)
                {
                <input class="btn btn-default" type="submit" name="btnConfirm" value="Add to Shopping Cart" />
                }
            }
            <br />
        </div>
    }
</div>
</div>
CartModel.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;

namespace ANON_Capstone.Models
{
public class CartModel
{
    public Course course { get; set; }
}
}
ShoppingCart.cshtml

@{
ViewBag.Title = "Shopping Cart";
}

<h2>Shopping Cart</h2>

@using (Html.BeginForm("ValidateCommand", "PayPal"))
{
var cart = Session["Cart"] as IEnumerable<ANON_Capstone.Models.CartModel>;
<div class="row">
    <div class="col-lg-9">
        <h2><strong>Shopping Cart</strong></h2><br />
        @if (cart != null)
        {
            <table border="1">
            <tr>
                <th>Course Image</th>
                <th>Course Name</th>
                <th>Course Desc</th>
                <th>Course Price</th>
            </tr>
            @foreach (var item in cart)
            {
                <tr>
                    <td><img src="~/Images/party.gif" style="width: 175px" class="img-responsive" /></td>
                    <td>@item.course.className</td>
                    <td>@item.course.classPrice</td>
                    <td>@item.course.classDesc</td>
                </tr>
            }
            </table>
            <input class="btn btn-default" type="submit" name="btnConfirm" value="Check Out with Paypal" />
        }

        else
        {
            <text>Your shopping cart is currently empty</text>
        }
    </div>


</div>
}
@{
ViewBag.Title=“购物车”;
}
购物车
@使用(Html.BeginForm(“ValidateCommand”、“PayPal”))
{
var cart=会话[“cart”]作为IEnumerable;
购物车
@如果(购物车!=null) { 球场形象 课程名称 课程描述 课程价格 @foreach(购物车中的var项目) { @item.course.className @项目、课程、课程价格 @item.course.classDesc } } 其他的 { 您的购物车当前为空 } }
您的Cart对象的CartModel类型不是IEnumarable。您好,谢谢您的评论。我是否必须将其更改为var cart=new CartModel(),作为IEnumerable?如果我没有遗漏某些内容,请创建cart对象并将其添加到ShoppingCart操作中的会话中。对象的类型为
CartModel
。但是,在您的视图中,您将其强制转换为
IEnumarable
。我认为应该是,
var cart=Session[“cart”]作为CartModel
;我按照您的建议进行了尝试,现在它给了我foreach循环的一个错误。它说CartModel不包含GetEnumerator的公共定义
@{
ViewBag.Title = "Shopping Cart";
}

<h2>Shopping Cart</h2>

@using (Html.BeginForm("ValidateCommand", "PayPal"))
{
var cart = Session["Cart"] as IEnumerable<ANON_Capstone.Models.CartModel>;
<div class="row">
    <div class="col-lg-9">
        <h2><strong>Shopping Cart</strong></h2><br />
        @if (cart != null)
        {
            <table border="1">
            <tr>
                <th>Course Image</th>
                <th>Course Name</th>
                <th>Course Desc</th>
                <th>Course Price</th>
            </tr>
            @foreach (var item in cart)
            {
                <tr>
                    <td><img src="~/Images/party.gif" style="width: 175px" class="img-responsive" /></td>
                    <td>@item.course.className</td>
                    <td>@item.course.classPrice</td>
                    <td>@item.course.classDesc</td>
                </tr>
            }
            </table>
            <input class="btn btn-default" type="submit" name="btnConfirm" value="Check Out with Paypal" />
        }

        else
        {
            <text>Your shopping cart is currently empty</text>
        }
    </div>


</div>
}