我正在开发购物车应用程序。它已经由一些开发人员开发。但我试图重建它,但我得到以下异常
App_Web_jwfiir5n.dll中发生了'System.NullReferenceException'类型的异常,但未在用户代码中处理 附加信息:对象引用未设置为对象的实例。
我视图中的代码如下:
<tbody>
@foreach (var item in Model)
{
<tr>
<td>@(Model.IndexOf(item) + 1)</td>
<td><a style="color:cornflowerblue" title="Click to see the product detail" href="/admin/ProductDetail?productId=@item.ProductId"> @item.Tbl_Product.ProductName</a></td>
<td>@(item.Tbl_Members.FirstName + " " + item.Tbl_Members.LastName)</td>
<td>@item.Tbl_Members.EmailId</td>
</tr>
}
</tbody>
Controler方面如下:
public ActionResult OrderDetail(int productId)
{
List<Tbl_Cart> ProductOrders = _unitOfWork.GetRepositoryInstance<Tbl_Cart>().GetListByParameter(i => i.CartStatusId == 3 && i.ProductId == productId).ToList();
return View(ProductOrders);
}
任何人都可以帮我解决这个错误。
答案 0 :(得分:0)
检查空值
<tbody>
if(Model != null)
{
@foreach (var item in Model)
{
<tr>
<td>@(Model.IndexOf(item) + 1)</td>
<td><a style="color:cornflowerblue" title="Click to see the product detail" href="/admin/ProductDetail?productId=@item.ProductId"> @item.Tbl_Product.ProductName</a></td>
<td>@(item.Tbl_Members.FirstName + " " + item.Tbl_Members.LastName)</td>
<td>@item.Tbl_Members.EmailId</td>
</tr>
}
}
</tbody>
答案 1 :(得分:-1)
protected void Page_Load(object sender, EventArgs e)
{
//String mycon = "Data Source=HP-PC\\SQLEXPRESS; Initial Catalog=CollegeData; Integrated Security=True";
String myquery = "Select * from Employee where Emp_Id=" + Request.QueryString["Emp_Id"];
SqlConnection conn = new SqlConnection(con);
SqlCommand cmd = new SqlCommand();
cmd.CommandText = myquery;
cmd.Connection = conn;
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
//DataSet ds = new DataSet();
//da.Fill(ds);
//if (ds.Tables[0].Rows.Count > 0)
{
Label1.Text = Request.QueryString["Emp_Id"].ToString();
Label2.Text = Request.QueryString["Emp_Name"].ToString();
Label3.Text = Request.QueryString["Emp_Address"].ToString();
Label4.Text = Request.QueryString["Emp_Designation"].ToString();
Label5.Text = Request.QueryString["Emp_BasicPay"].ToString();
Label6.Text = Request.QueryString["Emp_Email"].ToString();
Label7.Text = Request.QueryString["Emp_Mobile"].ToString();
}
conn.Close();