c#Razor Pages Select Tag Helper

マットレイランド

Razor、c#、ASPの新機能であり、以下を参照してください。https://docs.microsoft.com/en-us/aspnet/core/tutorials/razor-pages/?view=aspnetcore-2.1

現在、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();

    }

そして、私のHTMLページの参照内で、選択できる評価の優れたリストを作成します。

<select asp-for="Movie.Rating" asp-items="Model.Ratings">
            </select>

私の問題は、生成されたオプションにMovie.Ratingフィールド以外の値(つまり、GOOD、BAD、UGLY)がないことです。すべて正常に機能しますが、DBに挿入するときに、「ID」を挿入したいのですが、 「MovieRating」

ページの生成時に次のhtmlを作成してもらいたいのですが。ここで、テーブル内の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> 

これを行うには、selectステートメント内の「MovieRating」フィールド以外を選択する必要があることを知っています。だから私はこれを変更してIDも選択することができます。ただし、結合された文字列をオプションフィールドに吐き出すだけで、値フィールドは生成されません。

これは私がやりたいことを達成するための正しい方法ですか。これを別の方法で実現する方法の例をオンラインでいくつか見つけることができますが、MVCについてはまだ詳しく説明したくありません。

しゅじゅ

現在のLINQクエリは単一の列をSELECTしMovieRatingており、文字列のリストであるそのクエリを実行した結果を使用してSelectListオブジェクトを構築しています。selectタグヘルパーはSelectList、MovieRating文字列値のみを基になるアイテムとして持つこのオブジェクトを使用しています。

あなたと仮定すると、Ratingsクラスが持つIdプロパティ(intタイプ)とMovieRatingプロパティ(stringタイプ)を、あなたが作成することもSelectListItem、あなたのLINQの式の突起部に。

List<SelectListItem> ratingItems = _context.Ratings
                                            .Select(a=>new SelectListItem
                                                       {
                                                          Value=a.Id.ToString(),
                                                          Text = a.MovieRating
                                                       }).ToList();

Ratings = ratingItems;
return Page();

これratingItemsSelectListItemオブジェクトのリストです。したがって、RatingsプロパティタイプをSelectListItemオブジェクトのコレクションに変更します

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

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

C#razor select2 disable

分類Dev

How to Use Razor Partial Tag Helper in a Layout

分類Dev

Custom tag helper isn't interpreted on razor page

分類Dev

Rails Image Tag Helper

分類Dev

Collection_select helper cannot build disabled html tag in ruby on rails erb

分類Dev

Razor htmlListBox helper

分類Dev

C#Razor変数を取得

分類Dev

ASP.net razor pages and multi-select, posting all items in select

分類Dev

ASP Core tag helper with inheritance in model

分類Dev

MVC Core HTML Bootstrap button in Tag Helper

分類Dev

how to access authorized user a custom tag helper

分類Dev

From within the tag helper, how can I access the attributes that are not properties of the tag helper?

分類Dev

placeholder for select tag

分類Dev

c#Razor Pages、EF Core、コードを使用して子レコードを生成し、を使用してまだ追加されていない親レコードに追加するにはどうすればよいですか?

分類Dev

Is there a way to tag pages using the OneNote API?

分類Dev

Anchor tag helper including page in querystring instead (?page=)

分類Dev

Set custom data- attributes on {{#linkTo}} helper <a> tag

分類Dev

How to get HTML tag included in the output using a helper method

分類Dev

Razor @helper functions not rendering any Html

分類Dev

set_select for select tag in codeigniter

分類Dev

Python splinter select by tag attribute

分類Dev

select tag is automaticaly closed on click

分類Dev

Simple value bind to select tag

分類Dev

Helper function and abstraction in c++

分類Dev

Class helper in C++ Builder

分類Dev

rails erb form helper options_for_select:selected

分類Dev

How get new installed product attribute in magento (using helper) on category pages

分類Dev

C#RazorページのBindProperty属性が機能しない

分類Dev

nullオブジェクトをJavaScriptに渡すC#Razor View

Related 関連記事

  1. 1

    C#razor select2 disable

  2. 2

    How to Use Razor Partial Tag Helper in a Layout

  3. 3

    Custom tag helper isn't interpreted on razor page

  4. 4

    Rails Image Tag Helper

  5. 5

    Collection_select helper cannot build disabled html tag in ruby on rails erb

  6. 6

    Razor htmlListBox helper

  7. 7

    C#Razor変数を取得

  8. 8

    ASP.net razor pages and multi-select, posting all items in select

  9. 9

    ASP Core tag helper with inheritance in model

  10. 10

    MVC Core HTML Bootstrap button in Tag Helper

  11. 11

    how to access authorized user a custom tag helper

  12. 12

    From within the tag helper, how can I access the attributes that are not properties of the tag helper?

  13. 13

    placeholder for select tag

  14. 14

    c#Razor Pages、EF Core、コードを使用して子レコードを生成し、を使用してまだ追加されていない親レコードに追加するにはどうすればよいですか?

  15. 15

    Is there a way to tag pages using the OneNote API?

  16. 16

    Anchor tag helper including page in querystring instead (?page=)

  17. 17

    Set custom data- attributes on {{#linkTo}} helper <a> tag

  18. 18

    How to get HTML tag included in the output using a helper method

  19. 19

    Razor @helper functions not rendering any Html

  20. 20

    set_select for select tag in codeigniter

  21. 21

    Python splinter select by tag attribute

  22. 22

    select tag is automaticaly closed on click

  23. 23

    Simple value bind to select tag

  24. 24

    Helper function and abstraction in c++

  25. 25

    Class helper in C++ Builder

  26. 26

    rails erb form helper options_for_select:selected

  27. 27

    How get new installed product attribute in magento (using helper) on category pages

  28. 28

    C#RazorページのBindProperty属性が機能しない

  29. 29

    nullオブジェクトをJavaScriptに渡すC#Razor View

ホットタグ

アーカイブ