Hyperwave
Hyperwave

Reputation: 133

Razor Pages OnPost - Can the parent page use the same partial model that is passed to its partial on creation?

I have looked at several suggested posts and I've been searching for similar issues without finding anything with this particular issue.

I have a Razor Page that use a few partials.

       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);
            }
        }

The TaskModelCreate object (the partial page's model):

        [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;
        }

I have verified that the partials model is the one that is created in the TasksModel constructor, by writing UserId to the page.

However, when submitting a new task from the view, the specified OnPost method always receives a TaskModelCreate object where the UserId property is 0 (my UserId during testing is 1) - which makes me believe that the form creates a new TaskModelCreate when it posts.

The Name and Description properties are filled as expected.

Is there a way to specify that the posted object should be the one it is given, rather than a new one?

I'm using Asp.Net Core Razor Pages (the non-MVC version).

Edit:

I was asked to share the views as well, so here they are.

The parent view:

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

My partial view:

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

Upvotes: 0

Views: 697

Answers (1)

Yiyi You
Yiyi You

Reputation: 18209

You can add <input asp-for="UserId" hidden /> to your form in partial view,so that when post,it will post UserId,here is a demo: Parent View(Tasks):

@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(add <input asp-for="UserId" hidden /> to form,if you want to pass IsCompleted,you can also add <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;
        }
    }

result: enter image description here

Upvotes: 0

Related Questions