首页 > 解决方案 > ASP.NET - 提交表单不传递数据

问题描述

我有这样的课:

public class saveModel
    {
        public int ID{ get; set; }
        [DisplayName("Area")]
        public string Area { get; set; }
        public string AreaDescription { get; set; }
        [DisplayName("Model")]
        public string Model { get; set; }
        [DisplayName("Description")]

}

在我的控制器中,我有这个:

[Authorize]
        public ActionResult ModelEdit(int id)
        {

            saveModel model = webService.getModel(id);

            return View(model);
        }

        [Authorize]
        [HttpPost]
        public ActionResult ModelEdit(saveModel model)
        {
            return View();
        }

鉴于我有这个:

@model MyApp.Models.saveModel

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    
    <div class="form-horizontal">
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.ModelID, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.ModelID, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.ModelID, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Area, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Area, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Area, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.AreaDescription, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.AreaDescription, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.AreaDescription, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Model, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Model, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Model, "", new { @class = "text-danger" })
            </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>
}

我遇到的问题是当我按下保存按钮时,它会转到正确的方法,但是我的 saveModel 在提交时是空的......我做错了什么?

标签: c#asp.net

解决方案


尝试包括 from body 属性,如下所示

[Authorize]
[HttpPost]
public ActionResult ModelEdit([FromBody]saveModel model)
{
    return View();
}

如果它不能解决您的问题,则将无效或空值传递到您的 ID 字段。您可以检查 ModelState 属性,该属性将指向正确的字段。


推荐阅读