使用ViewComponent从RazorPage获取请求

时间:2019-04-11 07:55:04

标签: asp.net-mvc asp.net-core-mvc razor-pages asp.net-core-viewcomponent

我有条件地在Razor页面中使用ViewComponent, 并且每个Viewcomponent都是独立的, 我的剃须刀页面是“ /Subfolder/Index.cshtml”

<div class="row">
<div class="col-md-4">
    @await Component.InvokeAsync("RightMenu")
</div>
<div class="col-md-8">
    @if (Model.SIndex != 0)
    {
        @await Component.InvokeAsync("SubContent", new { id = Model.SIndex })

    }

    @if (Model.SIndex == 15)
    {
        <div class="row">
            <div class="col-md-12">
                <form method="post">
                    @await Component.InvokeAsync("QuestionUs", new { askLibrarian = new Lib.Model.AskLibrarian() })
                    <div class="form-group">
                        <input type="submit" value="ask Question" class="btn btn-default" asp-page-handler="question" />
                    </div>
                </form>
            </div>
        </div>
    }


</div>

后面的代码是“ /subfolder/index.cshtml.cs”

 public class IndexModel : PageModel
{
    private readonly Lib.Model.LibContext _context;
    [BindProperty]
    public int SIndex { get; set; }
    public async Task OnGet(int Id)
    {
            SIndex = Id;
    }
    [BindProperty]
    public AskLibrarian AskLibrarian { get; set; }

    public async Task<IActionResult> OnPostquestionAsync()
    {
        if (!ModelState.IsValid)
        {
            return Page();
        }

        _context.AskLibrarians.Add(AskLibrarian);
        await _context.SaveChangesAsync();

        return RedirectToPage("./Index");
    }
}

现在,“ questionViewcomponent”是一种显示许多输入元素的简单形式 在“ /subfolder/component/questionus/default.cshtml”中

@model Lib.Model.AskLibrarian

        <div asp-validation-summary="ModelOnly" class="text-danger"></div>
        <div class="form-row">
            <div class="form-group col-md-6">
                <label asp-for="FullName" class="control-label"></label>
                <div class="input-group mb-2 mr-sm-2">
                    <div class="input-group-prepend">
                        <div class="input-group-text"><i class="fas fa-user"></i></div>
                    </div>
                    <input asp-for="FullName" class="form-control" />
                    <span asp-validation-for="FullName" class="text-danger"></span>
                </div>
            </div>
            <div class="form-group col-md-6">
                <label asp-for="Email" class="control-label"></label>
                <div class="input-group mb-2 mr-sm-2">
                    <div class="input-group-prepend">
                        <div class="input-group-text"><i class="fas fa-envelope"></i></div>
                    </div>
                    <input asp-for="Email" class="form-control" />
                    <span asp-validation-for="Email" class="text-danger"></span>
                </div>
            </div>
        </div>
        <div class="form-row">
            <div class="form-group col-md-4">
                <label asp-for="LibraryNameId" class="control-label"></label>
                <div class="input-group mb-2 mr-sm-2">
                    <div class="input-group-prepend">
                        <div class="input-group-text"><i class="fas fa-book"></i></div>
                    </div>
                    <select asp-for="LibraryNameId" class="form-control" asp-items="ViewBag.LibraryNameId"></select>
                </div>
            </div>
            <div class="form-group col-md-8">
                <label asp-for="Subject" class="control-label"></label>
                <div class="input-group mb-2 mr-sm-2">
                    <div class="input-group-prepend">
                        <div class="input-group-text"><i class="fas fa-book-reader"></i></div>
                    </div>
                    <input asp-for="Subject" class="form-control" />
                    <span asp-validation-for="Subject" class="text-danger"></span>
                </div>
            </div>

        </div>
        <div class="form-group">
            <label asp-for="Text" class="control-label"></label>
            <textarea asp-for="Text" class="form-control" style="min-height:250px;"></textarea>
            <span asp-validation-for="Text" class="text-danger"></span>
        </div>

在“ OnpostQuestionAsync”上设置断点,当我单击带有“问题”处理程序的提交按钮时,不执行任何操作,而是向我显示空白页而不是问题表格。 我该怎么解决

1 个答案:

答案 0 :(得分:0)

很久以后,我的问题通过删除解决了

 <input type="submit" value="ask Question" class="btn btn-default" asp-page-handler="question" />

并添加表单标签

<form method="post" sp-page-handler="question">