模型不绑定 _Footer.cshtml 部分视图 _Layout.cshtml 页面中的 razor 页面

Model does not bind from _Footer.cshtml partial view in _Layout.cshtml page in razor pages

我想在我的网页页脚实现新闻通讯订阅功能。

订阅窗口必须在所有页面上可见。出于这个原因,我将它作为局部视图集成到我的 _Layout 中。

_Layout.cshtml 文件是:

<body id="top-header">

    <!-- preloader start -->
    <div class="preloader">
        <div class="spin"></div>
    </div>
    <!-- preloader end -->
    <!-- Header Start -->
    @await Html.PartialAsync("_Header")
    <!-- Header Close -->

    @RenderBody()

    <!-- Footer Start -->
    <partial name="_Footer" model="new SubscribeNewsletterPageModel()" />
    <!-- Footer Close -->
    <!--  Page Scroll to Top  -->
    <a class="scroll-to-top js-scroll-trigger" href="#top-header">
        <i class="fa fa-angle-up"></i>
    </a>
    <!--  Page Scroll to Top  -->

    <script src="~/lib/jquery/dist/jquery.min.js"></script>
    <script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
    <script src="~/js/site.min.js" asp-append-version="true"></script>

    <!-- Main Script -->
    <script src="~/js/script.min.js"></script>

    @await RenderSectionAsync("Scripts", required: false)
    
</body>

我创建了一个 SubscribeNewsletterPageModel.cs class,其中包含我想用于 post 订阅者电子邮件地址的模型:

public class SubscribeNewsletterPageModel : PageModel
{
    

    [BindProperty]
    public Newsletter newsletter { get; set; }


    public async Task<IActionResult> OnPostSubscribeNewsletter()
    {

        TempData["returnurl"] = Request.Headers["Referer"].ToString();
        var apiKey = "xxxxx";
        string bearer = "Bearer " + apiKey;
        string newsletterForJSON = newsletter.EMail;

        // queryParams added only for testing
        // in production newsletterForJSON must be used
        string queryParams = @"{
                                  'contacts': [
                                    {
                                      'email': 'firstname.lastname@gmail.com',
                                    }
                                  ]
                                  }";

        JObject queryParamsJSON = JObject.Parse(queryParams);

        var client = new RestClient("https://api.sendgrid.com/v3/marketing/contacts");
        var request = new RestRequest(Method.PUT);
        request.AddHeader("content-type", "application/json");
        request.AddHeader("authorization", bearer);
        request.AddParameter("application/json", queryParamsJSON, ParameterType.RequestBody);
        IRestResponse response = await client.ExecuteAsync(request);
        var error = response.Content;

        return Redirect(TempData["returnurl"].ToString());

    }

}

在我的 _Footer.cshtml 上,它是一个 Razor View,我从我的模型开始:

@model SubscribeNewsletterPageModel

然后我有我的表格:

<form asp-page-handler="SubscribeNewsletter" method="post">
<div class="position-relative">
    <input asp-for="@Model.newsletter.EMail" class="form-control border-0 text-white newsletter-form" id="newsletter" name="newsletter" placeholder="Geben Sie Ihre E-Mail ein">
    <button type="submit" class="btn btn-primary btn-subscribe">Register</button>
</div>
</form> 

我的 Index Razor 页面继承自 SubscribeNewsletterPageModel

public class IndexModel : SubscribeNewsletterPageModel

如果我开始调试 (F5) 并在我的 OnPostSubscribeNewsletter 方法中设置断点,我会看到我的绑定 属性 是空的!

有什么想法吗?

If I start debugging (F5) and set a breakpoint in my OnPostSubscribeNewsletter method, I see that my bound property is EMPTY!

你的意思是邮箱没有绑定成功?

在您的 _Footer.cshtml 中,您可以尝试将 name="newsletter" 更改为 名称="newsletter.Email" 更改下面的代码

<input asp-for="@Model.newsletter.EMail" class="form-control border-0 text-white newsletter-form" id="newsletter" name="newsletter" placeholder="Geben Sie Ihre E-Mail ein">

进入

<input asp-for="@Model.newsletter.EMail" class="form-control border-0 text-white newsletter-form" id="newsletter" name="newsletter.Email" placeholder="Geben Sie Ihre E-Mail ein">

结果: