gpt4 book ai didi

c# - 如何将数据传递给 Controller ​​-一对多关系.net core

转载 作者:太空宇宙 更新时间:2023-11-03 14:39:35 24 4
gpt4 key购买 nike

我想把这些数据保存到数据库

有两个模型。运动员和客户

关系是客户有很多运动员

我想知道如何将客户详细信息与运动员详细信息一起传递

public class Athlete
{
[Key]
public string ID { get; set; } = Guid.NewGuid().ToString();
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string Description { get; set; }
public Client Client { get; set; }
}



public class Client
{
[Key]
public string ID { get; set; } = Guid.NewGuid().ToString();
public string CompanyName { get; set; }
public string ApplicationUserId { get; set; }
public List<Athlete> Athletes { get; set; }
}

//这是HTML表单

<li>
<label>First Name</label>
@Html.TextBoxFor(x => x.FirstName, new { maxlength = 100 })
</li>
<li>
<label>Last Name</label>
@Html.TextBoxFor(x => x.LastName, new { maxlength = 100 })
</li>
<li>
<label>Email</label>
@Html.TextBoxFor(x => x.Email, new { maxlength = 100 })
</li>
<li>
<label>Address Line 3</label>
@Html.TextBoxFor(x => x.AddressLine3, new { maxlength = 100 })
</li>
</ul>

最佳答案

I want to know how to pass client details with Athlete details

您可以在添加客户端详细信息时使用 javascript 添加多个运动员详细信息,并在 Controller 中更新数据库,如下所示:

模型

public class Client
{
[Key]
public string ID { get; set; } = Guid.NewGuid().ToString();
public string CompanyName { get; set; }
public string ApplicationUserId { get; set; }
public List<Athlete> Athletes { get; set; }
}
public class Athlete
{
[Key]
public string ID { get; set; } = Guid.NewGuid().ToString();
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string Description { get; set; }
//public Client Client { get; set; }
}

查看

@model WebApplication1.Models.Clients.Client

<div class="row">
<div class="col-md-4">
<form asp-action="Create">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="CompanyName" class="control-label"></label>
<input asp-for="CompanyName" class="form-control" />
<span asp-validation-for="CompanyName" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="ApplicationUserId" class="control-label"></label>
<input asp-for="ApplicationUserId" class="form-control" />
<span asp-validation-for="ApplicationUserId" class="text-danger"></span>
</div>

<div class="form-group">
<label asp-for="Athletes" class="control-label"></label>
<table id="tblAthletes" class="table" cellpadding="0" cellspacing="0">
<thead>
<tr>
<th style="width:150px">First Name</th>
<th style="width:150px">Last Name</th>
<th style="width:150px">Email</th>
<th style="width:150px">Description</th>
<th></th>
</tr>
</thead>
<tbody id="item-list">
<tr>
<td><input type="text" asp-for="Athletes" class="items" name="Athletes[0].FirstName" /></td>
<td><input type="text" asp-for="Athletes" class="items" name="Athletes[0].LastName" /></td>
<td><input type="text" asp-for="Athletes" class="items" name="Athletes[0].Email" /></td>
<td><input type="text" asp-for="Athletes" class="items" name="Athletes[0].Description" /></td>
<td><input type="button" id="btnAdd" value="Add" /></td>
</tr>
</tbody>
</table>
</div>

<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
</div>
</div>

@section Scripts
{
<script type="text/javascript">
$("#btnAdd").click(function (e) {

e.preventDefault();
var i = ($(".items").length) / 4;
var n = '<tr>' + '<td><input type="text" class="items" name="athletes[' + i + '].FirstName" /></td>' +
'<td><input type="text" class="items" name="athletes[' + i + '].LastName" /></td>' +
'<td><input type="text" class="items" name="athletes[' + i + '].Email" /></td>' +
'<td><input type="text" class="items" name="athletes[' + i + '].Description" /></td>' +'</tr>';

$("#item-list").append(n);

</script>
}

Controller

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create( Client client)
{
if (ModelState.IsValid)
{
_context.Add(client);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(client);
}

关于c# - 如何将数据传递给 Controller ​​-一对多关系.net core,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57882018/

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