0

大家好,我有一些图像,我正在动态检索并在这样的视图中显示

这是我的控制器

    private ProductEntities products = new ProductEntities();
    public List<ProductItems> GetAllProducts()
    {
        var items = new List<ProductItems>();
        var records = products.Products.ToList();
        foreach (var item in records)
        {
            ProductItems model = new ProductItems();
            model.ProductID = item.ProductId;
            model.ProductName = item.ProductName;
            model.ImageURL = item.ImageURL;
            items.Add(model);
        }
        return items;
    }

这是我的索引页面视图

   <script type="text/javascript">
    $(document).ready(function () {
        $.getJSON("/api/ProductDetails", function (data) {
            $.each(data, function (idx, ele) {
                $("<img/>").attr({ src: ele.ImageURL }).appendTo("#makeMeScrollable");
                $("#makeMeScrollable").append('<h4><a href="#">' + ele.ProductName + '</a></h4>');

            });
        });
    });      

</script>

</head>
<body>
<h1>Products</h1>
<div class="rightsection_main">
    <div class="img_main" id="makeMeScrollable">            

       </div>
   </div>
  </body>

现在我想要的是,当用户单击图像时,我必须将图像的 ID 传递给我的 apicontroller 中的方法,并且我必须在另一个视图中显示该图像..我如何将我的图像传递给另一个视图和我的 api/控制器操作的 ID

我必须将我的 ProductID 传递给这个方法

  public IEnumerable<ProductItems> ProductDeatils(long ProductID)
    {
        var productdeatils = products.ExecuteStoreQuery<ProductItems>("GetProductDetail @ProductID ", new SqlParameter("@ProductID", ProductID));           
        return productdeatils ;
    }
4

1 回答 1

0

问题是 API 操作不会返回视图。它们用于使用某种格式(例如 JSON 或 XML)序列化模型。因此,当您说要使用 ProductDetils API 操作来显示视图时,这没有多大意义。视图由返回 ActionResults 的标准 ASP.NET MVC 控制器操作返回。那么让我们看看如何设置它。

假设您有以下 API 控制器:

public class ProductsController : ApiController
{
    private ProductEntities products = new ProductEntities();
    public IEnumerable<ProductItems> Get()
    {
        return products.Products.ToList().Select(item => new ProductItems
        {
            ProductID = item.ProductId,
            ProductName = item.ProductName,
            ImageURL = item.ImageURL
        });
    }
}

public class ProductDetailsController : ApiController
{
    private ProductEntities products = new ProductEntities();
    public ProductItems Get(long id)
    {
        return products.ExecuteStoreQuery<ProductItems>(
            "GetProductDetail @ProductID ", 
            new SqlParameter("@ProductID", id)
        );
    }
}

好的,现在我们需要标准的 ASP.NET MVC 控制器来为视图提供服务:

public class HomeController : Controller
{
    public ActionResult Products()
    {
        return View();
    }

    public ActionResult ProductDetail(long id)
    {
        using (var client = new HttpClient())
        {
            var productDetailUrl = Url.RouteUrl(
                "DefaultApi", 
                new { httproute = "", controller = "productdetails", id = id },
                Request.Url.Scheme
            );
            var model = client
                .GetAsync(productDetailUrl)
                .Result
                .Content
                .ReadAsAsync<ProductItems>()
                .Result;
            return View(model);
        }
    }
}

以及单击链接时显示产品列表和产品详细信息的相应视图:

~/Views/Home/Products.cshtml

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <h1>Products</h1>
    <div class="rightsection_main">
        <div class="img_main" id="makeMeScrollable">            

        </div>
    </div>

    <script type="text/javascript" src="~/scripts/jquery-1.8.2.js"></script>
    <script type="text/javascript">
        var productsUrl = '@Url.RouteUrl("DefaultApi", new { httproute = "", controller = "products" })';
        var productDetailUrl = '@Url.Action("productdetail", "home", new { id = "__id__" })';

        $.getJSON(productsUrl, function (data) {
            $.each(data, function (idx, product) {
                $('<img/>').attr({ src: product.ImageURL }).appendTo('#makeMeScrollable');
                $('#makeMeScrollable').append(
                    $('<h4/>').html(
                        $('<a/>', {
                            href: productDetailUrl.replace('__id__', product.ProductID),
                            text: product.ProductName
                        })
                    )
                );
            });
        });
    </script>
</body>
</html>

~/Views/Home/ProductDetail.cshtml

@model ProductItems
@{
    Layout = null;
}

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>ProductDetail</title>
</head>
<body>
    <h4>@Html.DisplayFor(x => x.ProductName)</h4>
    <img src="@Model.ImageURL">
</body>
</html>
于 2012-10-04T15:06:06.613 回答