如何在 C# 中返回和检索对象值?

齐乌尔·卡比尔·法赫德

我正在为我的项目使用 3 层架构。但是对于一个实例,我需要从方法中返回Object并检索值。这是我的两个模型:

public class Product
{
    public int ProductId { get; set; }
    public string ProductName { get; set; }
    public decimal Price { get; set; }
}

public class Item
{
    public Product Products { get; set; }
    public int Quantity { get; set; }
}

我的方法是:

public class ProductGateway{

public List<Product> GetProductByProductId(int productId)
    {
        List<Product> productList = new List<Product>();
        SqlConnection connection = new SqlConnection(ConnectionString);
        string query = "SELECT ProductName,cast(Price as decimal(10,2)) as Price FROM   Product  WHERE ProductId='" + productId + "' ";
        SqlCommand command = new SqlCommand(query, connection);
        connection.Open();
        SqlDataReader reader = command.ExecuteReader();
        while (reader.Read())
        {              
            Product product = new Product(); 
            product.ProductName = reader["ProductName"].ToString();
            product.Price = (decimal)reader["Price"];
            productList.Add(product);
        }
        return productList;
}
    }

和我的控制器:CartController

ProductGateway gateway=new ProductGateway();
public ActionResult BuyProduct( int id)
    {
        if (Session["cart"]==null)
        {
            List<Item> cart= new List<Item>();
            cart.Add(new Item()
            {
                Products = gateway.GetProductByProductId(id),// this line getting an error(no list)
                Quantity = 1
            });
            Session["cart"] = cart;
        }

而我的 cshtml 视图:

@{
                    var cart = (List<Item>) Session["cart"];
                }
                @foreach (var item in cart)
                {
                    <tr>
                        <td class="cart_product">
                            <a href="@Url.Action("ProductDetails", "Product", new { id = item.Products.ProductId })">@item.Product.Price </a>// an error occurred on ProductId and Price   

现在的问题是我的返回类型是 List Controller get an error onProducts = gateway.AllProductsByProductId(id)并要求更改 Item model as 。因此Public List<Product> Products我想从 GetProductByProductId 发送 Object 以避免 Cart Controller 中的错误。有没有办法解决这个问题,或者我需要更改整个代码?我真的对这个场景感到困惑。

我的要求

1.我需要使用产品属性,例如@item.Product.Price在我的 cshtml 视图中。(当前错误:无法解析符号Price

2.所以我的网关应该返回一个产品对象而不是产品列表(现在网关返回产品列表)。

3.如果可以从网关返回对象(假设网关为每个id返回单个产品),那么我如何从购物车控制器中检索该对象

谢谢

编码Yoshi

在您的评论中,您表示只能存在一个具有给定产品 ID 的产品。在这种情况下,您GetProductByProductId(int productId)应该退货一种产品。对该方法进行以下更改:

public Product GetProductByProductId(int productId)
{
    // your code

    // Change this part
    if(reader.Read())
    {
        Product product = new Product();
        product.ProductName = reader["ProductName"].ToString();
        product.Price = (decimal)reader["Price"];
        return product;
    }

    // not found so we can return null or whatever you want
    return null;
}

并且您还应该尝试使用SqlParameter以避免SQL 注入,尤其是当参数来自最终用户时。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章