Jonathan
Jonathan

Reputation: 651

Perform a get action with asp.net core 2.1

I'm working on asp.net core 2.1

Now we have views with classes like:

enter image description here

Each view have their own class, my class have OnGet() method, so I think this is a Get request when page load. But I put breakpoint into that method and never hits.

I want to do a simple get to do a SelectList into my view like:

public void OnGet()
        {

            var roles = _roleManager.Roles.ToList();

            List<SelectListItem> item = roles.ConvertAll(a =>
            {
                return new SelectListItem()
                {
                    Text = a.Id.ToString(),
                    Value = a.Name.ToString(),
                };
            }).ToList();

            var vm = new ApplicationRoleViewModel();

            vm.RolesToUser = roles
                         .Select(a => new SelectListItem()
                         {
                             Value = a.Id.ToString(),
                             Text = a.Name
                         })
                         .ToList();


            return ;
        }

View:

 <select asp-for="RoleId" asp-items="@Model.RolesToUser">
        <option>Please select one</option>
    </select>

But it always throw null because it never returns results from OnGet(), my question is. How can I do a simple get action in this new structure of views! Regards Full Code:

AssignRoleToUser.cshtml.cs

namespace Security.WebUi.Pages
{
    public class AssignRoleToUserModel : PageModel
    {

        private readonly RoleManager<ApplicationRole> _roleManager;

        public AssignRoleToUserModel(
             RoleManager<ApplicationRole> roleManager
            )
        {
            _roleManager = roleManager;
        }


        public void OnGet()
        {

            var roles = _roleManager.Roles.ToList();

            List<SelectListItem> item = roles.ConvertAll(a =>
            {
                return new SelectListItem()
                {
                    Text = a.Id.ToString(),
                    Value = a.Name.ToString(),
                };
            }).ToList();

            var vm = new ApplicationRoleViewModel();

            vm.RolesToUser = roles
                         .Select(a => new SelectListItem()
                         {
                             Value = a.Id.ToString(),
                             Text = a.Name
                         })
                         .ToList();


            return ;
        }


    }
}

View:

@page
@model Security.Dto.ViewModels.ApplicationRoleViewModel


    <h4>Assign role to user</h4>
    <hr />
    <div asp-validation-summary="All" class="text-danger"></div>
    <select asp-for="RoleId" asp-items="@Model.RolesToUser">
        <option>Please select one</option>
    </select>

    <br />


@section Scripts {

}

ApplicationRoleViewModel:

namespace Security.Dto.ViewModels
{
    public class ApplicationRoleViewModel
    {
        public Guid RoleId { get; set; }
        public List<SelectListItem> RolesToUser { get; set; }
    }
}

Upvotes: 1

Views: 166

Answers (1)

Chris Pratt
Chris Pratt

Reputation: 239300

You're using a Razor Page, apparently. With Razor Pages, the model for the view is the PageModel itself, i.e. your codebehind file. In your OnGet action you're newing up some random class, and setting a property on that, but then it just gets garbage collected. Instead, you should have this RolesToUser property on your PageModel itself, and set that:

public IEnumerable<SelectListItem> RolesToUser { get; set; }

...

public void OnGet()
{
    var roles = _roleManager.Roles.ToList();

    RolesToUser = roles.Select(a => new SelectListItem()
    {
        Value = a.Id.ToString(),
        Text = a.Name
    }).ToList();
}

Upvotes: 3

Related Questions