![](/img/trans.png)
[英]GetExternalLoginInfoAsync returns null in asp.net core 2.0
[英]ASP.NET Core 2.0 Model returns null for items not used in a form
上下文
ASP.NET Core 2.0 MVC項目
問題
使用視圖模型是這樣的:
public class UpdateReturnIssueViewModel
{
public IssueTrackerIssue Issue { get; set; }
public bool CloseIssue { get; set; }
}
其中IssueTrackerIssue是這樣的:
public class IssueTrackerIssue
{
[Required]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ID { get; set; }
[StringLength(19)]
public string OrderNumber { get; set; }
[Range(1, 50)]
public int Quantity { get; set; }
[StringLength(200)]
public string ReturnCustomerComment { get; set; }
}
我這樣獲取View的數據:
[HttpGet]
public IActionResult UpdateReturnIssue(int ID)
{
// populate the view model in some way
return View(this.updateReturnIssueViewModel);
}
在我的視圖中,我僅使用(即為其定義輸入asp-for標記)IssueTrackerIssue.OrderNumber和CloseIssue。
這將被發布到控制器,因此:
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult UpdateReturnIssue(UpdateReturnIssueViewModel viewModel)
{
if (this.ModelState.IsValid)
{
// Some other processing
}
}
在控制器中檢查viewModel時,我看到除了 View中使用的屬性外 ,所有內容均為null。
題
如何獲取從控制器傳遞到View並完整返回到Controller的View模型的全部內容? 換句話說,如何在視圖模型未使用該視圖模型的某些部分的情況下完全往返於該視圖模型?
原來有兩個問題需要解決。
1)正如@JoshMein和其他人指出的那樣,必須在表單上使用值,否則它們將以null的形式發布回控制器。 使用隱藏值可以克服此問題。
2)我的視圖是主視圖-細節類型視圖,並且細節集合始終返回null,即為空集合。 我現在意識到這是由於我在View中編碼了Detail集合的迭代的方式所致。 我在用
@foreach (IssueTrackerIssueComment comment in Model.Issue.Comments)
{
<tr>
<td><input asp-for="@comment.ID" type="hidden" /></td>
<td><input asp-for="@comment.DateCreated" type="hidden" /></td>
<td><input asp-for="@comment.LastUpdated" type="hidden" /></td>
<td>
<input asp-for="@comment.DateCreated" class="form-control" type="date" asp-format="{0:yyyy-MM-dd}" />
</td>
<td>
<input asp-for="@comment.Narrative" class="form-control" />
</td>
<td>
<button asp-action="DeleteReturnIssueComment" asp-route-id=@comment.ID class="btn btn-primary">
<span class="glyphicon glyphicon-trash" />
</button>
</td>
</tr>
}
ASP.NET顯然無法弄清楚這些@comment項是否已綁定到模型,因此它沒有將它們傳遞回Controller。
因此,我將詳細信息集合從ICollection <>更改為IList <>,並使用@for而不是@foreach,因此:
@for (int i = 0; i < Model.Issue.Comments.Count; i++)
{
<tr>
<td><input asp-for="@Model.Issue.Comments[i].ID" type="hidden" /></td>
<td><input asp-for="@Model.Issue.Comments[i].DateCreated" type="hidden" /></td>
<td><input asp-for="@Model.Issue.Comments[i].LastUpdated" type="hidden" /></td>
<td>
<input asp-for="@Model.Issue.Comments[i].DateCreated" class="form-control" type="date" asp-format="{0:yyyy-MM-dd}" />
</td>
<td>
<input asp-for="@Model.Issue.Comments[i].Narrative" class="form-control" />
</td>
<td>
<button asp-action="DeleteReturnIssueComment" asp-route-id=@Model.Issue.Comments[i].ID class="btn btn-primary">
<span class="glyphicon glyphicon-trash" />
</button>
</td>
</tr>
}
現在,使用@Model而不是@comment來引用這些項目,所有內容都可以很好地綁定在一起,並且我可以在控制器中看到注釋。
我不相信有一種方法可以不通過形式傳遞值。 您可以使用隱藏字段來存儲要傳遞回服務器的數據。 這是為表格中現有模型字段創建隱藏字段的剃刀語法。
@Html.HiddenFor(x => x.HiddenFieldName)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.