当表单提交给操作[重复]时,模型为null>

问题描述 投票:0回答:1

但是如果我想再次提交表单,它将在Edit Action方法的AlbumViewModel中为我提供空的Album对象。 (我添加了屏幕截图)enter image description hereViewModel:

        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正确绑定到文本框)。但是,如果我想再次提交该表格,它将给我...

c# asp.net-mvc asp.net-mvc-5 model-binding
1个回答
0
投票

使用[FromBody]

© www.soinside.com 2019 - 2024. All rights reserved.