简体   繁体   English

c#Razor Pages选择标签助手

[英]c# Razor Pages Select Tag Helper

New to Razor, c# and ASP and with refrence to the following. Razor的C#和ASP的新功能,并参考以下内容。 https://docs.microsoft.com/en-us/aspnet/core/tutorials/razor-pages/?view=aspnetcore-2.1 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. 目前,我可以使用Pages代码中的以下代码将模型中的行排列到选择视图中。

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. 在我的HTML页面参考中,可以产生一个不错的评级列表供您选择。

<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 (ie GOOD, BAD, UGLY), everything works ok but when I inset into the DB I would like to inset the "ID" and not the "MovieRating" 我的问题是生成的选项没有Movie.Rating字段的值(即GOOD,BAD,UGLY)没有其他值,一切正常,但是当我插入数据库时​​,我想插入“ ID”而不是“电影评分”

I would like to have the following html created when the page is generated. 我想在页面生成时创建以下html。 Where the ID field from within the Table is the Value and the "MovieRating" is the Text. 表中的ID字段是“值”,“ MovieRating”是文本。

<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. 为此,我知道我需要在select语句中选择多个“ MovieRating”字段。 So I can change this to also select the ID. 因此,我可以将其更改为还选择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. 我可以在网上找到一些有关如何以另一种方式实现此目标的示例,但我现在还不想钻研MVC。

Your current LINQ query is SELECT ing 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. 当前的LINQ查询正在SELECT单个列MovieRating并且您正在使用执行该查询的结果(即字符串列表)来构建SelectList对象。 Your select tag helper is using this SelectList object which has only the MovieRating string values as the underlying items. 您的选择标签帮助器正在使用此SelectList对象,该对象仅将MovieRating字符串值作为基础项。

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. 假设您的Ratings类具有Id属性( int类型)和MovieRating属性( string类型),则可以在LINQ表达式的投影部分中创建SelectListItem

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. 在这里, ratingItemsSelectListItem对象的列表。 So change your Ratings properties type to a collection of SelectListItem objects. 因此,将您的Ratings属性类型更改为SelectListItem对象的集合。

public List<SelectListItem> Ratings { get; set; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM