Multiple conflicting route parameter formats in Razor Pages
I'm making simple crud forms based on the tutorials for Razor Pages MVVM - https://docs.microsoft.com/en-us/aspnet/core/tutorials/razor-pages/?view=aspnetcore-2.1
The issue is the elements on the Index page use different formats for the route parameter and I end up with URL's like /StockIndexMonths/2?StockIndexId=1
Where both /2 and StockIndexId=1 are the same parameter
- The select list will use ?StockIndexId=1
- The Create New link will use /1, when returning to the Index /1 is used
- If I use the select list again I get both /1?StockIndexId=2
Can anyone tell me the preferred way to force the same parameter format to be used? I'm trying to keep Razor Pages doing it's 'magic'
Index.cshtml
@page "StockIndexId?"
@model Investments.Pages.StockIndexMonths.IndexModel
@
ViewData["Title"] = "Index";
<h2>Index</h2>
<form>
<select asp-for="StockIndexId" asp-items="Model.StockIndexNameSelect" onchange="this.form.submit();"></select>
</form>
<a asp-page="Create" asp-route-StockIndexId="@Model.StockIndexId">Create New</a>
<table class="table">
...
razor-pages
add a comment |
I'm making simple crud forms based on the tutorials for Razor Pages MVVM - https://docs.microsoft.com/en-us/aspnet/core/tutorials/razor-pages/?view=aspnetcore-2.1
The issue is the elements on the Index page use different formats for the route parameter and I end up with URL's like /StockIndexMonths/2?StockIndexId=1
Where both /2 and StockIndexId=1 are the same parameter
- The select list will use ?StockIndexId=1
- The Create New link will use /1, when returning to the Index /1 is used
- If I use the select list again I get both /1?StockIndexId=2
Can anyone tell me the preferred way to force the same parameter format to be used? I'm trying to keep Razor Pages doing it's 'magic'
Index.cshtml
@page "StockIndexId?"
@model Investments.Pages.StockIndexMonths.IndexModel
@
ViewData["Title"] = "Index";
<h2>Index</h2>
<form>
<select asp-for="StockIndexId" asp-items="Model.StockIndexNameSelect" onchange="this.form.submit();"></select>
</form>
<a asp-page="Create" asp-route-StockIndexId="@Model.StockIndexId">Create New</a>
<table class="table">
...
razor-pages
add a comment |
I'm making simple crud forms based on the tutorials for Razor Pages MVVM - https://docs.microsoft.com/en-us/aspnet/core/tutorials/razor-pages/?view=aspnetcore-2.1
The issue is the elements on the Index page use different formats for the route parameter and I end up with URL's like /StockIndexMonths/2?StockIndexId=1
Where both /2 and StockIndexId=1 are the same parameter
- The select list will use ?StockIndexId=1
- The Create New link will use /1, when returning to the Index /1 is used
- If I use the select list again I get both /1?StockIndexId=2
Can anyone tell me the preferred way to force the same parameter format to be used? I'm trying to keep Razor Pages doing it's 'magic'
Index.cshtml
@page "StockIndexId?"
@model Investments.Pages.StockIndexMonths.IndexModel
@
ViewData["Title"] = "Index";
<h2>Index</h2>
<form>
<select asp-for="StockIndexId" asp-items="Model.StockIndexNameSelect" onchange="this.form.submit();"></select>
</form>
<a asp-page="Create" asp-route-StockIndexId="@Model.StockIndexId">Create New</a>
<table class="table">
...
razor-pages
I'm making simple crud forms based on the tutorials for Razor Pages MVVM - https://docs.microsoft.com/en-us/aspnet/core/tutorials/razor-pages/?view=aspnetcore-2.1
The issue is the elements on the Index page use different formats for the route parameter and I end up with URL's like /StockIndexMonths/2?StockIndexId=1
Where both /2 and StockIndexId=1 are the same parameter
- The select list will use ?StockIndexId=1
- The Create New link will use /1, when returning to the Index /1 is used
- If I use the select list again I get both /1?StockIndexId=2
Can anyone tell me the preferred way to force the same parameter format to be used? I'm trying to keep Razor Pages doing it's 'magic'
Index.cshtml
@page "StockIndexId?"
@model Investments.Pages.StockIndexMonths.IndexModel
@
ViewData["Title"] = "Index";
<h2>Index</h2>
<form>
<select asp-for="StockIndexId" asp-items="Model.StockIndexNameSelect" onchange="this.form.submit();"></select>
</form>
<a asp-page="Create" asp-route-StockIndexId="@Model.StockIndexId">Create New</a>
<table class="table">
...
razor-pages
razor-pages
asked Nov 15 '18 at 0:06
mrDavidmrDavid
31
31
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Alter the form tag so that it uses the POST
method:
<form method="post">
Currently, because the method
is not specified, GET
is used by default, which appends forms values to the URL as query string values. That's why you see what you are seeing.
Thanks for the suggestion. It does solve the immediate issue, but it requires adding a OnPostAsync and duplicating the OnGetAsync code. I think I'm better using javascript which is my current work around
– mrDavid
Nov 18 '18 at 7:54
You can centralise any page initialisation code, such as populating SelectLists, in the PageModel constructor instead of duplicating it in multiple handler methods.
– Mike Brind
Nov 18 '18 at 8:17
Yes, but I would also need to duplicate the code to generate the main Index table (or use a central function). I'm not posting a new item to be saved, just an ID for filtering. Isn't a form GET the correct usage? Respectfully, I'm trying to stick to the principles, or else I would have continued using Web Forms. I still see the issue as Razor Pages is using 2 formats of parameter and I'd hope there is a way to force it to use 1. If there isn't, a single javascript command seems as effective, with a lot less code, and staying 'restful'
– mrDavid
Nov 21 '18 at 22:38
A form submitted using GET will append values in the query string. That's how it works in Web Forms, MVC, PHP etc. Nothing to do with Razor Pages. Using JS to pass the selected value in the URL format that you want is a good option. Or you can use the OnPost handler to do a RedirectToPage call
– Mike Brind
Nov 22 '18 at 7:36
Thanks Mike, appreciate your help. You did provide a working solution so I'll mark this as answered
– mrDavid
Nov 25 '18 at 21:32
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%2f53310627%2fmultiple-conflicting-route-parameter-formats-in-razor-pages%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
Alter the form tag so that it uses the POST
method:
<form method="post">
Currently, because the method
is not specified, GET
is used by default, which appends forms values to the URL as query string values. That's why you see what you are seeing.
Thanks for the suggestion. It does solve the immediate issue, but it requires adding a OnPostAsync and duplicating the OnGetAsync code. I think I'm better using javascript which is my current work around
– mrDavid
Nov 18 '18 at 7:54
You can centralise any page initialisation code, such as populating SelectLists, in the PageModel constructor instead of duplicating it in multiple handler methods.
– Mike Brind
Nov 18 '18 at 8:17
Yes, but I would also need to duplicate the code to generate the main Index table (or use a central function). I'm not posting a new item to be saved, just an ID for filtering. Isn't a form GET the correct usage? Respectfully, I'm trying to stick to the principles, or else I would have continued using Web Forms. I still see the issue as Razor Pages is using 2 formats of parameter and I'd hope there is a way to force it to use 1. If there isn't, a single javascript command seems as effective, with a lot less code, and staying 'restful'
– mrDavid
Nov 21 '18 at 22:38
A form submitted using GET will append values in the query string. That's how it works in Web Forms, MVC, PHP etc. Nothing to do with Razor Pages. Using JS to pass the selected value in the URL format that you want is a good option. Or you can use the OnPost handler to do a RedirectToPage call
– Mike Brind
Nov 22 '18 at 7:36
Thanks Mike, appreciate your help. You did provide a working solution so I'll mark this as answered
– mrDavid
Nov 25 '18 at 21:32
add a comment |
Alter the form tag so that it uses the POST
method:
<form method="post">
Currently, because the method
is not specified, GET
is used by default, which appends forms values to the URL as query string values. That's why you see what you are seeing.
Thanks for the suggestion. It does solve the immediate issue, but it requires adding a OnPostAsync and duplicating the OnGetAsync code. I think I'm better using javascript which is my current work around
– mrDavid
Nov 18 '18 at 7:54
You can centralise any page initialisation code, such as populating SelectLists, in the PageModel constructor instead of duplicating it in multiple handler methods.
– Mike Brind
Nov 18 '18 at 8:17
Yes, but I would also need to duplicate the code to generate the main Index table (or use a central function). I'm not posting a new item to be saved, just an ID for filtering. Isn't a form GET the correct usage? Respectfully, I'm trying to stick to the principles, or else I would have continued using Web Forms. I still see the issue as Razor Pages is using 2 formats of parameter and I'd hope there is a way to force it to use 1. If there isn't, a single javascript command seems as effective, with a lot less code, and staying 'restful'
– mrDavid
Nov 21 '18 at 22:38
A form submitted using GET will append values in the query string. That's how it works in Web Forms, MVC, PHP etc. Nothing to do with Razor Pages. Using JS to pass the selected value in the URL format that you want is a good option. Or you can use the OnPost handler to do a RedirectToPage call
– Mike Brind
Nov 22 '18 at 7:36
Thanks Mike, appreciate your help. You did provide a working solution so I'll mark this as answered
– mrDavid
Nov 25 '18 at 21:32
add a comment |
Alter the form tag so that it uses the POST
method:
<form method="post">
Currently, because the method
is not specified, GET
is used by default, which appends forms values to the URL as query string values. That's why you see what you are seeing.
Alter the form tag so that it uses the POST
method:
<form method="post">
Currently, because the method
is not specified, GET
is used by default, which appends forms values to the URL as query string values. That's why you see what you are seeing.
answered Nov 15 '18 at 8:20
Mike BrindMike Brind
17.3k54069
17.3k54069
Thanks for the suggestion. It does solve the immediate issue, but it requires adding a OnPostAsync and duplicating the OnGetAsync code. I think I'm better using javascript which is my current work around
– mrDavid
Nov 18 '18 at 7:54
You can centralise any page initialisation code, such as populating SelectLists, in the PageModel constructor instead of duplicating it in multiple handler methods.
– Mike Brind
Nov 18 '18 at 8:17
Yes, but I would also need to duplicate the code to generate the main Index table (or use a central function). I'm not posting a new item to be saved, just an ID for filtering. Isn't a form GET the correct usage? Respectfully, I'm trying to stick to the principles, or else I would have continued using Web Forms. I still see the issue as Razor Pages is using 2 formats of parameter and I'd hope there is a way to force it to use 1. If there isn't, a single javascript command seems as effective, with a lot less code, and staying 'restful'
– mrDavid
Nov 21 '18 at 22:38
A form submitted using GET will append values in the query string. That's how it works in Web Forms, MVC, PHP etc. Nothing to do with Razor Pages. Using JS to pass the selected value in the URL format that you want is a good option. Or you can use the OnPost handler to do a RedirectToPage call
– Mike Brind
Nov 22 '18 at 7:36
Thanks Mike, appreciate your help. You did provide a working solution so I'll mark this as answered
– mrDavid
Nov 25 '18 at 21:32
add a comment |
Thanks for the suggestion. It does solve the immediate issue, but it requires adding a OnPostAsync and duplicating the OnGetAsync code. I think I'm better using javascript which is my current work around
– mrDavid
Nov 18 '18 at 7:54
You can centralise any page initialisation code, such as populating SelectLists, in the PageModel constructor instead of duplicating it in multiple handler methods.
– Mike Brind
Nov 18 '18 at 8:17
Yes, but I would also need to duplicate the code to generate the main Index table (or use a central function). I'm not posting a new item to be saved, just an ID for filtering. Isn't a form GET the correct usage? Respectfully, I'm trying to stick to the principles, or else I would have continued using Web Forms. I still see the issue as Razor Pages is using 2 formats of parameter and I'd hope there is a way to force it to use 1. If there isn't, a single javascript command seems as effective, with a lot less code, and staying 'restful'
– mrDavid
Nov 21 '18 at 22:38
A form submitted using GET will append values in the query string. That's how it works in Web Forms, MVC, PHP etc. Nothing to do with Razor Pages. Using JS to pass the selected value in the URL format that you want is a good option. Or you can use the OnPost handler to do a RedirectToPage call
– Mike Brind
Nov 22 '18 at 7:36
Thanks Mike, appreciate your help. You did provide a working solution so I'll mark this as answered
– mrDavid
Nov 25 '18 at 21:32
Thanks for the suggestion. It does solve the immediate issue, but it requires adding a OnPostAsync and duplicating the OnGetAsync code. I think I'm better using javascript which is my current work around
– mrDavid
Nov 18 '18 at 7:54
Thanks for the suggestion. It does solve the immediate issue, but it requires adding a OnPostAsync and duplicating the OnGetAsync code. I think I'm better using javascript which is my current work around
– mrDavid
Nov 18 '18 at 7:54
You can centralise any page initialisation code, such as populating SelectLists, in the PageModel constructor instead of duplicating it in multiple handler methods.
– Mike Brind
Nov 18 '18 at 8:17
You can centralise any page initialisation code, such as populating SelectLists, in the PageModel constructor instead of duplicating it in multiple handler methods.
– Mike Brind
Nov 18 '18 at 8:17
Yes, but I would also need to duplicate the code to generate the main Index table (or use a central function). I'm not posting a new item to be saved, just an ID for filtering. Isn't a form GET the correct usage? Respectfully, I'm trying to stick to the principles, or else I would have continued using Web Forms. I still see the issue as Razor Pages is using 2 formats of parameter and I'd hope there is a way to force it to use 1. If there isn't, a single javascript command seems as effective, with a lot less code, and staying 'restful'
– mrDavid
Nov 21 '18 at 22:38
Yes, but I would also need to duplicate the code to generate the main Index table (or use a central function). I'm not posting a new item to be saved, just an ID for filtering. Isn't a form GET the correct usage? Respectfully, I'm trying to stick to the principles, or else I would have continued using Web Forms. I still see the issue as Razor Pages is using 2 formats of parameter and I'd hope there is a way to force it to use 1. If there isn't, a single javascript command seems as effective, with a lot less code, and staying 'restful'
– mrDavid
Nov 21 '18 at 22:38
A form submitted using GET will append values in the query string. That's how it works in Web Forms, MVC, PHP etc. Nothing to do with Razor Pages. Using JS to pass the selected value in the URL format that you want is a good option. Or you can use the OnPost handler to do a RedirectToPage call
– Mike Brind
Nov 22 '18 at 7:36
A form submitted using GET will append values in the query string. That's how it works in Web Forms, MVC, PHP etc. Nothing to do with Razor Pages. Using JS to pass the selected value in the URL format that you want is a good option. Or you can use the OnPost handler to do a RedirectToPage call
– Mike Brind
Nov 22 '18 at 7:36
Thanks Mike, appreciate your help. You did provide a working solution so I'll mark this as answered
– mrDavid
Nov 25 '18 at 21:32
Thanks Mike, appreciate your help. You did provide a working solution so I'll mark this as answered
– mrDavid
Nov 25 '18 at 21:32
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%2f53310627%2fmultiple-conflicting-route-parameter-formats-in-razor-pages%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