简体   繁体   中英

How can I get record by Id

How can I get record by Id? I have the following code to create master detail pages in asp.net core controller, I am able to get all products using the following code and works perfect

Interface

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

Controller Actions

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

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

But how can I get single record by Id to create a detail page.I tried this but doesn't work

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

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

GetProductCode

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; }
    }
}

Given that your data source is actually a file and not a database, you're going to be deserializing that file each time anyway. That is your performance bottleneck. So if you want you can just use your existing GetProducts() service method and then filter in the controller (using LINQ). It's not a super clean way (code-wise), but the performance will basically be the same.

[HttpGet]
public async Task<IActionResult> GetProductsDetail(int id)
{
    // Get all the products and then filter by id
    // change "a.Id" to the actual DTO Id property name if different
    var product = (await ProductsService.GetProducts()).FirstOrDefault(a => a.Id == id);
    if (product != null) {
       // If we found something, return that single ProductDTO
       return Json(product);
    } else {
       // Not Found or whatever you want
       return NotFound();
    }
}

FirstOrDefault() will return the first object with your desired ID (assuming the ProductDTO property is called Id). If it doesn't find anything, it will return null, so then you probably want to return a 404 Not Found or something similar.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM