提问人:Manatchanok Bumrungban 提问时间:10/31/2023 最后编辑:marc_sManatchanok Bumrungban 更新时间:11/2/2023 访问量:52
List 在从视图传递到控制器时返回 null 模型
List returns null model when passing from view to controller
问:
我的模型在发送回控制器时始终为 null。我尝试了很多方法来解决它,但没有任何效果。提交按钮后,我检查了网络分流器,发现所有值都是正确的,但在控制器中,模型仍然是空的。
型
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace OTWebApplication.Models
{
public class AP_UserForm
{
public String UFID { get; set; }
public String UF_YID { get; set; }
public String UF_FormID { get; set;}
public String UF_FormName { get; set; }
public String UF_EvaID { get; set; } // คนประเมิน
public String UF_EvaName { get; set; }
public String UF_RootID { get; set; } // พนักงาน
public String UF_RootName { get; set; }
public String UF_Available { get; set; } //ให้ประเมิน หรือไม่ต้องประเมินพนักงานคนนี้
public String UF_STID { get; set; } // สถานะใบประเมิน
}
public class AP_UserFormL
{
public List<AP_UserForm> AP_UserForms { get; set; }
public AP_UserFormL()
{
AP_UserForms = new List<AP_UserForm>();
}
}
}
[HTML全文]
@model OTWebApplication.Models.AP_UserFormL
<form asp-action="ManageAP_usershow" method="post">
<table class="table">
<thead>
<tr>
<th>EmployeeID</th>
<th>EmployeeName</th>
<th>EvaluatorID</th>
<th>FormName</th>
<th></th>
</tr>
</thead>
<tbody>
@if (Model != null)
{
@for (var i = 0; i < Model.AP_UserForms.Count(); i++)
{
<tr>
<td>
<input type="hidden" asp-for="@Model.AP_UserForms[i].UFID" class="form-control" />
<input type="hidden" asp-for="@Model.AP_UserForms[i].UF_YID" class="form-control" />
<input type="hidden" asp-for="@Model.AP_UserForms[i].UF_STID" class="form-control" />
<input asp-for="@Model.AP_UserForms[i].UF_RootID" class="form-control" />
</td>
<td>
<input asp-for="@Model.AP_UserForms[i].UF_RootName" class="form-control" />
</td>
<td>
<select asp-for="@Model.AP_UserForms[i].UF_EvaID" class="form-control">
@foreach (var item in ViewBag.ARoot)
{
<option value="@item.ARoot_ID">@item.ARoot_Name</option>
}
<option value="@Model.AP_UserForms[i].UF_EvaID">@Model.AP_UserForms[i].UF_EvaName</option>
</select>
</td>
<td>
<select asp-for="@Model.AP_UserForms[i].UF_FormID" class="form-control">
@foreach (var item in ViewBag.Form)
{
<option value="@item.FormID">@item.FormName</option>
}
<option value="@Model.AP_UserForms[i].UF_FormID">@Model.AP_UserForms[i].UF_FormName</option>
</select>
</td>
<td>
<select asp-for="@Model.AP_UserForms[i].UF_Available" class="form-control">
<option value="0">ไม่ต้องประเมิน</option>
<option value="1">ประเมินปกติ</option>
</select>
</td>
</tr>
}
}
</tbody>
</table>
<button input type="submit"value="Edit">Update</button>
</Form>
这是控制器,用于显示数据,一切正常,但对于方法,它的参数总是为 null:Post
public IActionResult ManageAP_usershow(String id)
{
AP_UserFormL ShowAPList = ManageAP_usershowList(id);
ViewData["ARoot"] = GetARoot();
ViewData["Form"] = GetForm();
return View(ShowAPList);
}
[HttpPost]
public IActionResult ManageAP_usershow([FromBody] AP_UserFormL model)
{
foreach(var item in model.AP_UserForms)
{
if (ModelState.IsValid)
{
Console.WriteLine("pass");
}
}
return RedirectToAction("ManageAP", "Appraisal");
}
答:
1赞
brett_0267
10/31/2023
#1
尝试从 post 方法中的参数中删除标签。该标记指示从预期内容类型中的请求正文中获取值,但您正在发送表单数据。请改为尝试,它需要内容类型。[FromBody]
[FromBody]
application/json
[FromForm]
application/x-www-form-urlencoded
[FromQuery] - Gets values from the query string.
[FromRoute] - Gets values from route data.
[FromForm] - Gets values from posted form fields.
[FromBody] - Gets values from the request body.
[FromHeader] - Gets values from HTTP headers.
来源:https://learn.microsoft.com/en-us/aspnet/core/mvc/models/model-binding?view=aspnetcore-7.0
评论
0赞
Manatchanok Bumrungban
10/31/2023
谢谢你的回答。我已经更改了,但它仍然传递了 null 模型:(。
0赞
Manatchanok Bumrungban
10/31/2023
我尝试了modelState.isValid,它发回了模型错误。你能告诉我下一步需要做什么吗?
1赞
Merter GÜLBAHAR
10/31/2023
#2
你试过这种方式吗?
[HttpPost]
public IActionResult ManageAP_usershow([FromBody] AP_UserForm model)
{
//...
}
评论
0赞
Manatchanok Bumrungban
11/2/2023
是的,我已经尝试过了,但仍然无效:(谢谢你的帮助。你还有别的办法吗?
1赞
Manatchanok Bumrungban
11/2/2023
#3
我已经找到了根本原因。 查看已发送到控制器的列表,但由于数据过多。
失败:OTWebApplication.Controllers.AppraisalController[0] ModelState 错误:无法读取请求表单。超过表单值计数限制 1024。
我确实将此代码放在程序 .cs 上
fail: OTWebApplication.Controllers.AppraisalController[0]
ModelState error for : Failed to read the request form. Form value count limit 1024 exceeded.
现在控制器已经可以接收到列表了。 感谢所有的帮助。谢谢!
评论