我使用的是Visual Studio 2017,其中包含最新更新的所有內容。 我想輸入一些正常的文本框回答問題以及複選框答案問題,並將所有答案記錄在同一個數據庫中。我的問題的一部分是如何獲得兩個單獨的模型,使用單獨的數據庫將單獨的數據庫作爲單個控制器的參考,以及一個數據庫記錄中所有問題答案的列表的視圖?也許我正在以這種錯誤的方式進行討論,因爲我對ASP.NET很陌生,所以這很有可能。任何輸入到這個問題,將不勝感激......使用兩個獨立模型在ASP.NET上創建表單
下面是我的兩個型號代碼:
型號I:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
namespace TessituraForm
{
public class TessituraFormContext : DbContext
{
public TessituraFormContext(DbContextOptions<TessituraFormContext> options) : base(options)
{
}
public DbSet<Models.Roles> Roles { get; set; }
}
}
模式二:
using System;
using System.ComponentModel.DataAnnotations;
using Microsoft.EntityFrameworkCore;
namespace TessituraForm.Models
{
public class Applicant
{
public int ID { get; set; }
[Display(Name = "Full Name:")]
[DataType(DataType.Text)]
[Required]
public string FullName { get; set; }
[Display(Name = "SI User Name:")]
[StringLength(10)]
[DataType(DataType.Text)]
[Required]
public string SIUserName { get; set; }
[Display(Name = "Supervisor:")]
[Required]
public string Supervisor { get; set; }
[Display(Name = "Badge Number:")]
[StringLength(8)]
[DataType(DataType.Text)]
[Required]
public string BadgeNumber { get; set; }
[Display(Name = "Badge Expiration Date:")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:MM-dd-yyyy}", ApplyFormatInEditMode = true)]
[Required]
public DateTime BadgeExpirationDate { get; set; }
[Display(Name = "Applicant is a(n):")]
[DataType(DataType.Text)]
[Required]
public string ApplicantIsAn { get; set; }
}
public class Roles
{
[Key]
public int ID { get; set; }
public string AdvancementFundraising { get; set; }
public string CustomerService { get; set; }
public string DiscoveryTheater { get; set; }
public string BusinessOffice { get; set; }
public string CustomerServiceSupport { get; set; }
public string ExecutiveStaff { get; set; }
public string Marketing { get; set; }
public string Programming { get; set; }
public string VolunteerCoordinator { get; set; }
public bool CheckboxAnswer { get; set; }
}
}
控制器代碼:
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System.Linq;
using System.Threading.Tasks;
using TessituraForm.Models;
namespace Applicant.Controllers
{
public class ApplicantController : Controller
{
private readonly TessituraFormContext _context;
public ApplicantController(TessituraFormContext context)
{
_context = context;
}
// GET: Applicants
public async Task<IActionResult> Index()
{
return View(await _context.Applicant.ToListAsync());
}
// GET: Applicants/Details/5
public async Task<IActionResult> Details(int? id)
{
if (id == null)
{
return NotFound();
}
var applicant = await _context.Applicant
.SingleOrDefaultAsync(m => m.ID == id);
if (applicant == null)
{
return NotFound();
}
return View(applicant);
}
// GET: Applicants/Create
public IActionResult Create()
{
return View();
}
// POST: Applicants/Create
// To protect from overposting attacks, please 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,FullName,SIUserName,Supervisor,BadgeNumber,BadgeExpirationDate,ApplicantIsAn,Roles")] Applicant applicant)
{
if (ModelState.IsValid)
{
_context.Add(applicant);
await _context.SaveChangesAsync();
return RedirectToAction("Index");
}
return View(applicant);
}
// GET: Applicants/Edit/5
public async Task<IActionResult> Edit(int? id)
{
if (id == null)
{
return NotFound();
}
var applicant = await _context.Applicant.SingleOrDefaultAsync(m => m.ID == id);
if (applicant == null)
{
return NotFound();
}
return View(applicant);
}
// POST: Applicants/Edit/5
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, [Bind("ID,FullName,SIUserName,Supervisor,BadgeNumber,BadgeExpirationDate,ApplicantIsAn,Roles")] Applicant applicant)
{
if (id != applicant.ID)
{
return NotFound();
}
if (ModelState.IsValid)
{
try
{
_context.Update(applicant);
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!ApplicantExists(applicant.ID))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToAction("Index");
}
return View(applicant);
}
// GET: Applicants/Delete/5
public async Task<IActionResult> Delete(int? id)
{
if (id == null)
{
return NotFound();
}
var applicant = await _context.Applicant
.SingleOrDefaultAsync(m => m.ID == id);
if (applicant == null)
{
return NotFound();
}
return View(applicant);
}
// POST: Applicants/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(int id)
{
var applicant = await _context.Applicant.SingleOrDefaultAsync(m => m.ID == id);
_context.Applicant.Remove(applicant);
await _context.SaveChangesAsync();
return RedirectToAction("Index");
}
private bool ApplicantExists(int id)
{
return _context.Applicant.Any(e => e.ID == id);
}
}
}
namespace DatabaseContext.Controllers
{
public class DatabaseContextController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult Roles()
{
Applicant objApplicant = newApplicant();
}
}
}
並查看指數代碼:
@model IEnumerable<TessituraForm.Models.Applicant>
@{
ViewData["Title"] = "Index";
}
<style>
body {
background-color: lightgray;
}
h2 {
color: black;
margin-left: 5px;
}
</style>
<img src="~/Images/TSA.jpg" alt="TSA" style="position:absolute;left:1372px;top:57px;width:150px;height:75px;" />
<h2>Index</h2>
<p>
<a asp-action="Create" style="margin-left:20px;">Create New</a>
</p>
<table class="table" border="1">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.FullName)
</th>
<th>
@Html.DisplayNameFor(model => model.SIUserName)
</th>
<th>
@Html.DisplayNameFor(model => model.Supervisor)
</th>
<th>
@Html.DisplayNameFor(model => model.BadgeNumber)
</th>
<th>
@Html.DisplayNameFor(model => model.BadgeExpirationDate)
</th>
<th>
@Html.DisplayNameFor(model => model.ApplicantIsAn)
</th>
<th>
@Html.DisplayNameFor(model => model.Roles)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.FullName)
</td>
<td>
@Html.DisplayFor(modelItem => item.SIUserName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Supervisor)
</td>
<td>
@Html.DisplayFor(modelItem => item.BadgeNumber)
</td>
<td>
@Html.DisplayFor(modelItem => item.BadgeExpirationDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.ApplicantIsAn)
</td>
<td>
@Html.DisplayFor(modelItem => item.Roles)
</td>
<td>
<a asp-action="Edit" asp-route-id="@item.ID">Edit</a> |
<a asp-action="Details" asp-route-id="@item.ID">Details</a> |
<a asp-action="Delete" asp-route-id="@item.ID">Delete</a>
</td>
</tr>
}
</tbody>
</table>
同樣,我的目標很簡單,就是有網頁與普通文本框答案和一個複選框部分相當的部分,以及在一種形式。任何幫助深表感謝。
您可以將模型1和模型2組合成一個模型3,作爲有效載荷交付到前端。或者您可以執行單獨的控制器調用或ajax調用來獲取模型1,因爲它似乎試圖從數據庫中獲取數據。如果它是一個單獨的控制器調用,那麼它將與其自己的觀點相關聯。 –
如果你想將兩個不同的模型合併成一個,那麼你可以使用viewmodel的概念://tutlane.com/tutorial/aspnet-mvc/how-to-use-viewmodel-in-asp-net-mvc-例如 –