将 ViewModel 传递给 View 不清除?
Passing ViewModel to a View not clearing down?
我有一个用 ASP.Net Core 3.1 编写的项目。我有一个视图模型,其中包含我所有测量员的列表和测量员的单个模型。该列表在我的 table 中用于显示所有条目,并且单个模型用于绑定到我的 Razor 视图中的输入控件。提交时,我在我的控制器中使用了所有更新方法,并检查传递的模型的 ID 是否为 0。如果为 0,我将其添加到我的数据库中,如果不是,我更新该条目。这一切都很好。然后我 return 使用新的视图模型查看视图。我的问题是它没有清除单个条目,因此所有输入控件仍然具有之前添加的条目。
查看:
@model SurveyorViewModel
<link href="~/CSS/Surveyor.css" rel="stylesheet" />
<form id="SurveyorList" asp-controller="Surveyor" asp-action="update" method="post" class="mt-3">
<input type="hidden" asp-for="Surveyor.PK_Id" />
<div id="cover" class="container vertical-center col-xl-5 col-lg-6 col-md-8 col-sm-10 mx-auto form p-4" style="box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2), 0 6px 20px 0 rgba(0,0,0,0.19);">
<div id="divSurveyorList" class="table mt-2">
<table id="surveyorList" class="table table-bordered table-hover">
<thead>
<tr>
<th>
<span style="font-family:Verdana; font-size: 12px; width: 150px;">Surveyors</span>
</th>
<th style="font-size: 12px; width: 40px; color:#6d6d6d;">Edit/Delete</th>
</tr>
</thead>
<tbody>
@if (Model != null)
{
foreach (var surveyor in Model.SurveyorList)
{
<tr style="color: #6d6d6d; font-family: Verdana; font-size:12px; font-weight:100;">
<td>
@surveyor.FullName
</td>
<td>
<div class="btn-group">
<a class="btn btn-primary" asp-controller="Surveyor" asp-action="EditSurveyor" asp-route-Id=@surveyor.PK_Id><span class="fas fa-pencil-alt"></span></a>
<a class="btn btn-danger" asp-controller="Surveyor" asp-action="DeleteSurveyor" asp-route-Id=@surveyor.PK_Id )><span class="fas fa-trash-alt"></span></a>
</div>
</td>
</tr>
}
}
</tbody>
</table>
</div>
<div class="row align-items-start mt-1">
<div class="col-4">
<label asp-for="Surveyor.FullName" class="col-sm-2 col-md-12 nopadding col-form-label">Full Name</label>
</div>
<div class="col-8">
<input asp-for="Surveyor.FullName" class="form-control" placeholder="Full Name" />
</div>
</div>
<div class="row align-items-start">
<div class="col-12">
<label asp-for="Surveyor.Address_Line_1" class="col-sm-2 col-md-4 col-form-label">Address</label>
</div>
</div>
<div class="row align-items-start">
<div class="col-12">
<input asp-for="Surveyor.Address_Line_1" class="form-control" placeholder="Address Line 1" />
</div>
</div>
<div class="row align-items-start">
<div class="col-12">
<input asp-for="Surveyor.Address_Line_2" class="form-control" placeholder="Address Line 2" />
</div>
</div>
<div class="row align-items-start">
<div class="col-12">
<input asp-for="Surveyor.Town" class="form-control" placeholder="Town" />
</div>
</div>
<div class="row align-items-start">
<div class="col-12">
<input asp-for="Surveyor.Postcode" class="form-control" placeholder="Post Code" />
</div>
</div>
<div class="row align-items-start">
<div class="col-12">
<label asp-for="Surveyor.ContactNo" class="col-sm-2 col-md-4 col-lg-6 col-form-label">Telephone No.</label>
</div>
</div>
<div class="row align-items-start">
<div class="col-12">
<input asp-for="Surveyor.ContactNo" class="form-control" placeholder="Telephone No." />
</div>
</div>
<div class="row align-items-start">
<div class="col-12">
<label asp-for="Surveyor.Email_Address" class="col-sm-2 col-md-4 col-lg-6 col-form-label">Email Address</label>
</div>
</div>
<div class="row align-items-start">
<div class="col-12">
<input asp-for="Surveyor.Email_Address" class="form-control" placeholder="Email Address" />
</div>
</div>
<div class="d-flex flex-row-reverse">
<button type="submit" class="btn btn-primary mt-2"><span class="fas fa-plus-circle"></span> Update</button>
<a class="btn btn-primary float-right mt-2" asp-controller="Surveyor" asp-action="Create" style="width: 100px; margin-right: 20px;" )><span class="fas fa-plus-circle"></span><span> Create</span></a>
<a class="btn btn-primary mt-2 mr-2" asp-controller="Surveyor" asp-action="Back" style="width: 90px;" )><span class="fas fa-hand-point-left"></span><span> Back</span></a>
</div>
</div>
</form>
视图模型:
public class SurveyorViewModel
{
public IEnumerable<Surveyor> SurveyorList { get; set; }
public Surveyor Surveyor { get; set; }
}
型号:
public class Surveyor
{
[Required]
[Key]
public int PK_Id { get; set; }
[Required]
[MaxLength(120)]
public string FullName { get; set; }
public string Address_Line_1 { get; set; }
public string Address_Line_2 { get; set; }
public string Town { get; set; }
public string Postcode { get; set; }
public string ContactNo { get; set; }
[EmailAddress]
public string Email_Address { get; set; }
public bool Archived { get; set; }
}
控制器方法:
private SurveyorViewModel PopulateLists()
{
SurveyorViewModel SurveyorViewModel = new SurveyorViewModel();
SurveyorViewModel.SurveyorList = context.Surveyors.ToList();
SurveyorViewModel.Surveyor = new Surveyor();
return SurveyorViewModel;
}
public IActionResult update(Surveyor surveyor)
{
if (ModelState.IsValid)
{
if (surveyor.PK_Id == 0)
{
surveyor = addSurveyor(surveyor);
}
else
{
surveyor = updateSurveyor(surveyor);
}
}
SurveyorViewModel surveyorViewModel = PopulateLists();
surveyorViewModel.Surveyor = surveyor;
return View("Surveyor", surveyorViewModel);
}
我改变了我的更新例程,在添加时 PK_Id 有一个传递给视图的值,但是当我检查元素时,我的隐藏字段的值为零。我做了一些研究,发现了以下代码,在检查时我的隐藏值具有正确的 PK_Id 值,但这没有传递给我的控制器???
<input type="hidden" value="@Model.Surveyor.PK_Id" name="PK_Id" />
非常感谢您的帮助,
My problem is it is not clearing the single entry so all the input controls still have the previously added entry.
在下面的代码中,您将 surveyorViewModel.Surveyor
设置为之前添加的条目
SurveyorViewModel surveyorViewModel = PopulateLists();
surveyorViewModel.Surveyor = surveyor;
return View("Surveyor", surveyorViewModel);
您可以尝试删除surveyorViewModel.Surveyor = surveyor;
,这样输入控件就不会再有之前添加的条目了。
I've altered my update routine which when doing the add the PK_Id has a value which is passed to the view but when I inspect the elements my hidden field has a value of zero. I did a bit of research and found the following bit of code, when doing the inspect my hidden value has the correct PK_Id value but this is not passed to my controller???
你在哪里传递 PK_Id 来查看,你在哪里有代码 <input type="hidden" value="@Model.Surveyor.PK_Id" name="PK_Id" />
?
我设法从这里的旧 post 找到了解决方案。基本上,在填充视图模型的新实例之前,我使用 ModelState.Remove("PK_Id")。从我读过的内容来看,视图将使用在 post 执行时过去的默认值,在我的情况下意味着因为它是一个新条目,它本来应该是 0。通过包含 remove 语句,它使用新的 Id我已经在我的模型中设置了。
我有一个用 ASP.Net Core 3.1 编写的项目。我有一个视图模型,其中包含我所有测量员的列表和测量员的单个模型。该列表在我的 table 中用于显示所有条目,并且单个模型用于绑定到我的 Razor 视图中的输入控件。提交时,我在我的控制器中使用了所有更新方法,并检查传递的模型的 ID 是否为 0。如果为 0,我将其添加到我的数据库中,如果不是,我更新该条目。这一切都很好。然后我 return 使用新的视图模型查看视图。我的问题是它没有清除单个条目,因此所有输入控件仍然具有之前添加的条目。
查看:
@model SurveyorViewModel
<link href="~/CSS/Surveyor.css" rel="stylesheet" />
<form id="SurveyorList" asp-controller="Surveyor" asp-action="update" method="post" class="mt-3">
<input type="hidden" asp-for="Surveyor.PK_Id" />
<div id="cover" class="container vertical-center col-xl-5 col-lg-6 col-md-8 col-sm-10 mx-auto form p-4" style="box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2), 0 6px 20px 0 rgba(0,0,0,0.19);">
<div id="divSurveyorList" class="table mt-2">
<table id="surveyorList" class="table table-bordered table-hover">
<thead>
<tr>
<th>
<span style="font-family:Verdana; font-size: 12px; width: 150px;">Surveyors</span>
</th>
<th style="font-size: 12px; width: 40px; color:#6d6d6d;">Edit/Delete</th>
</tr>
</thead>
<tbody>
@if (Model != null)
{
foreach (var surveyor in Model.SurveyorList)
{
<tr style="color: #6d6d6d; font-family: Verdana; font-size:12px; font-weight:100;">
<td>
@surveyor.FullName
</td>
<td>
<div class="btn-group">
<a class="btn btn-primary" asp-controller="Surveyor" asp-action="EditSurveyor" asp-route-Id=@surveyor.PK_Id><span class="fas fa-pencil-alt"></span></a>
<a class="btn btn-danger" asp-controller="Surveyor" asp-action="DeleteSurveyor" asp-route-Id=@surveyor.PK_Id )><span class="fas fa-trash-alt"></span></a>
</div>
</td>
</tr>
}
}
</tbody>
</table>
</div>
<div class="row align-items-start mt-1">
<div class="col-4">
<label asp-for="Surveyor.FullName" class="col-sm-2 col-md-12 nopadding col-form-label">Full Name</label>
</div>
<div class="col-8">
<input asp-for="Surveyor.FullName" class="form-control" placeholder="Full Name" />
</div>
</div>
<div class="row align-items-start">
<div class="col-12">
<label asp-for="Surveyor.Address_Line_1" class="col-sm-2 col-md-4 col-form-label">Address</label>
</div>
</div>
<div class="row align-items-start">
<div class="col-12">
<input asp-for="Surveyor.Address_Line_1" class="form-control" placeholder="Address Line 1" />
</div>
</div>
<div class="row align-items-start">
<div class="col-12">
<input asp-for="Surveyor.Address_Line_2" class="form-control" placeholder="Address Line 2" />
</div>
</div>
<div class="row align-items-start">
<div class="col-12">
<input asp-for="Surveyor.Town" class="form-control" placeholder="Town" />
</div>
</div>
<div class="row align-items-start">
<div class="col-12">
<input asp-for="Surveyor.Postcode" class="form-control" placeholder="Post Code" />
</div>
</div>
<div class="row align-items-start">
<div class="col-12">
<label asp-for="Surveyor.ContactNo" class="col-sm-2 col-md-4 col-lg-6 col-form-label">Telephone No.</label>
</div>
</div>
<div class="row align-items-start">
<div class="col-12">
<input asp-for="Surveyor.ContactNo" class="form-control" placeholder="Telephone No." />
</div>
</div>
<div class="row align-items-start">
<div class="col-12">
<label asp-for="Surveyor.Email_Address" class="col-sm-2 col-md-4 col-lg-6 col-form-label">Email Address</label>
</div>
</div>
<div class="row align-items-start">
<div class="col-12">
<input asp-for="Surveyor.Email_Address" class="form-control" placeholder="Email Address" />
</div>
</div>
<div class="d-flex flex-row-reverse">
<button type="submit" class="btn btn-primary mt-2"><span class="fas fa-plus-circle"></span> Update</button>
<a class="btn btn-primary float-right mt-2" asp-controller="Surveyor" asp-action="Create" style="width: 100px; margin-right: 20px;" )><span class="fas fa-plus-circle"></span><span> Create</span></a>
<a class="btn btn-primary mt-2 mr-2" asp-controller="Surveyor" asp-action="Back" style="width: 90px;" )><span class="fas fa-hand-point-left"></span><span> Back</span></a>
</div>
</div>
</form>
视图模型:
public class SurveyorViewModel
{
public IEnumerable<Surveyor> SurveyorList { get; set; }
public Surveyor Surveyor { get; set; }
}
型号:
public class Surveyor
{
[Required]
[Key]
public int PK_Id { get; set; }
[Required]
[MaxLength(120)]
public string FullName { get; set; }
public string Address_Line_1 { get; set; }
public string Address_Line_2 { get; set; }
public string Town { get; set; }
public string Postcode { get; set; }
public string ContactNo { get; set; }
[EmailAddress]
public string Email_Address { get; set; }
public bool Archived { get; set; }
}
控制器方法:
private SurveyorViewModel PopulateLists()
{
SurveyorViewModel SurveyorViewModel = new SurveyorViewModel();
SurveyorViewModel.SurveyorList = context.Surveyors.ToList();
SurveyorViewModel.Surveyor = new Surveyor();
return SurveyorViewModel;
}
public IActionResult update(Surveyor surveyor)
{
if (ModelState.IsValid)
{
if (surveyor.PK_Id == 0)
{
surveyor = addSurveyor(surveyor);
}
else
{
surveyor = updateSurveyor(surveyor);
}
}
SurveyorViewModel surveyorViewModel = PopulateLists();
surveyorViewModel.Surveyor = surveyor;
return View("Surveyor", surveyorViewModel);
}
我改变了我的更新例程,在添加时 PK_Id 有一个传递给视图的值,但是当我检查元素时,我的隐藏字段的值为零。我做了一些研究,发现了以下代码,在检查时我的隐藏值具有正确的 PK_Id 值,但这没有传递给我的控制器???
<input type="hidden" value="@Model.Surveyor.PK_Id" name="PK_Id" />
非常感谢您的帮助,
My problem is it is not clearing the single entry so all the input controls still have the previously added entry.
在下面的代码中,您将 surveyorViewModel.Surveyor
设置为之前添加的条目
SurveyorViewModel surveyorViewModel = PopulateLists();
surveyorViewModel.Surveyor = surveyor;
return View("Surveyor", surveyorViewModel);
您可以尝试删除surveyorViewModel.Surveyor = surveyor;
,这样输入控件就不会再有之前添加的条目了。
I've altered my update routine which when doing the add the PK_Id has a value which is passed to the view but when I inspect the elements my hidden field has a value of zero. I did a bit of research and found the following bit of code, when doing the inspect my hidden value has the correct PK_Id value but this is not passed to my controller???
你在哪里传递 PK_Id 来查看,你在哪里有代码 <input type="hidden" value="@Model.Surveyor.PK_Id" name="PK_Id" />
?
我设法从这里的旧 post 找到了解决方案。基本上,在填充视图模型的新实例之前,我使用 ModelState.Remove("PK_Id")。从我读过的内容来看,视图将使用在 post 执行时过去的默认值,在我的情况下意味着因为它是一个新条目,它本来应该是 0。通过包含 remove 语句,它使用新的 Id我已经在我的模型中设置了。