如何将外键从一个视图传递到另一个视图?
How does one pass foreign key from one view to another?
我对 .NET 和 MVC 还很陌生,我正在尝试弄清楚模型关系。
我有2个模型,用于预订研讨会。
namespace App.Models{
public class PreBook{
[Key]
public int IdPreBook{get; set;}
[DataType(DataType.Date)]
public DateTime Date { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public string Adress { get; set; }
public string Email { get; set; }
public string PhoneNum { get; set; }
[ForeignKey("Seminar")]
public int IdSeminar { get; set; }
public Seminar Seminar {get;set;}
}
}
.
namespace App.Models{
public class Seminar{
[Key]
public int IdSeminar {get; set;}
public string Name {get;set;}
public string Description {get;set;}
[DataType(DataType.Date)]
public DateTime Date{ get; set; }
public bool Filled { get; set; }
public List<Predbiljezba> Predbiljezbe {get; set;}
}
}
我的索引视图列出了所有不是 full/filled 的研讨会。
Index view
@model IQueryable<Seminar>
<div class="container p-3">
<div class="row pt-4">
<form asp-action="Index" method="get">
<div class="form-actions no-color">
<p>
<b>Search:</b> <input type="text" name="SearchString" value="@ViewData["CurrentFilter"]" />
<input type="submit" value="Search" class="btn btn-default" /> |
<a asp-action="Index">Back to Full List</a>
</p>
</div>
</form>
</div>
@if(Model.Count() > 0){
<table class="table table-bordered table-striped" style="width:100%">
<thead>
<tr>
<th>
</th>
<th>
Name
</th>
<th>
Description
</th>
<th>
Date
</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
if(!item.Filled){
<tr>
<td class="text-center">
<a asp-controller="PreBook" asp-route-Id="@item.IdSeminar" asp-action="PreBook">Choose</a>
</td>
<td width="40%">@item.Naziv</td>
<td width="20%">@item.Opis</td>
<td width="20%">@item.Datum</td>
<!----<td width="30%"></td>---->
</tr>
}
}
</tbody>
</table>
}else{
<p>No category exists.</p>
}
这是我在点击“选择”时调用的预订操作,这是我需要帮助的地方。
public IActionResult PreBook(){
ViewData["IdSeminara"] = new SelectList(_context.Seminars, "IdSeminar", "Naziv");
return View();
}
这是 post 操作:
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult PreBook(PreBook obj)
{
if (ModelState.IsValid)
{
_context.PreBooks.Add(obj);
_context.SaveChanges();
return RedirectToAction("Index");
}
else
{
return View(obj);
}
}
这是预订前的视图。
PreBook View
及其代码:
@model PreBook
<form method="post" asp-action="PreBook">
<div class="border p-3">
<div asp-validation-summery="ModelOnly" class="text-danger"></div>
<p></p>
<h4>I want to display chosen seminar name here</h4>
<div class="row">
<div class="col-8">
<div class="form-group row">
<div class="col-4">
<b>Name:</b>
</div>
<div class="col-8">
<input asp-for="Name" class="form-control" />
<span asp-validation-for="Name" class="text-danger"></span>
</div>
</div>
<div class="form-group row">
<div class="col-4">
<b>Surname:</b>
</div>
<div class="col-8">
<input asp-for="Surname" class="form-controll" />
<span asp-validation-for="Surname" class="text-danger"></span>
</div>
</div>
<div class="form-group row">
<div class="col-4">
<b>Adress:</b>
</div>
<div class="col-8">
<input asp-for="Adress" class="form-controll" />
<span asp-validation-for="Adress" class="text-danger"></span>
</div>
</div>
<div class="form-group row">
<div class="col-4">
<b>Email:</b>
</div>
<div class="col-8">
<input asp-for="Email" class="form-controll" />
<span asp-validation-for="Email" class="text-danger"></span>
</div>
</div>
<div class="form-group row">
<div class="col-4">
<b>Phonenumber:</b>
</div>
<div class="col-8">
<input asp-for="PhoneNum" class="form-controll" />
<span asp-validation-for="PhoneNum" class="text-danger"></span>
</div>
</div>
<div class="form-group row">
<input type="submit" value="Confirm" class="btn btn-primary" />
</div>
</div>
<div class="col-4">
@*Keep this empty*@
</div>
</div>
</div>
那么,当我选择要预订的研讨会时,如何将其密钥发送到 PreBook 视图并将其作为 PreBook 模型中的外键保存到数据库中? (我还想在 PreBook 视图中显示所选的研讨会名称)。我一直在寻找这一天,但我还没有找到任何明确的解释。
dotnet 和 ef 都是 5.0
试试这个,它应该有效
[Route("Prebook/Prebook/{id}")]
public IActionResult PreBook(int Id){
//Get name of seminar from id
string seminarName=_context.Seminars.Where(x=>x.Id==Id).FirstOrDefault().Name;
Seminar sem=new Seminar(){Name=seminarName,Id=Id);
Prebook pre=new Preebook(){Seminar=sem,IdSeminar=Id}
return View(pre);
}
查看可以使用以下代码
<div class="col-8">
<input type="hidden" value="IdSeminar" name="idSeminar"/>
<input asp-for="Seminar.Name" class="form-control" />
<span asp-validation-for="Seminar.Name" class="text-danger">
</span>
</div>
还将异常处理应用到预订插入。
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult PreBook(PreBook obj)
{
if (ModelState.IsValid)
{
obj.Seminar=null;
try{
_context.PreBooks.Add(obj);
_context.SaveChanges();
}
catch(Exception ex)
{
Debug.Write(ex.Message);
}
return RedirectToAction("Index");
}
else
{
return View(obj);
}
}
我对 .NET 和 MVC 还很陌生,我正在尝试弄清楚模型关系。 我有2个模型,用于预订研讨会。
namespace App.Models{
public class PreBook{
[Key]
public int IdPreBook{get; set;}
[DataType(DataType.Date)]
public DateTime Date { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public string Adress { get; set; }
public string Email { get; set; }
public string PhoneNum { get; set; }
[ForeignKey("Seminar")]
public int IdSeminar { get; set; }
public Seminar Seminar {get;set;}
}
}
.
namespace App.Models{
public class Seminar{
[Key]
public int IdSeminar {get; set;}
public string Name {get;set;}
public string Description {get;set;}
[DataType(DataType.Date)]
public DateTime Date{ get; set; }
public bool Filled { get; set; }
public List<Predbiljezba> Predbiljezbe {get; set;}
}
}
我的索引视图列出了所有不是 full/filled 的研讨会。 Index view
@model IQueryable<Seminar>
<div class="container p-3">
<div class="row pt-4">
<form asp-action="Index" method="get">
<div class="form-actions no-color">
<p>
<b>Search:</b> <input type="text" name="SearchString" value="@ViewData["CurrentFilter"]" />
<input type="submit" value="Search" class="btn btn-default" /> |
<a asp-action="Index">Back to Full List</a>
</p>
</div>
</form>
</div>
@if(Model.Count() > 0){
<table class="table table-bordered table-striped" style="width:100%">
<thead>
<tr>
<th>
</th>
<th>
Name
</th>
<th>
Description
</th>
<th>
Date
</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
if(!item.Filled){
<tr>
<td class="text-center">
<a asp-controller="PreBook" asp-route-Id="@item.IdSeminar" asp-action="PreBook">Choose</a>
</td>
<td width="40%">@item.Naziv</td>
<td width="20%">@item.Opis</td>
<td width="20%">@item.Datum</td>
<!----<td width="30%"></td>---->
</tr>
}
}
</tbody>
</table>
}else{
<p>No category exists.</p>
}
这是我在点击“选择”时调用的预订操作,这是我需要帮助的地方。
public IActionResult PreBook(){
ViewData["IdSeminara"] = new SelectList(_context.Seminars, "IdSeminar", "Naziv");
return View();
}
这是 post 操作:
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult PreBook(PreBook obj)
{
if (ModelState.IsValid)
{
_context.PreBooks.Add(obj);
_context.SaveChanges();
return RedirectToAction("Index");
}
else
{
return View(obj);
}
}
这是预订前的视图。 PreBook View
及其代码:
@model PreBook
<form method="post" asp-action="PreBook">
<div class="border p-3">
<div asp-validation-summery="ModelOnly" class="text-danger"></div>
<p></p>
<h4>I want to display chosen seminar name here</h4>
<div class="row">
<div class="col-8">
<div class="form-group row">
<div class="col-4">
<b>Name:</b>
</div>
<div class="col-8">
<input asp-for="Name" class="form-control" />
<span asp-validation-for="Name" class="text-danger"></span>
</div>
</div>
<div class="form-group row">
<div class="col-4">
<b>Surname:</b>
</div>
<div class="col-8">
<input asp-for="Surname" class="form-controll" />
<span asp-validation-for="Surname" class="text-danger"></span>
</div>
</div>
<div class="form-group row">
<div class="col-4">
<b>Adress:</b>
</div>
<div class="col-8">
<input asp-for="Adress" class="form-controll" />
<span asp-validation-for="Adress" class="text-danger"></span>
</div>
</div>
<div class="form-group row">
<div class="col-4">
<b>Email:</b>
</div>
<div class="col-8">
<input asp-for="Email" class="form-controll" />
<span asp-validation-for="Email" class="text-danger"></span>
</div>
</div>
<div class="form-group row">
<div class="col-4">
<b>Phonenumber:</b>
</div>
<div class="col-8">
<input asp-for="PhoneNum" class="form-controll" />
<span asp-validation-for="PhoneNum" class="text-danger"></span>
</div>
</div>
<div class="form-group row">
<input type="submit" value="Confirm" class="btn btn-primary" />
</div>
</div>
<div class="col-4">
@*Keep this empty*@
</div>
</div>
</div>
那么,当我选择要预订的研讨会时,如何将其密钥发送到 PreBook 视图并将其作为 PreBook 模型中的外键保存到数据库中? (我还想在 PreBook 视图中显示所选的研讨会名称)。我一直在寻找这一天,但我还没有找到任何明确的解释。
dotnet 和 ef 都是 5.0
试试这个,它应该有效
[Route("Prebook/Prebook/{id}")]
public IActionResult PreBook(int Id){
//Get name of seminar from id
string seminarName=_context.Seminars.Where(x=>x.Id==Id).FirstOrDefault().Name;
Seminar sem=new Seminar(){Name=seminarName,Id=Id);
Prebook pre=new Preebook(){Seminar=sem,IdSeminar=Id}
return View(pre);
}
查看可以使用以下代码
<div class="col-8">
<input type="hidden" value="IdSeminar" name="idSeminar"/>
<input asp-for="Seminar.Name" class="form-control" />
<span asp-validation-for="Seminar.Name" class="text-danger">
</span>
</div>
还将异常处理应用到预订插入。
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult PreBook(PreBook obj)
{
if (ModelState.IsValid)
{
obj.Seminar=null;
try{
_context.PreBooks.Add(obj);
_context.SaveChanges();
}
catch(Exception ex)
{
Debug.Write(ex.Message);
}
return RedirectToAction("Index");
}
else
{
return View(obj);
}
}