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:
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
|
show 3 more comments
I'm working on asp.net core 2.1
Now we have views with classes like:
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
Yourasp-for="RoleId"
I supposeRoleId
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 theRoleId
?asp-for
needs a Model.
– Llazar
Nov 13 '18 at 20:06
RoleId
it's the value of viewmodel :ApplicationRoleViewModel
@Llazar
– Jonathan
Nov 13 '18 at 20:08
|
show 3 more comments
I'm working on asp.net core 2.1
Now we have views with classes like:
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
I'm working on asp.net core 2.1
Now we have views with classes like:
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
c# asp.net asp.net-core .net-core asp.net-core-2.1
edited Nov 13 '18 at 20:03
Jonathan
asked Nov 13 '18 at 19:54
JonathanJonathan
3248
3248
Yourasp-for="RoleId"
I supposeRoleId
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 theRoleId
?asp-for
needs a Model.
– Llazar
Nov 13 '18 at 20:06
RoleId
it's the value of viewmodel :ApplicationRoleViewModel
@Llazar
– Jonathan
Nov 13 '18 at 20:08
|
show 3 more comments
Yourasp-for="RoleId"
I supposeRoleId
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 theRoleId
?asp-for
needs a Model.
– Llazar
Nov 13 '18 at 20:06
RoleId
it'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
RoleId
it's the value of viewmodel : ApplicationRoleViewModel
@Llazar– Jonathan
Nov 13 '18 at 20:08
RoleId
it's the value of viewmodel : ApplicationRoleViewModel
@Llazar– Jonathan
Nov 13 '18 at 20:08
|
show 3 more comments
1 Answer
1
active
oldest
votes
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();
But when page load it never hitOnGet
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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();
But when page load it never hitOnGet
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
add a comment |
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();
But when page load it never hitOnGet
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
add a comment |
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();
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();
answered Nov 13 '18 at 20:02
Chris PrattChris Pratt
155k20238304
155k20238304
But when page load it never hitOnGet
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
add a comment |
But when page load it never hitOnGet
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Your
asp-for="RoleId"
I supposeRoleId
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
RoleId
it's the value of viewmodel :ApplicationRoleViewModel
@Llazar– Jonathan
Nov 13 '18 at 20:08