如何在 属性 核心中的 属性 详细信息视图中显示 属性 状态标题
How to display Property Status title in Property Details view in ASP.NET Core
我有 属性状态 table 和 属性 Table。在属性Table中,属性状态是外键table。我想使用 属性DetailsVM 在 属性 控制器详细信息页面中显示 属性 状态标题。我正在使用 Asp.Net Core 3.1。我已经完成了所有的事情,但卡在这里。
//PropertyStatus.cs
public class PropertyStatus
{
public PropertyStatus()
{
Properties = new Collection<Property>();
}
public int Id{get;set;}
public string Title { get; set; }
public string Slug { get; set; }
public ICollection<Property> Properties { get; set; }
}
//Property.cs
public class Property
{
public Property()
{
PropertyGalleries = new Collection<PropertyGallery>();
}
public int Id{get;set;}
public string Title { get; set; }
public string Slug { get; set; }
public PropertyStatus PropertyStatus { get; set; }
public int PropertyStatusId { get; set; }
}
//PropertyController.cs
[Route("property/{id}/{*slug}")]
public ActionResult Details(int id, string slug)
{
var property = _propertyRepository.GetById(id);
if (property == null)
return NotFound();
var viewModel = new PropertyDetailsVM
{
Property = property,
Id = property.Id,
Title = property.Title,
Slug = property.Slug,
PropertyStatus=property.PropertyStatus
}
}
//PropertyDetailsVM
public class PropertyDetailsVM
{
public int Id { get; set; }
public string Title { get; set; }
public string Slug {get; set; }
public Property Property { get; set; }
public PropertyStatus PropertyStatus { get; set; }
}
根据您的代码,属性Status 和 属性 class 配置为一对多关系。如果是这样,在查询属性时,可以使用Include方法通过导航属性.
加载它的属性状态
请参考以下示例:
//PropertyStatus.cs
public class PropertyStatus
{
public PropertyStatus()
{
Properties = new List<Property>();
}
public int Id { get; set; }
public string Title { get; set; }
public string Slug { get; set; }
public ICollection<Property> Properties { get; set; }
}
//Property.cs
public class Property
{
public int Id { get; set; }
public string Title { get; set; }
public string Slug { get; set; }
public PropertyStatus PropertyStatus { get; set; }
public int PropertyStatusId { get; set; }
}
控制器:
private readonly ILogger<HomeController> _logger;
//private readonly IDbContextProvider _dbProvider;
private readonly IConfiguration Configuration;
private readonly ApplicationDbContext _context;
public HomeController(ILogger<HomeController> logger, ApplicationDbContext context, IConfiguration configuration)
{
_logger = logger;
//_dbProvider = provider;
Configuration = configuration;
_context = context;
}
public IActionResult Index()
{
//required using Microsoft.EntityFrameworkCore;
//you can check your _propertyRepository.GetById method, and sue the include method
var property = _context.Properties.Include(c => c.PropertyStatus).Where(c => c.Id == 1).FirstOrDefault();
if (property == null)
return NotFound();
var viewModel = new PropertyDetailsVM
{
Property = property,
Id = property.Id,
Title = property.Title,
Slug = property.Slug,
PropertyStatus = property.PropertyStatus
};
return View(viewModel);
}
然后在查看页面:使用如下代码显示属性状态信息:
@model Core3_1MVC.Models.PropertyDetailsVM
@{
ViewData["Title"] = "Index";
}
<div>
<dl class="row">
<dt class = "col-sm-2">
@Html.DisplayNameFor(model => model.Id)
</dt>
<dd class = "col-sm-10">
@Html.DisplayFor(model => model.Id)
</dd>
<dt class = "col-sm-2">
@Html.DisplayNameFor(model => model.Title)
</dt>
<dd class = "col-sm-10">
@Html.DisplayFor(model => model.Title)
</dd>
<dt class = "col-sm-2">
@Html.DisplayNameFor(model => model.Slug)
</dt>
<dd class = "col-sm-10">
@Html.DisplayFor(model => model.Slug)
</dd>
<dt class = "col-sm-2">
PropertyStatus Title:
</dt>
<dd class = "col-sm-10">
@Html.DisplayFor(model => model.PropertyStatus.Title)
</dd>
</dl>
</div>
截图如下:
更详细的信息,您可以参考以下文章:
我有 属性状态 table 和 属性 Table。在属性Table中,属性状态是外键table。我想使用 属性DetailsVM 在 属性 控制器详细信息页面中显示 属性 状态标题。我正在使用 Asp.Net Core 3.1。我已经完成了所有的事情,但卡在这里。
//PropertyStatus.cs
public class PropertyStatus
{
public PropertyStatus()
{
Properties = new Collection<Property>();
}
public int Id{get;set;}
public string Title { get; set; }
public string Slug { get; set; }
public ICollection<Property> Properties { get; set; }
}
//Property.cs
public class Property
{
public Property()
{
PropertyGalleries = new Collection<PropertyGallery>();
}
public int Id{get;set;}
public string Title { get; set; }
public string Slug { get; set; }
public PropertyStatus PropertyStatus { get; set; }
public int PropertyStatusId { get; set; }
}
//PropertyController.cs
[Route("property/{id}/{*slug}")]
public ActionResult Details(int id, string slug)
{
var property = _propertyRepository.GetById(id);
if (property == null)
return NotFound();
var viewModel = new PropertyDetailsVM
{
Property = property,
Id = property.Id,
Title = property.Title,
Slug = property.Slug,
PropertyStatus=property.PropertyStatus
}
}
//PropertyDetailsVM
public class PropertyDetailsVM
{
public int Id { get; set; }
public string Title { get; set; }
public string Slug {get; set; }
public Property Property { get; set; }
public PropertyStatus PropertyStatus { get; set; }
}
根据您的代码,属性Status 和 属性 class 配置为一对多关系。如果是这样,在查询属性时,可以使用Include方法通过导航属性.
加载它的属性状态请参考以下示例:
//PropertyStatus.cs
public class PropertyStatus
{
public PropertyStatus()
{
Properties = new List<Property>();
}
public int Id { get; set; }
public string Title { get; set; }
public string Slug { get; set; }
public ICollection<Property> Properties { get; set; }
}
//Property.cs
public class Property
{
public int Id { get; set; }
public string Title { get; set; }
public string Slug { get; set; }
public PropertyStatus PropertyStatus { get; set; }
public int PropertyStatusId { get; set; }
}
控制器:
private readonly ILogger<HomeController> _logger;
//private readonly IDbContextProvider _dbProvider;
private readonly IConfiguration Configuration;
private readonly ApplicationDbContext _context;
public HomeController(ILogger<HomeController> logger, ApplicationDbContext context, IConfiguration configuration)
{
_logger = logger;
//_dbProvider = provider;
Configuration = configuration;
_context = context;
}
public IActionResult Index()
{
//required using Microsoft.EntityFrameworkCore;
//you can check your _propertyRepository.GetById method, and sue the include method
var property = _context.Properties.Include(c => c.PropertyStatus).Where(c => c.Id == 1).FirstOrDefault();
if (property == null)
return NotFound();
var viewModel = new PropertyDetailsVM
{
Property = property,
Id = property.Id,
Title = property.Title,
Slug = property.Slug,
PropertyStatus = property.PropertyStatus
};
return View(viewModel);
}
然后在查看页面:使用如下代码显示属性状态信息:
@model Core3_1MVC.Models.PropertyDetailsVM
@{
ViewData["Title"] = "Index";
}
<div>
<dl class="row">
<dt class = "col-sm-2">
@Html.DisplayNameFor(model => model.Id)
</dt>
<dd class = "col-sm-10">
@Html.DisplayFor(model => model.Id)
</dd>
<dt class = "col-sm-2">
@Html.DisplayNameFor(model => model.Title)
</dt>
<dd class = "col-sm-10">
@Html.DisplayFor(model => model.Title)
</dd>
<dt class = "col-sm-2">
@Html.DisplayNameFor(model => model.Slug)
</dt>
<dd class = "col-sm-10">
@Html.DisplayFor(model => model.Slug)
</dd>
<dt class = "col-sm-2">
PropertyStatus Title:
</dt>
<dd class = "col-sm-10">
@Html.DisplayFor(model => model.PropertyStatus.Title)
</dd>
</dl>
</div>
截图如下:
更详细的信息,您可以参考以下文章: