gpt4 book ai didi

c# - 如何将外键从一个 View 传递到另一个 View ?

转载 作者:行者123 更新时间:2023-12-04 07:29:00 27 4
gpt4 key购买 nike

我对 .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;}
}
}
我的索引 View 列出了所有未满/满的研讨会。
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>
}

这是我单击“选择”时调用的 Prebook 操作,这是我需要帮助的地方。
public IActionResult PreBook(){
ViewData["IdSeminara"] = new SelectList(_context.Seminars, "IdSeminar", "Naziv");
return View();
}
这是后期操作:
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult PreBook(PreBook obj)
{
if (ModelState.IsValid)
{
_context.PreBooks.Add(obj);
_context.SaveChanges();
return RedirectToAction("Index");
}
else
{
return View(obj);
}
}
这是预定 View 。
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>

那么,当我选择要预订的研讨会时,如何将其 key 发送到 PreBook View 并将其作为 PreBook 模型中的外键保存到 DB? (我还想在 PreBook View 中显示选定的研讨会名称)。我一直在寻找这一整天,但没有找到任何明确的解释。
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);
}
在 View 中,您可以使用以下代码
               <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);
}
}

关于c# - 如何将外键从一个 View 传递到另一个 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68057850/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com