根据对象ID在View中返回对象详细信息ASP.net core MVC
Returning object details in the View based on the object ID ASP.net core MVC
我正在为 phone 构建在线目录,我有两个控制器,一个用于 phone 目录,第二个用于 phone 的 details.With 目录,一切都很好,现在我的目标是在catalog.I中点击phone的名字或照片后查看phone详情 觉得有了phone的id,更容易解决这个任务,我也使用存储库模式。
此目录的控制器:
public class PhonesCatalog : Controller
{
private readonly IPhoneRepository _repository;
public int PageSize = 6;
public PhonesCatalog(IPhoneRepository repository)
{
_repository = repository;
}
[HttpGet]
public IActionResult Catalog(int productPage = 1)
=> View(new ProductsListViewModel
{
Phones = _repository.Phones
.OrderBy(x => x.PhoneId)
.Skip((productPage -1) * PageSize)
.Take(PageSize),
PagingInfo = new PagingInfo
{
CurrentPage = productPage,
ItemsPrePage = PageSize,
TotalItems = _repository.Phones.Count()
}
});
}
存储库模式:
public interface IPhoneRepository
{
IQueryable<Phone> Phones { get; }
Phone GetPhoneById(int id);
}
public class EfMobileStoreRepository : IPhoneRepository
{
private readonly MobileStoreCatalogContext _context;
public EfMobileStoreRepository(MobileStoreCatalogContext context)
{
_context = context;
}
public IQueryable<Phone> Phones => _context.Phones;
public Phone GetPhoneById(int id) => _context.Phones
.FirstOrDefault(p => p.PhoneId == id);
}
这里是 Phone 详细控制器和视图:
public class PhonesDetails : Controller
{
private readonly IPhoneRepository _repository;
public PhonesDetails(IPhoneRepository repository)
{
_repository = repository;
}
[HttpGet]
public IActionResult Details(int id)
{
return View(_repository.GetPhoneById(id));
}
}
@model Mobile_Store_Catalog_wandio.Models.Phone
<h4>Phone Details</h4>
<div class="row">
<p>@Model.PhoneName</p>
<p>@Model.Manufactor</p>
<p>@Model.OperationSystem</p>
<p>@Model.Processor</p>
<p>@Model.Memory</p>
<p>@Model.ScreenResolution</p>
<p>@Model.Size</p>
<p>@Model.Wight</p>
</div>
此处目录视图:
@model ProductsListViewModel
<h1>Phone Catalog</h1>
<div class="container-fluid">
<div class="container-fluid">
<div class="row">
<div class="col-md-3 btn-group-vertical text-center">
Filter
Search
</div>
<div class=" row col-md-9">
@foreach (var p in Model.Phones)
{
<div class=" col-md-4 border border-dark">
<a href="@Url.Action("Details", "PhonesDetails")">
<img class="img-fluid" src="/Images/@p.ImageName"/>
</a>
<p class="text-center container">@p.PhoneName</p>
<p class="text-white text-center bg-success">@p.Price.ToString("C2")</p>
</div>
}
</div>
</div>
</div>
</div>
<div class="row">
<div page-model="@Model.PagingInfo" page-action="Catalog" page-classes-enabled="true"
page-class="btn" page-class-normal="btn-outline-dark"
page-class-selected="btn-primary" class="btn-group-toggle m-1 al">
</div>
</div>
这里是 ProductViewList 代码:
public class ProductsListViewModel
{
public IEnumerable<Phone> Phones { get; set; }
public PagingInfo PagingInfo { get; set; }
}
问题是,当我点击 phone 图片时,它只需要 phone 的第一个 ID 和 return 的详细信息,这无关紧要图片被点击,但我需要 return id 的详细信息,而不仅仅是第一个 id Phone 的详细信息。
我这里做错了什么,谁能帮帮我?
您可以这样更改代码:
<a asp-controller="PhonesDetails" asp-action="Details" asp-route-id="@p.Id">
<img class="img-fluid" src="/Images/@p.ImageName"/></a>
我正在为 phone 构建在线目录,我有两个控制器,一个用于 phone 目录,第二个用于 phone 的 details.With 目录,一切都很好,现在我的目标是在catalog.I中点击phone的名字或照片后查看phone详情 觉得有了phone的id,更容易解决这个任务,我也使用存储库模式。
此目录的控制器:
public class PhonesCatalog : Controller
{
private readonly IPhoneRepository _repository;
public int PageSize = 6;
public PhonesCatalog(IPhoneRepository repository)
{
_repository = repository;
}
[HttpGet]
public IActionResult Catalog(int productPage = 1)
=> View(new ProductsListViewModel
{
Phones = _repository.Phones
.OrderBy(x => x.PhoneId)
.Skip((productPage -1) * PageSize)
.Take(PageSize),
PagingInfo = new PagingInfo
{
CurrentPage = productPage,
ItemsPrePage = PageSize,
TotalItems = _repository.Phones.Count()
}
});
}
存储库模式:
public interface IPhoneRepository
{
IQueryable<Phone> Phones { get; }
Phone GetPhoneById(int id);
}
public class EfMobileStoreRepository : IPhoneRepository
{
private readonly MobileStoreCatalogContext _context;
public EfMobileStoreRepository(MobileStoreCatalogContext context)
{
_context = context;
}
public IQueryable<Phone> Phones => _context.Phones;
public Phone GetPhoneById(int id) => _context.Phones
.FirstOrDefault(p => p.PhoneId == id);
}
这里是 Phone 详细控制器和视图:
public class PhonesDetails : Controller
{
private readonly IPhoneRepository _repository;
public PhonesDetails(IPhoneRepository repository)
{
_repository = repository;
}
[HttpGet]
public IActionResult Details(int id)
{
return View(_repository.GetPhoneById(id));
}
}
@model Mobile_Store_Catalog_wandio.Models.Phone
<h4>Phone Details</h4>
<div class="row">
<p>@Model.PhoneName</p>
<p>@Model.Manufactor</p>
<p>@Model.OperationSystem</p>
<p>@Model.Processor</p>
<p>@Model.Memory</p>
<p>@Model.ScreenResolution</p>
<p>@Model.Size</p>
<p>@Model.Wight</p>
</div>
此处目录视图:
@model ProductsListViewModel
<h1>Phone Catalog</h1>
<div class="container-fluid">
<div class="container-fluid">
<div class="row">
<div class="col-md-3 btn-group-vertical text-center">
Filter
Search
</div>
<div class=" row col-md-9">
@foreach (var p in Model.Phones)
{
<div class=" col-md-4 border border-dark">
<a href="@Url.Action("Details", "PhonesDetails")">
<img class="img-fluid" src="/Images/@p.ImageName"/>
</a>
<p class="text-center container">@p.PhoneName</p>
<p class="text-white text-center bg-success">@p.Price.ToString("C2")</p>
</div>
}
</div>
</div>
</div>
</div>
<div class="row">
<div page-model="@Model.PagingInfo" page-action="Catalog" page-classes-enabled="true"
page-class="btn" page-class-normal="btn-outline-dark"
page-class-selected="btn-primary" class="btn-group-toggle m-1 al">
</div>
</div>
这里是 ProductViewList 代码:
public class ProductsListViewModel
{
public IEnumerable<Phone> Phones { get; set; }
public PagingInfo PagingInfo { get; set; }
}
问题是,当我点击 phone 图片时,它只需要 phone 的第一个 ID 和 return 的详细信息,这无关紧要图片被点击,但我需要 return id 的详细信息,而不仅仅是第一个 id Phone 的详细信息。
我这里做错了什么,谁能帮帮我?
您可以这样更改代码:
<a asp-controller="PhonesDetails" asp-action="Details" asp-route-id="@p.Id">
<img class="img-fluid" src="/Images/@p.ImageName"/></a>