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

cshtml页面@符号使用错误无法渲染 POST提交按钮开发问题求助

错误原因
  1. 注释格式错误:Razor模板混合C#和HTML上下文时,不能直接使用C#的//单行注释,否则会被识别为普通文本导致语法解析失败,Razor专属注释需使用@* 注释内容 *@格式
  2. 上下文切换错误:写完HTML闭合标签后,Razor默认处于HTML解析上下文,后续的using代码块属于C#逻辑,需要添加@前缀标记为C#代码
  3. 辅助方法调用错误:Html.TextBoxFor返回的HTML字符串需要加@前缀才会输出到页面,且末尾不能加额外分号,否则分号会被作为普通文本输出到页面
修复后的代码
@if (identity.IsInRole("AdminAFM") || identity.IsInRole("ResponsabilAFM"))
{
    if (Model.ValidationMessages.Contains("VIN") == true)
    {
        <div class="form-group">
            <div class="col-xs-6">
                <a class="btn btn-lg btn-block btn-default faa-parent animated-hover" href="@Url.Action("DownloadFile", "InvoiceValidation", new { fileName = Model.FileInfoGuid })">
                    <i class="fa fa-download faa-tada"></i>&nbsp;Descarcare fisier
                </a>
            </div>

            <div class="col-xs-6">
                <a id="ValidareManuala" class="btn btn-lg btn-block btn-success animated-hover" href="@Url.Action("ValidateInvoiceManual", "InvoiceValidation", new { ValidationID = Model.InvoiceValidationID })">
                    <i class="fa fa-check"></i>&nbsp;Validare manuala
                </a>
            </div>
        </div>

        @* 问题区域修复后 *@
        @using (Html.BeginForm("ValidateInvoiceManualWithoutRequestNumber", "InvoiceValidation", FormMethod.Post))
        {
            @Html.TextBoxFor(m => m.RequestName)
            <button type="submit">Setare Numar inregistrare</button>
        }
        @* 问题区域结束 *@
    }
}
补充说明

你当前的Controller代码没有接收和使用提交的RequestName字段,需要补充存储过程的参数赋值逻辑,否则提交的表单数据不会生效:

[HttpPost]
public ActionResult ValidateInvoiceManualWithoutRequestNumber(InvoiceModel model) 
{
    using (var conn = new SqlConnection(connectionString))
    using (var command = new SqlCommand("ProcedureName", conn) { CommandType = CommandType.StoredProcedure }) 
    {
        // 补充参数赋值逻辑示例
        command.Parameters.AddWithValue("@RequestName", model.RequestName);
        conn.Open();
        command.ExecuteNonQuery();
    }

    return RedirectToAction("Index");
}

内容的提问来源于stack exchange,提问作者Dan-Robert Samoila

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.25 21:15:01