最新のものまですべてVisual Studio 2017を使用しています。 私はいくつかの通常のテキストボックス回答質問とチェックボックス回答質問でフォームに入力しようとしており、すべての回答が同じデータベースに記録されています。私の問題の一部は、別々のデータベースを使用して別々のデータベースを使用して1つのコントローラにこれらの質問の参照として、別の質問に1つのデータベースレコード内のすべての質問回答の一覧を表示する方法です。多分私は間違ったやり方をしています。私はASP.NETを初めて使っているので、これはおそらくそうです。この問題への任意の入力を大幅に...2つの独立したモデルを使用して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; }
}
}
モデルII:
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>
繰り返しますが、私の目標は、同様に通常のテキストボックスの回答やチェックボックスセクションのセクションを持っているWebページ上のフォームを持っているだけです。どんな助けも大歓迎です。
public class YourNewModel
{
public Applicant Applicant {get;set;} //note you can declare it here as a List or any how you want
public Roles Roles {get;set;}
}
注これはあなたのアイデアを与え、あなたが持って起動するだけです :あなたは何ができるか
モデル1とモデル2を組み合わせて、フロントエンドにペイロードとして配信されるモデル3にすることができます。または、別のコントローラ呼び出しまたはajax呼び出しを実行して、DBからデータを取得しようとしているように、モデル1を取得することもできます。それが別のコントローラ呼び出しだった場合、それはそれ自身のビューに結びついていました。 –
2つの異なるモデルを1つに統合したい場合は、viewmodel.httpという概念を使用できます。//tutlane.com/tutorial/aspnet-mvc/how-to-use-viewmodel-in-asp-net-mvc- with-example –