c# - 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 在提交时是空的......我做错了什么?
解决方案
尝试包括 from body 属性,如下所示
[Authorize]
[HttpPost]
public ActionResult ModelEdit([FromBody]saveModel model)
{
return View();
}
如果它不能解决您的问题,则将无效或空值传递到您的 ID 字段。您可以检查 ModelState 属性,该属性将指向正确的字段。
推荐阅读
- node.js - NodeJS 中的像素级图像处理
- javascript - 带有下拉菜单的引导选项卡已损坏
- python - 如何通过 Python 的 CalDav 更新事件
- python-3.x - paperspace dask.distributed Worker 无法启动
- amazon-web-services - AWS 置放群组中的单流和多流
- ios - swift:如何在为 aws-api-gateway 生成的 iOS sdk 中的 PUT 请求中设置正文?
- blockchain - 我如何将用于在 ERC20 中购买代币的以太币退回?
- amazon-web-services - 如何让我的 AWS Bitnami Magento 部署以使用 https 加载资产?
- javascript - 在 Safari 中使用 FileSaver.js 会损坏下载
- amazon-web-services - AWS API 网关和弹性搜索获取查询