提问人:ahmed abdelaziz 提问时间:10/10/2023 最后编辑:ahmed abdelaziz 更新时间:10/11/2023 访问量:51
单击批准按钮时,如何将所有带有值的模型属性传递给批准索引操作?
How to pass all model properties with values to Approve Index Action when click approve button?
问:
我正在开发一个 ASP.NET MVC 应用程序。我遇到了一个问题,即单击“批准”按钮时无法将模型属性值传递给操作方法。ResignationRequester
ApproveIndex
所有属性,例如和加载详细信息页面加载时,然后我写评论,然后单击“批准”按钮保存。Request no
employee no
SpeakStuffComment
SpeakStuffComment
request no
单击“批准”按钮时,如何将模型传递给操作方法?ResignationRequester
ApproveIndex
@model HR.WorkforceRequisition.Models.ResignationRequester
@{
ViewBag.Title = "Details";
Layout = "~/Views/Shared/_LayoutResignation.cshtml";
}
<div style="padding-top:10px;">
@if (!string.IsNullOrEmpty(ViewBag.msg))
{
<div class="alert alert-success">
@ViewBag.msg
</div>
}
@if (ViewBag.AllowApprove == true)
{
using (Html.BeginForm("ApprovalIndex", "Resignation", FormMethod.Post, htmlAttributes: new { @style = "display:inline;" }))
{
@Html.AntiForgeryToken()
<a onclick = "$(this).parents('form:first').submit();" class="btn btn-primary" style="min-width: 100px;
margin-left: 5px;"><i class="glyphicon glyphicon-ok"></i> Approve </a>
}
<table style="border: 1px solid black;width:100%;">
<tr>
<td class="hover" style="font-weight: bold; padding-top: 10px;">
<div class="form-group hover">
@Html.LabelFor(model => model.RequestNo, htmlAttributes: new { @class = "control-label col-md-5" })
<div class="col-md-7">
@Model.RequestNo
</div>
</div>
</td>
</tr>
<tr>
<td class="hover" style="width: 50%; font-weight: bold; padding-top: 10px;">
@Html.LabelFor(model => model.EmpName, htmlAttributes: new { @class = "control-label col-md-5" })
<div class="col-md-7">
@Model.EmpName
</div>
</td>
<td class="hover" style="font-weight: bold; padding-top: 10px;">
@Html.LabelFor(model => model.EmpID, htmlAttributes: new { @class = "control-label col-md-5" })
<div class="col-md-7">
@Model.EmpID
</div>
</td>
</tr>
</table>
<table style="border: 1px solid black;width:100%;">
<tr>
<td class="hover" style="font-weight: bold; padding-top: 10px;">
<div class="form-group hover">
@Html.Label("If yes, why did the staff resign? If No, Why? ", htmlAttributes: new { @class = "control-label col-md-5" })
<div class="col-md-7">
@Html.EditorFor(model => model.SpeakStuffComment, new
{
htmlAttributes = new
{ @class = "form-control" }
})
</div>
</div>
</td>
</tr>
</table>
</div>
法典:
public class ResignationRequester
{
[Display(Name = "Employee No : ")]
[Required,]
public int EmpID { get; set; }
[Display(Name = "Request No")]
public int RequestNo { get; set; }
[Required]
[Display(Name = "Emp. Name: ")]
public string EmpName { get; set; }
[DataType(DataType.MultilineText)]
public string SpeakStuffComment { get; set; }
}
批准索引操作,如下所示:
public class ResignationController : Controller
{
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> ApprovalIndex(ResignationRequester REQ)
{
return new EmptyResult();
}
}
调用 API 时,它命中操作结果断点,但值显示为 null
我在代码上的问题高于所有属性值,如显示 null - 请问如何解决这个问题?SpeakStuffComment and Request no
从调试断点开始,我检查了模型 Resignation Requester 的属性值,尽管它有数据,但它显示所有属性值都为 null。
那么什么是问题以及如何解决它呢?
答:
1赞
kkkristo
10/11/2023
#1
你能提供 ApproveIndex 获取和发布操作代码吗?
评论
0赞
ahmed abdelaziz
10/11/2023
操作结果 ApprovalIndex 如下: public class ResignationController : Controller { [ValidateAntiForgeryToken] public async Task<ActionResult> ApprovalIndex(int id) { return new EmptyResult();
0赞
kkkristo
10/11/2023
这是你行动的全部代码吗?您使用参数“id”,但没有代码使用它。据我了解,您的 POST 操作应该接收 Model 或 ViewModel 的对象作为参数来处理其值。您是否在浏览器中检查过开发工具上的“网络”?您的表单发送了什么内容?
0赞
ahmed abdelaziz
10/11/2023
是的,发布操作结果,我需要将所有请求者索引模型作为参数发送
0赞
ahmed abdelaziz
10/11/2023
所以你能帮我吗,如果你需要任何信息,我会给的
0赞
ahmed abdelaziz
10/11/2023
所以我请做些什么来解决这个问题
评论