У меня проблема с отношением один к одному между двумя таблицами.Я не знаю, как связать две формы ввода, где одна форма привязана к одной таблице, а другая - к другой. Каждая форма имеет свой собственный вид и собственный контроллер.Пожалуйста, дайте мне совет или решение.
Это моя первая модель Ученик:
namespace Sport_student.Models
{
public class student
{
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int StudentId { get; set; }
[Display(Name = "Šifra ispitanika:")]
[Range(1, 9999)]
public int StudentNumber { get; set; }
public string Gender { get; set; }
[NotMapped]
public List<SelectListItem> Genders { set; get; }
[Display(Name = "Datum rođenja ispitanika:")]
[DataType(DataType.Date)]
public DateTime DateBirth { get; set; }
[Display(Name = "Mjesto rođenja ispitanika:")]
public string PlaceBirth { get; set; }
public string Class { get; set; }
[NotMapped]
public List<SelectListItem> Classes { set; get; }
[Display(Name = "Datum testiranja ispitanika:")]
[DataType(DataType.Date)]
public DateTime TestDate { get; set; }
[Display(Name = "Godina rođenja majke:")]
[Range(1900, 2000)]
public int MumDate { get; set; }
[Display(Name = "Godina rođenja oca:")]
[Range(1900, 2000)]
public int DadDate { get; set; }
public string MumSport { get; set; }
[NotMapped]
public List<SelectListItem> MumSports { set; get; }
public string DadSport { get; set; }
[NotMapped]
public List<SelectListItem> DadSports { set; get; }
public string WhichSportMum { get; set; }
[NotMapped]
public List<SelectListItem> WhichSportMums { set; get; }
public string WhichSportDad { get; set; }
[NotMapped]
public List<SelectListItem> WhichSportDads { set; get; }
public string SportStudentPast { get; set; }
[NotMapped]
public List<SelectListItem> SportsStudentPast { set; get; }
public string SportStudentWhat { get; set; }
[NotMapped]
public List<SelectListItem> SportsStudentWhat { set; get; }
public string HowLong { get; set; }
[NotMapped]
public List<SelectListItem> HowLongs { set; get; }
public string SameTime { get; set; }
[NotMapped]
public List<SelectListItem> SameTimes { set; get; }
//proba
[ForeignKey("MeasureId")]
public int? MeasureId { get; set; }
public virtual Measures Measures { get; set; }
}
}
Это моя вторая модель:
namespace Sport_student.Models
{
public class Measures
{
[Key]
public int MeasureId { get; set; }
[Display(Name = "Visina:")]
public decimal Visina { get; set; }
[Display(Name = "Težina:")]
public decimal Tezina { get; set; }
[Display(Name = "Skok u dalj:")]
public decimal Sdaljmj { get; set; }
[Display(Name = "Dvamet:")]
public decimal Dvamet { get; set; }
[Display(Name = "Japan:")]
public decimal Japan { get; set; }
[Display(Name = "Kus:")]
public decimal Kus { get; set; }
[Display(Name = "Pretkorak:")]
public decimal Pretk { get; set; }
[Display(Name = "Čučnjevi:")]
public decimal Cucnjevi { get; set; }
[Display(Name = "Bacmed:")]
public decimal Bacmed { get; set; }
[Display(Name = "Ravklup:")]
public decimal Ravklup { get; set; }
[Display(Name = "Ravnasa:")]
public decimal Ravnasa { get; set; }
[Display(Name = "BeepTest:")]
public decimal BeepTest { get; set; }
//proba
public virtual student Student { get; set; }
}
}
Контроллер для ученика:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("StudentId,StudentNumber,Gender,DateBirth,PlaceBirth,Class,TestDate,MumDate,DadDate,MumSport,DadSport,WhichSportMum,WhichSportDad,SportStudentPast,SportStudentWhat,HowLong,SameTime,MeasureId")] student student)
{
if (ModelState.IsValid)
{
_context.Add(student);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(student);
}
Контроллер для мер:
public async Task<IActionResult> Create_M([Bind("MeasureId,Visina,Tezina,Sdaljmj,Dvamet,Japan,Kus,Pretk,Cucnjevi,Bacmed,Ravklup,Ravnasa,BeepTest")] Measures measures)
{
if (ModelState.IsValid)
{
//tu stala
_context.Add(measures);
await _context.SaveChangesAsync();
return RedirectToAction("Create","Student");
}
return View(measures);
}
Просмотр с формой ввода для ученика:
<div class="row">
<div class="col-md-6">
<form asp-action="Create">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<input type="hidden" asp-for="MeasureId" />
<div class="form-group">
<label asp-for="StudentNumber" class="control-label"></label>
<input asp-for="StudentNumber" class="form-control" />
<span asp-validation-for="StudentNumber" class="text-danger"></span>
</div>
<div class="form-group">
<label>Spol:</label>
@foreach (var item in @ViewBag.Genders)
{
<input asp-for="Gender" type="radio" value="@item.Value" /> @item.Text
}
</div>
<div class="form-group">
<label asp-for="DateBirth" class="control-label"></label>
<input asp-for="DateBirth" class="form-control" />
<span asp-validation-for="DateBirth" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="PlaceBirth" class="control-label"></label>
<input asp-for="PlaceBirth" class="form-control" />
<span asp-validation-for="PlaceBirth" class="text-danger"></span>
</div>
<div class="form-group">
<label>Razred:</label>
@foreach (var item in @ViewBag.Classes)
{
<input asp-for="Class" type="radio" value="@item.Value" /> @item.Text
}
</div>
<div class="form-group">
<label asp-for="TestDate" class="control-label"></label>
<input asp-for="TestDate" class="form-control" />
<span asp-validation-for="TestDate" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="MumDate" class="control-label"></label>
<input asp-for="MumDate" class="form-control" />
<span asp-validation-for="MumDate" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="DadDate" class="control-label"></label>
<input asp-for="DadDate" class="form-control" />
<span asp-validation-for="DadDate" class="text-danger"></span>
</div>
<div class="form-group">
<label>Bavljenje sportom majke:</label>
<select asp-for="MumSport" asp-items="@(ViewBag.MumSports)">
<option>Odaberi</option>
</select>
</div>
<div class="form-group">
<label>Bavljenje sportom oca:</label>
<select asp-for="DadSport" asp-items="@(ViewBag.DadSports)">
<option>Odaberi</option>
</select>
</div>
<div class="form-group">
<label>Kojim se sportom najviše bavila majka:</label>
<select asp-for="WhichSportMum" asp-items="@(ViewBag.WhichSportMums)">
<option>Odaberi</option>
</select>
</div>
<div class="form-group">
<label>Kojim se sportom najviše bavio otac:</label>
<select asp-for="WhichSportDad" asp-items="@(ViewBag.WhichSportDads)">
<option>Odaberi</option>
</select>
</div>
<div class="form-group">
<label>Je li se polaznik do sada bavio nekim sportom?</label>
@foreach (var item in @ViewBag.SportsStudentPast)
{
<input asp-for="SportStudentPast" type="radio" value="@item.Value" /> @item.Text
}
</div>
<div class="form-group">
<label>Kojim sportom se bavio</label>
<select asp-for="SportStudentWhat" asp-items="@(ViewBag.SportsStudentWhat)">
<option>Odaberi</option>
</select>
</div>
<div class="form-group">
<label>Koliko dugo?</label>
@foreach (var item in @ViewBag.HowLongs)
{
<input asp-for="HowLong" type="radio" value="@item.Value" /> @item.Text
}
</div>
<div class="form-group">
<label>Ispitanik se:</label>
@foreach (var item in @ViewBag.SameTimes)
{
<input asp-for="SameTime" type="radio" value="@item.Value" /> @item.Text
}
</div>
<a href="~/Measures/Create_M">Create New</a>
<div class="form-group">
<input type="submit" value="Kreiraj" class="btn btn-primary" />
</div>
</form>
</div>
</div>
Просмотр с формой ввода для мер:
<div class="row">
<div class="col-md-4">
<form asp-action="Create_M">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Visina" class="control-label"></label>
<input asp-for="Visina" class="form-control" />
<span asp-validation-for="Visina" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Tezina" class="control-label"></label>
<input asp-for="Tezina" class="form-control" />
<span asp-validation-for="Tezina" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Sdaljmj" class="control-label"></label>
<input asp-for="Sdaljmj" class="form-control" />
<span asp-validation-for="Sdaljmj" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Dvamet" class="control-label"></label>
<input asp-for="Dvamet" class="form-control" />
<span asp-validation-for="Dvamet" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Japan" class="control-label"></label>
<input asp-for="Japan" class="form-control" />
<span asp-validation-for="Japan" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Kus" class="control-label"></label>
<input asp-for="Kus" class="form-control" />
<span asp-validation-for="Kus" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Pretk" class="control-label"></label>
<input asp-for="Pretk" class="form-control" />
<span asp-validation-for="Pretk" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Cucnjevi" class="control-label"></label>
<input asp-for="Cucnjevi" class="form-control" />
<span asp-validation-for="Cucnjevi" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Bacmed" class="control-label"></label>
<input asp-for="Bacmed" class="form-control" />
<span asp-validation-for="Bacmed" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Ravklup" class="control-label"></label>
<input asp-for="Ravklup" class="form-control" />
<span asp-validation-for="Ravklup" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Ravnasa" class="control-label"></label>
<input asp-for="Ravnasa" class="form-control" />
<span asp-validation-for="Ravnasa" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="BeepTest" class="control-label"></label>
<input asp-for="BeepTest" class="form-control" />
<span asp-validation-for="BeepTest" class="text-danger"></span>
</div>
@*//@Html.HiddenFor(p => p.MeasureId);*@
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
</div>
</div>
Мои таблицы (Student, Measures) в базе данных верны, в таблице Student есть FK MeasureId.Я действительно не знаю, как соединить две формы ввода из двух разных видов в двух разных контроллерах. Пожалуйста, направьте меня в правильном направлении.Любой совет приветствуется.Примечание: данные из форм заполнены правильно, но внешний ключ в таблице Student остается нулевым