如何解决Razor Pages OnPost-父页面可以使用与创建时传递给它的部分相同的部分模型吗?
我查看了一些建议的帖子,并且一直在寻找类似的问题,但没有找到与此特定问题相关的任何内容。
我有一个Razor页面,其中使用了一些部分。
public class TasksModel : PageModel
{
[BindProperty]
public TaskModelCreate Create { get; set; }
public TasksModel(ITaskService taskService,IHttpContextAccessor httpContextAccessor)
{
_taskService = taskService;
_userId = GetUserId(httpContextAccessor);
Create = new TaskModelCreate(_userId);
}
public async Task OnPostCreate()
{
// What I would like to do:
// (Using this parameter: TaskModelCreate taskModel)
await _taskService.createuserTask(taskModel.UserTask);
// What I do Now,which works but means that I will have to have a "GetX" method on a lot
// of things that just return a property:
var task = await Create.GetTask(_userId);
await _taskService.createuserTask(task);
}
}
TaskModelCreate对象(部分页面的模型):
[BindProperty]
public string Name { get; set; }
[BindProperty]
public string Description { get; set; }
public bool IsCompleted { get; set; }
public UserTask UserTask => new UserTask(UserId,Name,Description);
public int UserId { get; set; } // For testing
public TaskModelCreate(int userId)
{
UserId = userId;
}
通过将UserId写入页面,我已经验证了partials模型是在TasksModel构造函数中创建的模型。
但是,从视图提交新任务时,指定的OnPost方法始终会收到一个TaskModelCreate对象,该对象的UserId属性为0(测试期间我的UserId为1),这使我相信表单在创建时会创建一个新的TaskModelCreate它发布。
是否有一种方法可以指定发布的对象应该是给定的对象,而不是新的对象?
我正在使用Asp.Net Core Razor Pages(非MVC版本)。
编辑:
父视图:
@model MyApp.UserInterface.Pages.TasksModel
@{
Layout = "_DashboardLayout";
ViewData["Title"] = "Tasks";
}
<div class="container-fluid">
<ul class="nav nav-tabs justify-content-start" id="taskTabs" role="tablist">
<li class="nav-item">
<a class="nav-link active" id="tasksActive-tab" data-toggle="tab" role="tab" href="#tasksActive" aria-controls="Active Tasks" aria-selected="true">Active Tasks</a>
</li>
<li class="nav-item">
<a class="nav-link" id="tasksCreate-tab" data-toggle="tab" role="tab" href="#tasksCreate" aria-controls="Create Task" aria-selected="false">Create Task +</a>
</li>
<li class="nav-item">
<a class="nav-link" id="tasksstats-tab" data-toggle="tab" href="#tasksstats" aria-controls="Statistics" aria-selected="false">Your Stats</a>
</li>
<li class="nav-item">
<a class="nav-link" id="tasksArchive-tab" data-toggle="tab" href="#tasksArchive" role="tab" aria-controls="Archive" aria-selected="false">Archive</a>
</li>
</ul>
<div class="tab-content" id="myTabContent">
<div class="tab-pane fade show active" id="tasksActive" role="tabpanel" aria-labelledby="tasksActive-tab">
<partial name="TaskPartials/_active" model="Model.Active" view-data="@ViewData"/>
</div>
<div class="tab-pane fade" id="tasksCreate" role="tabpanel" aria-labelledby="tasksCreate-tab">
<partial name="TaskPartials/_create" model="Model.Create" view-data="@ViewData"/>
</div>
<div class="tab-pane fade" id="tasksstats" role="tabpanel" aria-labelledby="tasksstats-tab">
<partial name="TaskPartials/_stats" model="Model.Stats" view-data="@ViewData"/>
</div>
<div class="tab-pane fade" id="tasksArchive" role="tabpanel" aria-labelledby="tasksArchive-tab">
<partial name="TaskPartials/_archive" model="Model.Archive" view-data="@ViewData"/>
</div>
</div>
我的局部视图:
@model Model.TaskModelCreate
@{ ViewData.TemplateInfo.HtmlFieldPrefix = "Create";
}
<h1> User: @Model.UserId</h1>
<form method="post" asp-page-handler="create">
<div class="container">
<div class="form-group">
<div class="input-group mb-sm-3">
<div class="input-group-prepend">
<span class="input-group-text" id="taskName" style="width: 6rem;">Name</span>
</div>
<input type="text" asp-for="Name" class="form-control" placeholder="Do something.."
aria-label="TaskName" aria-describedby="taskName" name="Name" />
</div>
<div class="input-group mb-sm-3">
<div class="input-group-prepend">
<span class="input-group-text" id="taskDescription" style="width: 6rem;">Description</span>
</div>
<input type="text" asp-for="Description" class="form-control" placeholder="I want to do X because Y." aria-label="TaskDescription"
aria-describedby="taskDescription" name="Description" />
</div>
<div class="input-group-append">
<button type="submit" class="btn btn-outline-primary" id="taskCreateButton">
Create
</button>
</div>
</div>
</div>
</form>
解决方法
您可以在部分视图中将<input asp-for="UserId" hidden />
添加到表单中,以便在发布时将发布UserId,这是一个演示:
父视图(任务):
@page
@model RazorPage1.Pages.Test.TasksModel
<h1>Tasks</h1>
<partial name="_Partial1" model="@Model.Create" />
Tasks.cshtml.cs:
public class TasksModel : PageModel
{
[BindProperty]
public TaskModelCreate Create { get; set; }
public IActionResult OnGet() {
Create = new TaskModelCreate { UserId = 1,Name = "create",Description = "ssss",IsCompleted = true };
return Page();
}
public async Task OnPostCreate()
{
int userId = Create.UserId;
}
}
_Partial1(将<input asp-for="UserId" hidden />
添加到表单中,如果要传递IsCompleted,也可以添加<input asp-for="IsCompleted" hidden />
):
@model TaskModelCreate
<h1> User: @Model.UserId</h1>
<form method="post" asp-page-handler="Create">
<input asp-for="UserId" hidden />
<input asp-for="IsCompleted" hidden />
<div class="container">
<div class="form-group">
<div class="input-group mb-sm-3">
<div class="input-group-prepend">
<span class="input-group-text" id="taskName" style="width: 6rem;">Name</span>
</div>
<input type="text" asp-for="Name" class="form-control" placeholder="Do something.."
aria-label="TaskName" aria-describedby="taskName" name="Name" />
</div>
<div class="input-group mb-sm-3">
<div class="input-group-prepend">
<span class="input-group-text" id="taskDescription" style="width: 6rem;">Description</span>
</div>
<input type="text" asp-for="Description" class="form-control" placeholder="I want to do X because Y." aria-label="TaskDescription"
aria-describedby="taskDescription" name="Description" />
</div>
<div class="input-group-append">
<button type="submit" class="btn btn-outline-primary" id="taskCreateButton">
Create
</button>
</div>
</div>
</div>
</form>
TaskModelCreate:
public class TaskModelCreate
{
[BindProperty]
public string Name { get; set; }
[BindProperty]
public string Description { get; set; }
public bool IsCompleted { get; set; }
//public UserTask UserTask => new UserTask(UserId,Name,Description);
public int UserId { get; set; } // For testing
public TaskModelCreate()
{
}
public TaskModelCreate(int userId)
{
UserId = userId;
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。