【问题标题】:Losing Model after posting ajax form MVC Razor Pages发布 ajax 表单 MVC Razor Pages 后丢失模型
【发布时间】:2020-06-30 16:24:35
【问题描述】:

我对理解 Razor 页面还很陌生,并且在发布表单后检索模型数据时遇到问题。它能够在页面加载时检索数据,但是一旦我发布表单,模型就为空。这是一个大公司项目的一部分,所以我很难发布所有数据,但这是代码的相关sn-p:

@inherits ViewPage<ViewModels.User.SignUp.VerifyActivationCodeModel>

@using (Ajax.BeginForm(nameof(SignUpController.VerifyActivationCode),
    SignUpController.ControllerName, null, new AjaxOptions
    {
        HttpMethod = "POST",
        OnSuccess = "VerifyActivationCodePage.OnSuccess",
        OnFailure = "ErrorHelper.HandleError"
    }, new { id = "verifyActivationCodeForm", autocomplete = "off" }))
{
    @Html.AntiForgeryToken()
    <div class="">
        <div class="">
            <h3 class="">
                @Html.Raw(Model.Title)
            </h3>
        </div>
    </div>
    <div class="">
        <div class="">
            <fieldset class="form-row">
                @Html.TextBoxSensitiveFor(m => m.ActivationCode,
                    new
                    {
                       placeholder = string.Format(ResourceManager.GetResource("VerifyToken"), Model.Token),
                       @class = "in-text",
                       maxlength = "100"
                    })

 // rest of form ...

当我发布此表单时,我尝试填充为占位符的 Model.Token 为“Null”,而在发布之前它已成功从资源中填充。

有人可以帮忙吗?谢谢

【问题讨论】:

  • 在表单中添加@Html.HiddenFor(m => m.Token),看看是否有帮助。
  • 成功了,非常感谢@KevinAmorim!
  • 不错!将其发布为答案:)

标签: c# asp.net-mvc razor


【解决方案1】:

为了将数据绑定回您的操作,您应该在表单中包含它们。

对于您不想在表单上显示的字段,您可以使用:

@Html.HiddenFor(m => m.FieldName)

这样它们将绑定回您的操作,但不会显示在您的表单上。

【讨论】:

    猜你喜欢
    • 2011-02-13
    • 1970-01-01
    • 2021-04-10
    • 2020-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多