c# Razor Pages Select Tag Helper
New to Razor, c# and ASP and with refrence to the following.
https://docs.microsoft.com/en-us/aspnet/core/tutorials/razor-pages/?view=aspnetcore-2.1
Currently I'm able to arrange my rows from my model into a select view using the following code in my Pages code.
public SelectList Ratings { get; set; }
public async Task<IActionResult> OnGetAsync()
{
IQueryable<string> ratingQuery = from m in _context.Ratings
orderby m.MovieRating
select m.MovieRating;
Ratings = new SelectList(await ratingQuery.Distinct().ToListAsync());
return Page();
}
And within my HTML page reference that to produce a nice list of ratings to choose from.
<select asp-for="Movie.Rating" asp-items="Model.Ratings">
</select>
My issue is the options generated do not have a value other than that of the Movie.Rating field (i.e. GOOD, BAD, UGLY), everything works ok but when I inset into the DB I would like to inset the "ID" and not the "MovieRating"
I would like to have the following html created when the page is generated. Where the ID field from within the Table is the Value and the "MovieRating" is the Text.
<select asp-for="Movie.Rating" asp-items="Model.Ratings">
<option value="1">Good</option>
<option value="2">Bad</option>
<option value="3">Ugly</option>
</select>
In order to do this I know I need to select more than the "MovieRating" field within the select statement. So I can change this to also select the ID. However it will just spit out a combined string into the option field and not produce a value field.
Is this the correct method to achieve what I want to do. I can find a few examples online of how to achieve this another way but I do not want to delve into MVC just yet.
c# visual-studio linq razor
add a comment |
New to Razor, c# and ASP and with refrence to the following.
https://docs.microsoft.com/en-us/aspnet/core/tutorials/razor-pages/?view=aspnetcore-2.1
Currently I'm able to arrange my rows from my model into a select view using the following code in my Pages code.
public SelectList Ratings { get; set; }
public async Task<IActionResult> OnGetAsync()
{
IQueryable<string> ratingQuery = from m in _context.Ratings
orderby m.MovieRating
select m.MovieRating;
Ratings = new SelectList(await ratingQuery.Distinct().ToListAsync());
return Page();
}
And within my HTML page reference that to produce a nice list of ratings to choose from.
<select asp-for="Movie.Rating" asp-items="Model.Ratings">
</select>
My issue is the options generated do not have a value other than that of the Movie.Rating field (i.e. GOOD, BAD, UGLY), everything works ok but when I inset into the DB I would like to inset the "ID" and not the "MovieRating"
I would like to have the following html created when the page is generated. Where the ID field from within the Table is the Value and the "MovieRating" is the Text.
<select asp-for="Movie.Rating" asp-items="Model.Ratings">
<option value="1">Good</option>
<option value="2">Bad</option>
<option value="3">Ugly</option>
</select>
In order to do this I know I need to select more than the "MovieRating" field within the select statement. So I can change this to also select the ID. However it will just spit out a combined string into the option field and not produce a value field.
Is this the correct method to achieve what I want to do. I can find a few examples online of how to achieve this another way but I do not want to delve into MVC just yet.
c# visual-studio linq razor
1
Change your query so it gets the name and the id:var ratingQuery = ...select new { m.MovieRating, m.MovieId }and learn how to create a SelectList from that. There are plenty of examples online.
– CodingYoshi
Nov 22 at 19:21
add a comment |
New to Razor, c# and ASP and with refrence to the following.
https://docs.microsoft.com/en-us/aspnet/core/tutorials/razor-pages/?view=aspnetcore-2.1
Currently I'm able to arrange my rows from my model into a select view using the following code in my Pages code.
public SelectList Ratings { get; set; }
public async Task<IActionResult> OnGetAsync()
{
IQueryable<string> ratingQuery = from m in _context.Ratings
orderby m.MovieRating
select m.MovieRating;
Ratings = new SelectList(await ratingQuery.Distinct().ToListAsync());
return Page();
}
And within my HTML page reference that to produce a nice list of ratings to choose from.
<select asp-for="Movie.Rating" asp-items="Model.Ratings">
</select>
My issue is the options generated do not have a value other than that of the Movie.Rating field (i.e. GOOD, BAD, UGLY), everything works ok but when I inset into the DB I would like to inset the "ID" and not the "MovieRating"
I would like to have the following html created when the page is generated. Where the ID field from within the Table is the Value and the "MovieRating" is the Text.
<select asp-for="Movie.Rating" asp-items="Model.Ratings">
<option value="1">Good</option>
<option value="2">Bad</option>
<option value="3">Ugly</option>
</select>
In order to do this I know I need to select more than the "MovieRating" field within the select statement. So I can change this to also select the ID. However it will just spit out a combined string into the option field and not produce a value field.
Is this the correct method to achieve what I want to do. I can find a few examples online of how to achieve this another way but I do not want to delve into MVC just yet.
c# visual-studio linq razor
New to Razor, c# and ASP and with refrence to the following.
https://docs.microsoft.com/en-us/aspnet/core/tutorials/razor-pages/?view=aspnetcore-2.1
Currently I'm able to arrange my rows from my model into a select view using the following code in my Pages code.
public SelectList Ratings { get; set; }
public async Task<IActionResult> OnGetAsync()
{
IQueryable<string> ratingQuery = from m in _context.Ratings
orderby m.MovieRating
select m.MovieRating;
Ratings = new SelectList(await ratingQuery.Distinct().ToListAsync());
return Page();
}
And within my HTML page reference that to produce a nice list of ratings to choose from.
<select asp-for="Movie.Rating" asp-items="Model.Ratings">
</select>
My issue is the options generated do not have a value other than that of the Movie.Rating field (i.e. GOOD, BAD, UGLY), everything works ok but when I inset into the DB I would like to inset the "ID" and not the "MovieRating"
I would like to have the following html created when the page is generated. Where the ID field from within the Table is the Value and the "MovieRating" is the Text.
<select asp-for="Movie.Rating" asp-items="Model.Ratings">
<option value="1">Good</option>
<option value="2">Bad</option>
<option value="3">Ugly</option>
</select>
In order to do this I know I need to select more than the "MovieRating" field within the select statement. So I can change this to also select the ID. However it will just spit out a combined string into the option field and not produce a value field.
Is this the correct method to achieve what I want to do. I can find a few examples online of how to achieve this another way but I do not want to delve into MVC just yet.
c# visual-studio linq razor
c# visual-studio linq razor
asked Nov 22 at 19:15
Matt Leyland
6062612
6062612
1
Change your query so it gets the name and the id:var ratingQuery = ...select new { m.MovieRating, m.MovieId }and learn how to create a SelectList from that. There are plenty of examples online.
– CodingYoshi
Nov 22 at 19:21
add a comment |
1
Change your query so it gets the name and the id:var ratingQuery = ...select new { m.MovieRating, m.MovieId }and learn how to create a SelectList from that. There are plenty of examples online.
– CodingYoshi
Nov 22 at 19:21
1
1
Change your query so it gets the name and the id:
var ratingQuery = ...select new { m.MovieRating, m.MovieId } and learn how to create a SelectList from that. There are plenty of examples online.– CodingYoshi
Nov 22 at 19:21
Change your query so it gets the name and the id:
var ratingQuery = ...select new { m.MovieRating, m.MovieId } and learn how to create a SelectList from that. There are plenty of examples online.– CodingYoshi
Nov 22 at 19:21
add a comment |
1 Answer
1
active
oldest
votes
Your current LINQ query is SELECTing a single column, MovieRating and you are using the result of executing that query, which is a list of strings, to build the SelectList object. Your select tag helper is using this SelectList object which has only the MovieRating string values as the underlying items.
Assuming your Ratings class has an Id property(int type) and MovieRating property(string type), you may create a SelectListItem in the projection part of your LINQ expression.
List<SelectListItem> ratingItems = _context.Ratings
.Select(a=>new SelectListItem
{
Value=a.Id.ToString(),
Text = a.MovieRating
}).ToList();
Ratings = ratingItems;
return Page();
Here the ratingItems is a list of SelectListItem objects. So change your Ratings properties type to a collection of SelectListItem objects.
public List<SelectListItem> Ratings { get; set; }
Thank you for your explanation, I have changed my code and this is now working. Quick question, why do/should we convert the ID to string ?
– Matt Leyland
Nov 22 at 19:33
1
Use the correct type. If you are storing numeric values, useint
– Shyju
Nov 22 at 19:37
1
Take a look at Select Tag Helper in ASP.NET Core MVC for various options to use the select tag helper
– Shyju
Nov 22 at 19:39
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%2f53436956%2fc-sharp-razor-pages-select-tag-helper%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
Your current LINQ query is SELECTing a single column, MovieRating and you are using the result of executing that query, which is a list of strings, to build the SelectList object. Your select tag helper is using this SelectList object which has only the MovieRating string values as the underlying items.
Assuming your Ratings class has an Id property(int type) and MovieRating property(string type), you may create a SelectListItem in the projection part of your LINQ expression.
List<SelectListItem> ratingItems = _context.Ratings
.Select(a=>new SelectListItem
{
Value=a.Id.ToString(),
Text = a.MovieRating
}).ToList();
Ratings = ratingItems;
return Page();
Here the ratingItems is a list of SelectListItem objects. So change your Ratings properties type to a collection of SelectListItem objects.
public List<SelectListItem> Ratings { get; set; }
Thank you for your explanation, I have changed my code and this is now working. Quick question, why do/should we convert the ID to string ?
– Matt Leyland
Nov 22 at 19:33
1
Use the correct type. If you are storing numeric values, useint
– Shyju
Nov 22 at 19:37
1
Take a look at Select Tag Helper in ASP.NET Core MVC for various options to use the select tag helper
– Shyju
Nov 22 at 19:39
add a comment |
Your current LINQ query is SELECTing a single column, MovieRating and you are using the result of executing that query, which is a list of strings, to build the SelectList object. Your select tag helper is using this SelectList object which has only the MovieRating string values as the underlying items.
Assuming your Ratings class has an Id property(int type) and MovieRating property(string type), you may create a SelectListItem in the projection part of your LINQ expression.
List<SelectListItem> ratingItems = _context.Ratings
.Select(a=>new SelectListItem
{
Value=a.Id.ToString(),
Text = a.MovieRating
}).ToList();
Ratings = ratingItems;
return Page();
Here the ratingItems is a list of SelectListItem objects. So change your Ratings properties type to a collection of SelectListItem objects.
public List<SelectListItem> Ratings { get; set; }
Thank you for your explanation, I have changed my code and this is now working. Quick question, why do/should we convert the ID to string ?
– Matt Leyland
Nov 22 at 19:33
1
Use the correct type. If you are storing numeric values, useint
– Shyju
Nov 22 at 19:37
1
Take a look at Select Tag Helper in ASP.NET Core MVC for various options to use the select tag helper
– Shyju
Nov 22 at 19:39
add a comment |
Your current LINQ query is SELECTing a single column, MovieRating and you are using the result of executing that query, which is a list of strings, to build the SelectList object. Your select tag helper is using this SelectList object which has only the MovieRating string values as the underlying items.
Assuming your Ratings class has an Id property(int type) and MovieRating property(string type), you may create a SelectListItem in the projection part of your LINQ expression.
List<SelectListItem> ratingItems = _context.Ratings
.Select(a=>new SelectListItem
{
Value=a.Id.ToString(),
Text = a.MovieRating
}).ToList();
Ratings = ratingItems;
return Page();
Here the ratingItems is a list of SelectListItem objects. So change your Ratings properties type to a collection of SelectListItem objects.
public List<SelectListItem> Ratings { get; set; }
Your current LINQ query is SELECTing a single column, MovieRating and you are using the result of executing that query, which is a list of strings, to build the SelectList object. Your select tag helper is using this SelectList object which has only the MovieRating string values as the underlying items.
Assuming your Ratings class has an Id property(int type) and MovieRating property(string type), you may create a SelectListItem in the projection part of your LINQ expression.
List<SelectListItem> ratingItems = _context.Ratings
.Select(a=>new SelectListItem
{
Value=a.Id.ToString(),
Text = a.MovieRating
}).ToList();
Ratings = ratingItems;
return Page();
Here the ratingItems is a list of SelectListItem objects. So change your Ratings properties type to a collection of SelectListItem objects.
public List<SelectListItem> Ratings { get; set; }
edited Nov 22 at 19:37
answered Nov 22 at 19:23
Shyju
144k87329435
144k87329435
Thank you for your explanation, I have changed my code and this is now working. Quick question, why do/should we convert the ID to string ?
– Matt Leyland
Nov 22 at 19:33
1
Use the correct type. If you are storing numeric values, useint
– Shyju
Nov 22 at 19:37
1
Take a look at Select Tag Helper in ASP.NET Core MVC for various options to use the select tag helper
– Shyju
Nov 22 at 19:39
add a comment |
Thank you for your explanation, I have changed my code and this is now working. Quick question, why do/should we convert the ID to string ?
– Matt Leyland
Nov 22 at 19:33
1
Use the correct type. If you are storing numeric values, useint
– Shyju
Nov 22 at 19:37
1
Take a look at Select Tag Helper in ASP.NET Core MVC for various options to use the select tag helper
– Shyju
Nov 22 at 19:39
Thank you for your explanation, I have changed my code and this is now working. Quick question, why do/should we convert the ID to string ?
– Matt Leyland
Nov 22 at 19:33
Thank you for your explanation, I have changed my code and this is now working. Quick question, why do/should we convert the ID to string ?
– Matt Leyland
Nov 22 at 19:33
1
1
Use the correct type. If you are storing numeric values, use
int– Shyju
Nov 22 at 19:37
Use the correct type. If you are storing numeric values, use
int– Shyju
Nov 22 at 19:37
1
1
Take a look at Select Tag Helper in ASP.NET Core MVC for various options to use the select tag helper
– Shyju
Nov 22 at 19:39
Take a look at Select Tag Helper in ASP.NET Core MVC for various options to use the select tag helper
– Shyju
Nov 22 at 19:39
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53436956%2fc-sharp-razor-pages-select-tag-helper%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
1
Change your query so it gets the name and the id:
var ratingQuery = ...select new { m.MovieRating, m.MovieId }and learn how to create a SelectList from that. There are plenty of examples online.– CodingYoshi
Nov 22 at 19:21