You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET MVC点击审批按钮如何传递ResignationRequester模型属性值

问题:ASP.NET MVC提交表单时模型属性值无法传递到控制器动作方法

问题描述

开发ASP.NET MVC应用程序时,点击“审批”按钮无法将ResignationRequester模型的属性值传递到ApprovalIndex动作方法。页面加载时RequestNo、EmpID、EmpName等属性均正常显示,填写SpeakStuffComment评论后点击审批按钮,期望基于RequestNo保存该评论,但调试时发现模型所有属性值均为null。

原因分析

  • 表单范围错误:当前表单仅包含AntiForgeryToken和提交按钮,其他模型属性的显示元素、SpeakStuffComment的编辑器都在表单外部,提交时这些值不会被包含在请求参数中。
  • 缺少隐藏域传递静态属性:RequestNo、EmpID、EmpName仅通过@Model.XXX直接展示,没有生成表单控件(如隐藏域),服务器无法获取这些值。

解决方案

  • 将所有需要提交的内容(包括静态属性的隐藏域、评论编辑器)包裹在表单内部。
  • 使用@Html.HiddenFor()为RequestNo、EmpID、EmpName生成隐藏域,确保这些值随表单提交到服务器。

修改后的视图代码

@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
                                <!-- 添加隐藏域传递RequestNo -->
                                @Html.HiddenFor(model => 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
                            <!-- 添加隐藏域传递EmpName -->
                            @Html.HiddenFor(model => 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
                            <!-- 添加隐藏域传递EmpID -->
                            @Html.HiddenFor(model => 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>

说明

  • 模型类ResignationRequester和控制器ApprovalIndex动作方法无需修改,仅调整视图即可解决问题。
  • 提交表单时,隐藏域会自动将RequestNo、EmpID、EmpName的值传递到服务器,结合SpeakStuffComment的输入值,模型绑定就能正常工作。

内容的提问来源于stack exchange,提问作者ahmed abdelaziz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 04:03:10