首页 > 解决方案 > 在 Razor 页面 Web 应用程序中发布异步而不重定向

问题描述

我正在尝试制作一个社交网络应用程序,用户可以在其中对帖子做出反应。但是每次有人做出反应时,我都需要能够将反应直接发布到模型上而无需重定向。

到目前为止,这是我的代码(带有重定向):

模型:

[BindProperty]
public Models.Table Table { get; set; }
public async Task<IActionResult> OnPostReactAsync(int t)
{
    var reaction = new Models.Table
    {
         Reaction = Table.Reaction,
         PostId = t,
         UserName = _userManager.GetUserName(User)
    };
    _context.Table.Add(reaction);
    await _context.SaveChangesAsync();
    return Redirect("./Index");
}

看法:

<form method="post" enctype="multipart/form-data">
    <div class="form-group">
        <input name="t" value="@item.ID" class="form-control" style="display:none;" />
    </div>
    <div class="form-group">
        <input asp-for="Table.Reaction" id="@item.ID x" type="radio" value="true" onclick="document.getElementById('cal').click()" />
        <label class="r good" for="@item.ID x"></label>
        <input asp-for="Table.Reaction" id="@item.ID y" type="radio" value="false" onclick="document.getElementById('cal').click()" />
        <label class="r bad" for="@item.ID y"></label>
    </div>
    <div class="form-group">
        <input type="submit" id="cal" asp-page-handler="React" value="submit" style="display:none;" class="btn btn-default" />
    </div>
</form>

标签: c#asp.netasp.net-corerazor-pages

解决方案


推荐阅读