Perform a get action with asp.net core 2.1










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;











share|improve this question
























  • Your asp-for="RoleId" I suppose RoleId is a property from a class. Must be the class like ` asp-for="Role"`.

    – Llazar
    Nov 13 '18 at 20:01











  • @CalC I update all my code

    – Jonathan
    Nov 13 '18 at 20:03











  • I update my question with full code @Llazar

    – Jonathan
    Nov 13 '18 at 20:04











  • What is the RoleId? asp-for needs a Model.

    – Llazar
    Nov 13 '18 at 20:06











  • RoleIdit's the value of viewmodel : ApplicationRoleViewModel @Llazar

    – Jonathan
    Nov 13 '18 at 20:08















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;











share|improve this question
























  • Your asp-for="RoleId" I suppose RoleId is a property from a class. Must be the class like ` asp-for="Role"`.

    – Llazar
    Nov 13 '18 at 20:01











  • @CalC I update all my code

    – Jonathan
    Nov 13 '18 at 20:03











  • I update my question with full code @Llazar

    – Jonathan
    Nov 13 '18 at 20:04











  • What is the RoleId? asp-for needs a Model.

    – Llazar
    Nov 13 '18 at 20:06











  • RoleIdit's the value of viewmodel : ApplicationRoleViewModel @Llazar

    – Jonathan
    Nov 13 '18 at 20:08













1












1








1


0






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;











share|improve this question
















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;








c# asp.net asp.net-core .net-core asp.net-core-2.1






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 20:03







Jonathan

















asked Nov 13 '18 at 19:54









JonathanJonathan

3248




3248












  • Your asp-for="RoleId" I suppose RoleId is a property from a class. Must be the class like ` asp-for="Role"`.

    – Llazar
    Nov 13 '18 at 20:01











  • @CalC I update all my code

    – Jonathan
    Nov 13 '18 at 20:03











  • I update my question with full code @Llazar

    – Jonathan
    Nov 13 '18 at 20:04











  • What is the RoleId? asp-for needs a Model.

    – Llazar
    Nov 13 '18 at 20:06











  • RoleIdit's the value of viewmodel : ApplicationRoleViewModel @Llazar

    – Jonathan
    Nov 13 '18 at 20:08

















  • Your asp-for="RoleId" I suppose RoleId is a property from a class. Must be the class like ` asp-for="Role"`.

    – Llazar
    Nov 13 '18 at 20:01











  • @CalC I update all my code

    – Jonathan
    Nov 13 '18 at 20:03











  • I update my question with full code @Llazar

    – Jonathan
    Nov 13 '18 at 20:04











  • What is the RoleId? asp-for needs a Model.

    – Llazar
    Nov 13 '18 at 20:06











  • RoleIdit's the value of viewmodel : ApplicationRoleViewModel @Llazar

    – Jonathan
    Nov 13 '18 at 20:08
















Your asp-for="RoleId" I suppose RoleId is a property from a class. Must be the class like ` asp-for="Role"`.

– Llazar
Nov 13 '18 at 20:01





Your asp-for="RoleId" I suppose RoleId is a property from a class. Must be the class like ` asp-for="Role"`.

– Llazar
Nov 13 '18 at 20:01













@CalC I update all my code

– Jonathan
Nov 13 '18 at 20:03





@CalC I update all my code

– Jonathan
Nov 13 '18 at 20:03













I update my question with full code @Llazar

– Jonathan
Nov 13 '18 at 20:04





I update my question with full code @Llazar

– Jonathan
Nov 13 '18 at 20:04













What is the RoleId? asp-for needs a Model.

– Llazar
Nov 13 '18 at 20:06





What is the RoleId? asp-for needs a Model.

– Llazar
Nov 13 '18 at 20:06













RoleIdit's the value of viewmodel : ApplicationRoleViewModel @Llazar

– Jonathan
Nov 13 '18 at 20:08





RoleIdit's the value of viewmodel : ApplicationRoleViewModel @Llazar

– Jonathan
Nov 13 '18 at 20:08












1 Answer
1






active

oldest

votes


















3














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






share|improve this answer























  • But when page load it never hit OnGet method, any idea why that happen?

    – Jonathan
    Nov 13 '18 at 20:12











  • You're loading a different page, then. Check your URL/routes. OnGet is the default for a Razor Page, so if that's never getting called, you're not hitting that page at all.

    – Chris Pratt
    Nov 13 '18 at 20:13










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53288542%2fperform-a-get-action-with-asp-net-core-2-1%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














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






share|improve this answer























  • But when page load it never hit OnGet method, any idea why that happen?

    – Jonathan
    Nov 13 '18 at 20:12











  • You're loading a different page, then. Check your URL/routes. OnGet is the default for a Razor Page, so if that's never getting called, you're not hitting that page at all.

    – Chris Pratt
    Nov 13 '18 at 20:13















3














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






share|improve this answer























  • But when page load it never hit OnGet method, any idea why that happen?

    – Jonathan
    Nov 13 '18 at 20:12











  • You're loading a different page, then. Check your URL/routes. OnGet is the default for a Razor Page, so if that's never getting called, you're not hitting that page at all.

    – Chris Pratt
    Nov 13 '18 at 20:13













3












3








3







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






share|improve this answer













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







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 '18 at 20:02









Chris PrattChris Pratt

155k20238304




155k20238304












  • But when page load it never hit OnGet method, any idea why that happen?

    – Jonathan
    Nov 13 '18 at 20:12











  • You're loading a different page, then. Check your URL/routes. OnGet is the default for a Razor Page, so if that's never getting called, you're not hitting that page at all.

    – Chris Pratt
    Nov 13 '18 at 20:13

















  • But when page load it never hit OnGet method, any idea why that happen?

    – Jonathan
    Nov 13 '18 at 20:12











  • You're loading a different page, then. Check your URL/routes. OnGet is the default for a Razor Page, so if that's never getting called, you're not hitting that page at all.

    – Chris Pratt
    Nov 13 '18 at 20:13
















But when page load it never hit OnGet method, any idea why that happen?

– Jonathan
Nov 13 '18 at 20:12





But when page load it never hit OnGet method, any idea why that happen?

– Jonathan
Nov 13 '18 at 20:12













You're loading a different page, then. Check your URL/routes. OnGet is the default for a Razor Page, so if that's never getting called, you're not hitting that page at all.

– Chris Pratt
Nov 13 '18 at 20:13





You're loading a different page, then. Check your URL/routes. OnGet is the default for a Razor Page, so if that's never getting called, you're not hitting that page at all.

– Chris Pratt
Nov 13 '18 at 20:13



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53288542%2fperform-a-get-action-with-asp-net-core-2-1%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

Syphilis

Darth Vader #20