【问题标题】:How can i create Master detail page in asp.net Core如何在 asp.net Core 中创建主详细信息页面
【发布时间】:2020-02-11 03:42:12
【问题描述】:

如何在 asp.net core MVC 的视图中显示数据?

我有以下json格式数据

[
    {
      "ProductID": "100000",
      "ProductName": "Product 1"
    },
    {
      "ProductID": "100001",
      "ProductName": "Product 2"
    },
    {
      "ProductID": "100002",
      "ProductName": "Product 3"
    },
    {
      "ProductID": "100002",
      "ProductName": "Product 4"
    }
]

我有这个代码来读取上面的json数据,从上面的json数据中读取产品信息

public class GetProducts_Action : BaseEFAction<GetProducts_Action_Request, GetProducts_Action_Response>
    {
        public IFileProvider FileProvider { get; }

        public GetProducts_Action(ILogger<GetProducts_Action> logger, DBContext context, ITransactionManager scope, IFileProvider fileProvider) : base(logger, context, scope)
        {
            FileProvider = fileProvider;
        }

        protected override Task<GetProducts_Action_Response> PerformActionAsync(GetProducts_Action_Request request)
        {
            IList<ProductDTO> product;

            using (var file = System.IO.File.OpenText(FileProvider.GetFileInfo("Product.json").PhysicalPath))
            {
                var serializer = new JsonSerializer();
                product = (IList<ProductDTO>)serializer.Deserialize(file, typeof(IList<ProductDTO>));
            }

            return Task.FromResult(new GetProducts_Action_Response { Products = product });
        }
    }

    public class GetProducts_Action_Request : BaseActionRequest
    {

    }

    public class GetProducts_Action_Response : BaseActionResponse
    {
        public IList<ProductDTO> Products { get; set; }
    }
}

界面:

public interface IProductService { Task<IList<ProductDTO>> GetProduct(); }

控制器动作显示所有效果很好的产品

public IActionResult Index()
{
    return View();
}

[HttpGet]
public async Task<IActionResult> GetProducts()
{
    var products = await ProductsService.GetProducts();
    return Json(products);
}

我能够使用上面的代码显示所有产品并且工作完美,我还创建了一个指向详细页面的链接,URL 看起来像这样localhost:3000/Product/Products/102。但是如何通过 id 获取产品信息并显示在详细页面中?.

我试过了

   [HttpGet("{id}")]
public async Task<IActionResult> GetProductsDetail(int id)
{
    var product = (await ProductsService.GetProducts()).FirstOrDefault(a => a.ProductCode == id);
    if (product != null) {
        return Json(product);
    } else {
        return NotFound();
    }
}

但我明白了

无法将类型 'System.Collections.Generic.List 隐式转换为 'System.Collections.IList'

产品列表结构

 public class ProductList
    {
       public string ProductCode { get; set; }
       public string ProductName { get; set; }
    }

我在详细视图中有网格

 @model ProductList

                                <div class="row">
                                @(Html.Kendo().Grid<ProductList>()
                               .Name("grid")
                                .Columns(cols =>
                                {
                                      cols.Bound(ProductList => ProductList.ProductCode).Hidden(false).Title(Localizer["Column 1"].Value);
                                      cols.Bound(ProductList => ProductList.ProductName).Hidden(false).Title(Localizer["Column 2"].Value);
                                 })
                                //.Editable(e => e.Mode(GridEditMode.PopUp).TemplateName(""))
                                .Excel(excel => excel
                                .FileName("Kendo UI Grid Export.xlsx")
                                .Filterable(true)
                                .ProxyURL(Url.Action("Excel_Export_Save", "Grid"))
                                )
                                .Navigatable(true)
                                .Filterable(f => f.Enabled(true).Mode(GridFilterMode.Row))
                                .Scrollable(s => s.Enabled(true))
                                .Sortable()
                                .PersistSelection()
                                .Pageable(pageable => pageable
                                .ButtonCount(5)
                                .Refresh(true)
                                .PageSizes(new[] { 5, 10, 20 }))
                                .DataSource(dataSource => dataSource
                                             .Custom()
                                             .PageSize(10)
                                             .Transport(transport => transport
                                             .Read(read => read.Action("GetProductsDetail", "Product"))

                                             ))
                                )

                            </div>

【问题讨论】:

  • 你的视图代码是什么样子的?
  • @TonyNgo 我只想显示产品名称,不管怎样。目前我使用剑道网格读取` .DataSource(dataSource => dataSource .Custom() .PageSize(10) .Transport(transport => transport .Read(read => read.Action("GetProductsDetail", "Product") )` 但这不是我想要的,我只想显示所选记录的产品名称
  • 如果我不知道您的视图如何,我无法帮助您
  • Controller或者View抛出的异常在哪里?
  • @TonyNgo 我在底部添加了我试图在 Kendo Grid 中显示信息但我不必是剑道我正在使用 kendoGrid 进行测试

标签: c# asp.net-mvc asp.net-core


【解决方案1】:

首先,你可以从这个得到结果

var listOfproducts = (await ProductsService.GetProducts().ToList());

之后,你可以获得价值

var product = listOfproducts.FirstOrDefault(a => a.ProductCode == id);

其次, 如我所见,GetProductsDetail 正在返回 Json,因此您正在通过 Ajax。如果你想重定向到一个页面:

  1. 在索引视图中:
@Html.ActionLink("You_Controller_Name", "GetProductsDetail", new { id = item.ID }) |
  1. 您应该返回 return View(product); 而不是 Json

阅读以下文章以更好地理解

Get Started with Entity Framework 6 Code First using MVC 5

Passing Data from the Controller to the View

【讨论】:

  • 这里完成了id = "#= ProductCode #" }。在控制器中你应该返回View 而不是Json
  • 为了保持简单的问题,您可以创建一个新问题然后给我发帖。我会为你解答。
  • 好的,我在这里发帖stackoverflow.com/questions/60181286/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-27
  • 2021-10-27
  • 1970-01-01
  • 1970-01-01
  • 2021-10-19
  • 2019-08-23
相关资源
最近更新 更多