我对ASP.NET很陌生,所以我可能比这更加努力。我有两个自定义类,定义如下:
public class Job
{
public int ID { get; set; }
public Address Pickup { get; set; }
public Address Dropoff { get; set; }
public string Data { get; set; }
}
public class Address
{
public int ID { get; set; }
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
public string AddressLine3 { get; set; }
[Required]
public string City { get; set; }
public string Postcode { get; set; }
}
我将它们显示在Job的索引页面上,获取所有作业并将它们作为列表返回,然后在Index.cshtml
我能够提取一些字段以便地址返回有意义的:
@model IEnumerable<MvcJobs.Models.Job>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Pickup) Address
</th>
<th>
@Html.DisplayNameFor(model => model.Dropoff) Address
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model) {
<tr>
<td>
<pre>@Html.DisplayFor(modelItem => item.Pickup.AddressLine1)
@Html.DisplayFor(modelItem => item.Pickup.AddressLine2)
@Html.DisplayFor(modelItem => item.Pickup.AddressLine3)
@Html.DisplayFor(modelItem => item.Pickup.City)
@Html.DisplayFor(modelItem => item.Pickup.Postcode)</pre>
</td>
<td>
<pre>@Html.DisplayFor(modelItem => item.Dropoff.AddressLine1)
@Html.DisplayFor(modelItem => item.Dropoff.AddressLine2)
@Html.DisplayFor(modelItem => item.Dropoff.AddressLine3)
@Html.DisplayFor(modelItem => item.Dropoff.City)
@Html.DisplayFor(modelItem => item.Dropoff.Postcode)</pre>
<a asp-action="Addresses/Edit" asp-route-id="@item.Dropoff.ID">Edit</a>
</td>
<td>
<a asp-action="Edit" asp-route-id="@item.ID">Edit</a>
</td>
</tr>
}
</tbody>
</table>
这将显示表格中的每个作业,其地址格式正确并显示。找出生成的Index.cshtml
文件并相应地调整它并不困难。不幸的是,生成的Edit.cshtml
文件格式不同:
@model MvcJobs.Models.Job
<form asp-action="Edit">
<div class="form-horizontal">
<h4>Job</h4>
<hr />
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<input type="hidden" asp-for="ID" />
<div class="form-group">
<label asp-for="Data" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="Data" class="form-control" />
<span asp-validation-for="Data" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
</form>
当以不同的方式访问和返回数据时,我不知道如何显示每个地址的属性,允许对它们进行编辑然后保存它们。任何建议都是受欢迎的。基本上,我希望表单不仅显示作业详细信息,还显示每个地址的详细信息,当我点击保存时,它会更新作业以及与之关联的两个地址。
感谢。