简体   繁体   中英

Losing Model after posting ajax form MVC Razor Pages

I'm pretty new to understanding Razor Pages and I am having an issue with retrieving model data after a form has been posted. It is able to retrieve the data on page load but as soon as I post the form, the model is null. This is a part of a large company project so it's difficult for me to post all of the data, but here's the relevant snippet of the code:

@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 ...

When I post this form, the Model.Token that I am trying to populate as a placeholder is 'Null', whereas before posting it successfully populates from Resources.

Is anyone able to help? Thanks

In order to bind the data back to your action you should have them in your form.

For fields you don't want to show on your form you can use:

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

This way they will bind back to your action, but will not show on your form.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM