但是如果我想再次提交表单,它将在Edit Action方法的AlbumViewModel中为我提供空的Album对象。 (我添加了屏幕截图)ViewModel:
public class AlbumViewModel { public Album Album { get; set; } }
动作:
[HttpPost] [ValidateAntiForgeryToken] public async Task<ActionResult> Edit(AlbumViewModel album) { if (ModelState.IsValid) { this._albumService.UpdateAlbum(album); } return RedirectToAction("Index"); }
cshtml:
@model Musicalog.Application.ViewModels.AlbumViewModel
@{
ViewBag.Title = "Edit";
}
<h2>Edit</h2>
@using (Html.BeginForm(Html.BeginForm("Edit", null, FormMethod.Post, null)))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => Model.Album.AlbumID)
@Html.HiddenFor(model => Model.Album.AlbumName)
@Html.HiddenFor(model => Model.Album.Artist)
@Html.HiddenFor(model => Model.Album.Stock)
@Html.HiddenFor(model => Model.Album.Type)
<div class="form-group">
@Html.LabelFor(model => model.Album.AlbumName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Album.AlbumName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Album.AlbumName, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.Album.AlbumName)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-success" />
</div>
</div>
我有一个简单的“编辑”页面。当我初始化编辑页面时,它可以正常工作(调用服务并将ViewModel正确绑定到文本框)。但是,如果我想再次提交该表格,它将给我...
使用[FromBody]