asp.net-mvc - Asp.Net MVC 在一个视图中显示两个表
问题描述
我有两张桌子。一个叫“人”,另一个叫“事件”
在应用程序中,它们都实现了 CRUD 操作。我想要做的是在一个视图中显示两个表格。
有两个控制器。一个用于“人”,另一个用于“事件”。
他们都有 CRUD 操作的原因是我需要用户能够分别创建人员条目和事件条目,但在同一个视图上彼此同步显示。我曾尝试为此使用 ViewModel,但一直遇到错误。因此,我现在寻求帮助。
型号代码为:
public int PersonId { get; set; }
[Display(Name = "Service ID:")]
public string Service_ID { get; set; }
[Display(Name = "Rank/ Title:")]
public string Rank { get; set; }
[Display(Name = "Initials:")]
public string Initials { get; set; }
[Display(Name = "First Name:")]
public string First_Name { get; set; }
[Display(Name = "Middle Name:")]
public string Middle_Name { get; set; }
[Display(Name = "Surname:")]
public string Surname { get; set; }
[Display(Name = "Service:")]
public string Service { get; set; }
[Display(Name = "Corps:")]
public string Corps { get; set; }
[Display(Name = "Unit:")]
public string Unit { get; set; }
[Display(Name = "DOB::")]
[DataType(DataType.Date)]
public DateTime DOB { get; set; }
[Display(Name = "Gender:")]
public string Gender { get; set; }
[Display(Name = "Address:")]
public string Address { get; set; }
[Display(Name = "Phone:")]
public string Phone { get; set; }
[Display(Name = "Plate Number:")]
public string Vehicle_Registration { get; set; }
[Display(Name = "Make:")]
public string Make { get; set; }
[Display(Name = "Model:")]
public string Model { get; set; }
[Display(Name = "Year:")]
public string Year { get; set; }
[Display(Name = "Colour:")]
public string Colour { get; set; }
[Display(Name = "WOF Exp:")]
[DataType(DataType.Date)]
public DateTime WOF { get; set; }
[Display(Name = "REGO Exp:")]
[DataType(DataType.Date)]
public DateTime REGO { get; set; }
public int IncidentId { get; set; }
[Display(Name = "Event Number")]
public string Event { get; set; }
[Display(Name = "POC")]
public string MP { get; set; }
[Display(Name = "Incident/ Offence")]
public string Incident { get; set; }
[Display(Name = "Location of Incident")]
public string Location { get; set; }
[Display(Name = "Date/Time of Incident")]
public DateTime Date_Time { get; set; }
[Display(Name = "Role in Incident")]
public string Role { get; set; }
[Display(Name = "Text")]
public string IncidentDetail { get; set; }
[Display(Name = "BOR")]
public string BOR { get; set; }
[Display(Name = "Action Taken")]
public string Action { get; set; }
人物背景
public class PersonsContext : DbContext
{
public PersonsContext (DbContextOptions<PersonsContext> options)
: base(options)
{
}
public DbSet<Persons> Persons { get; set; }
public DbSet<Incidents> Incidents { get; set; }
}
人事控制者
public class PersonsController: Controller
{
private readonly PersonsContext _context;
public PersonsController(PersonsContext context)
{
_context = context;
}
// GET: Persons
public async Task<IActionResult> Index(string searchstring, string searchstring1, string searchstring2, string searchstring3)
{
var persons = from m in _context.Persons
select m;
if (!String.IsNullOrEmpty(searchstring))
{
persons = persons.Where(s => s.Service_ID.Contains(searchstring));
}
if (!String.IsNullOrEmpty(searchstring1))
{
persons = persons.Where(s => s.First_Name.Contains(searchstring1));
}
if (!String.IsNullOrEmpty(searchstring2))
{
persons = persons.Where(s => s.Surname.Contains(searchstring2));
}
if (!String.IsNullOrEmpty(searchstring3))
{
persons = persons.Where(s => s.Vehicle_Registration.Contains(searchstring3));
}
return View(await persons.ToListAsync());
}
// GET: Persons/Details/5
public async Task<IActionResult> Details(int? id)
{
if (id == null)
{
return NotFound();
}
var persons = await _context.Persons
.FirstOrDefaultAsync(m => m.PersonId == id);
if (persons == null)
{
return NotFound();
}
return View(persons);
}
// GET: Persons/Create
public IActionResult Create()
{
return View();
}
// POST: Persons/Create
// To protect from overposting attacks, enable the specific properties you want to bind to, for
// more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Id,Service_ID,Rank,Initials,First_Name,Middle_Name,Surname,Service,Corps,Unit,DOB,Gender,Address,Phone,Vehicle_Registration,Make,Model,Year,Colour,WOF,REGO")] Persons persons)
{
if (ModelState.IsValid)
{
_context.Add(persons);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(persons);
}
// GET: Persons/Edit/5
public async Task<IActionResult> Edit(int? id)
{
if (id == null)
{
return NotFound();
}
var persons = await _context.Persons.FindAsync(id);
if (persons == null)
{
return NotFound();
}
return View(persons);
}
// POST: Persons/Edit/5
// To protect from overposting attacks, enable the specific properties you want to bind to, for
// more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, [Bind("Id,Service_ID,Rank,Initials,First_Name,Middle_Name,Surname,Service,Corps,Unit,DOB,Gender,Address,Phone,Vehicle_Registration,Make,Model,Year,Colour,WOF,REGO")] Persons persons)
{
if (id != persons.PersonId)
{
return NotFound();
}
if (ModelState.IsValid)
{
try
{
_context.Update(persons);
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!PersonsExists(persons.PersonId))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToAction(nameof(Index));
}
return View(persons);
}
// GET: Persons/Delete/5
public async Task<IActionResult> Delete(int? id)
{
if (id == null)
{
return NotFound();
}
var persons = await _context.Persons
.FirstOrDefaultAsync(m => m.PersonId == id);
if (persons == null)
{
return NotFound();
}
return View(persons);
}
// POST: Persons/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(int id)
{
var persons = await _context.Persons.FindAsync(id);
_context.Persons.Remove(persons);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
private bool PersonsExists(int id)
{
return _context.Persons.Any(e => e.PersonId == id);
}
}
事件背景
public class IncidentsContext : DbContext
{
public IncidentsContext (DbContextOptions<IncidentsContext> options)
: base(options)
{
}
public DbSet<MP_EST.Models.Incidents> Incidents { get; set; }
}
}
事故控制器
public class IncidentsController : Controller
{
private readonly IncidentsContext _context;
public IncidentsController(IncidentsContext context)
{
_context = context;
}
// GET: Incidents
public async Task<IActionResult> Index()
{
return View(await _context.Incidents.ToListAsync());
}
// GET: Incidents/Details/5
public async Task<IActionResult> Details(int? id)
{
if (id == null)
{
return NotFound();
}
var incidents = await _context.Incidents
.FirstOrDefaultAsync(m => m.IncidentId == id);
if (incidents == null)
{
return NotFound();
}
return View(incidents);
}
// GET: Incidents/Create
public IActionResult Create()
{
return View();
}
// POST: Incidents/Create
// To protect from overposting attacks, enable the specific properties you want to bind to, for
// more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Id,Event,NZDFMP,Incident,Location,Date_Time,Role,IncidentDetail,BOR,Action")] Incidents incidents)
{
if (ModelState.IsValid)
{
_context.Add(incidents);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(incidents);
}
// GET: Incidents/Edit/5
public async Task<IActionResult> Edit(int? id)
{
if (id == null)
{
return NotFound();
}
var incidents = await _context.Incidents.FindAsync(id);
if (incidents == null)
{
return NotFound();
}
return View(incidents);
}
// POST: Incidents/Edit/5
// To protect from overposting attacks, enable the specific properties you want to bind to, for
// more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, [Bind("Id,Event,NZDFMP,Incident,Location,Date_Time,Role,IncidentDetail,BOR,Action")] Incidents incidents)
{
if (id != incidents.IncidentId)
{
return NotFound();
}
if (ModelState.IsValid)
{
try
{
_context.Update(incidents);
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!IncidentsExists(incidents.IncidentId))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToAction(nameof(Index));
}
return View(incidents);
}
// GET: Incidents/Delete/5
public async Task<IActionResult> Delete(int? id)
{
if (id == null)
{
return NotFound();
}
var incidents = await _context.Incidents
.FirstOrDefaultAsync(m => m.IncidentId == id);
if (incidents == null)
{
return NotFound();
}
return View(incidents);
}
// POST: Incidents/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(int id)
{
var incidents = await _context.Incidents.FindAsync(id);
_context.Incidents.Remove(incidents);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
private bool IncidentsExists(int id)
{
return _context.Incidents.Any(e => e.IncidentId == id);
}
}
视图模型类
public class ViewDetail
{
public Persons persons { get; set; }
public Incidents incidents { get; set; }
}
视图细节控制器
public class ViewDetailController : Controller
{
public IActionResult Index()
{
return View();
}
}
解决方案
使用下面的示例代码,我们可以将两个模型组合成一个模型,然后在视图中返回,基本上这就是视图模型的概念。
var users = (from u in dbContext.SeekerAccounts join od in dbContext.UserTypes on u.user_type_id 等于 od.Id_UserType where u.email.Equals(_username) && u.password.Equals(encodepassword) select new { u.City, u .contact_number,u.Current_Address,u.date_of_birth,u.email,u.email_notification_active,u.Full_Name,u.gender,u.Home_Phone,u.Id_SeekerAccount,u.Is_active,u.Location,u.Nationality,u.password , u.registration_date, u.ResumeFileName, u.SMS_notification_active, u.user_type_id, od.User_Type, od.user_type_name }).FirstOrDefault(); userProfile.id = users.Id_SeekerAccount;用户资料。城市=用户。城市;userProfile.userType = users.User_Type; userProfile.contact_number = users.contact_number; userProfile.Current_Address = users.Current_Address; userProfile.date_of_birth = users.date_of_birth; userProfile.email = 用户.email; userProfile.email_notification_active = users.email_notification_active;userProfile.Full_Name = users.Full_Name; userProfile.gender = 用户.gender; userProfile.Home_Phone = users.Home_Phone; userProfile.Is_active = users.Is_active; userProfile.Location = 用户.Location; userProfile.Nationality = users.Nationality; userProfile.registration_date = users.registration_date; 用户资料。ResumeFileName = users.ResumeFileName; userProfile.SMS_notification_active = users.SMS_notification_active;userProfile.user_type_id = users.user_type_id; 会话[“用户配置文件”] = 用户配置文件;if (users != null) { return true; } 否则 { 返回假;}
推荐阅读
- javascript - CSS:将 div 元素垂直和水平放置在中心,将页脚放置在底部
- python - 如何使用 NLTK 制作更高级的响应聊天机器人?
- javascript - 条件内容如果状态值
- mapbox - Choropleth 图层 mapbox geojson 不起作用
- php - 从 WooCommerce 3 中的订单中获取税率
- typescript - 通过 Typescript 检查对象接口
- python - 如何使用函数退出 for 循环
- r - 如何将 SF 对象转换为空间点数据框?
- bash - 有没有办法过滤 bash 'find' 命令的输出以排除“权限被拒绝”的结果?
- java - 缓存上下文时,MockIntegrationMessage 不起作用