简体   繁体   English

如何使用 ASP.NET Core Razor 页面在下拉列表中预选项目

[英]How to preselect an item in a dropdown using ASP.NET Core Razor pages

If it helps we are using ASP.NET Core 2.1 and Razor pages (not traditional MVC)如果有帮助,我们将使用 ASP.NET Core 2.1 和 Razor 页面(不是传统的 MVC)

I am having issues having the select work.我在选择工作时遇到问题。 We are generating a list of options for our page using list of SelectListItem that brings in the enums and we place it in our asp-items and that works awesome.我们正在使用引入枚举的SelectListItem列表为我们的页面生成一个选项列表,并将其放置在我们的 asp-item 中,并且效果很好。 For our asp-for we are using a label that increments because have loop of a few dropdowns.对于我们的 asp-for,我们使用了一个递增的标签,因为有几个下拉列表的循环。

<select asp-for="@Model.Answers[answerOptionIndex].AnswerText" 
        asp-items="@Model.CriticalIllnessTierSelectListItems" 
        class="form-control">
    <option></option>
</select>

For doing a product add, this works great, the asp-for generates Answers[0].AnswerText for name and Answers_0__AnswerText for id and that's wonderful.为了进行产品添加,这很好用,asp-for 为名称生成Answers[0].AnswerText ,为 id 生成Answers_0__AnswerText ,这很棒。

The trouble is we need to be able to edit an existing form so in order to prepopulate the values, I found that you can use the asp-for for that, so I put in @answerTextForQuestion for asp-for and now the correct option is preselected on page load... but then it messes up my the IDs and Name.问题是我们需要能够编辑现有表单,因此为了预填充值,我发现您可以使用 asp-for,所以我为asp-for输入了@answerTextForQuestion ,现在正确的选项是在页面加载时预先选择...但随后它弄乱了我的 ID 和名称。

When I do that I get this:当我这样做时,我得到了这个:

<select name="answerTextForQuestion" class="form-control" 
        id="answerTextForQuestion">

What's strange is it doesn't put the value answerTextForQuestion is, it just puts the variable.奇怪的是它没有放置值answerTextForQuestion ,它只是放置变量。

I try to manually set name and ID to @Model.Answers[answerOptionIndex].AnswerText and leave the asp-for as @answerTextForQuestion but it ignores it and still puts in @answerTextForQuestion for the question and answer.我尝试手动将名称和 ID 设置为@Model.Answers[answerOptionIndex].AnswerText并将asp-for保留为@answerTextForQuestion但它忽略它并仍然为问题和答案输入@answerTextForQuestion

I don't know how have my cake and eat it to.我不知道我的蛋糕怎么吃。 I need my asp-for to be @Model.Answers[answerOptionIndex].AnswerText but I also need it to choose whichever value is @answerTextForQuestion .我需要我的 asp-for 是@Model.Answers[answerOptionIndex].AnswerText但我也需要它选择@answerTextForQuestion任何值。 Because we are using SelectListItem I can't just throw a selected attribute on the right one.因为我们使用的是SelectListItem所以我不能只在正确的属性上抛出一个 selected 属性。

I found a Stack Overflow that tried to use a loop where if that item in selected matched answerTextForQuestion then make the selected attribute true but that wasn't working and I read in the comments that selected is read only, so that won't work.我发现了一个堆栈溢出,它试图使用一个循环,如果选定的匹配answerTextForQuestion中的项目然后使选定的属性为真,但这不起作用,我在评论中阅读了选定的只读,因此不起作用。 I bolded this because I am worried it would get downvoted as a duplicate post.我将其加粗,因为我担心它会被认为是重复的帖子。

Edit: a better title may have been is there another attribute I can use to tell the browser which option should be preselected without using ASP-FOR.编辑:可能有更好的标题,我可以使用另一个属性来告诉浏览器应该在不使用 ASP-FOR 的情况下预选哪个选项。 We have a lot of our AJAX built around the name and ID that ASP-FOR uses but I just need this one function.我们有很多围绕 ASP-FOR 使用的名称和 ID 构建的 AJAX,但我只需要这个功能。 I looked at the C# docs but didn't see anything.我查看了 C# 文档,但没有看到任何内容。

For preselecting an item in dropdownlist, you could refer a part of the code for the simple example as shown below要预先选择下拉列表中的项目,您可以参考以下简单示例的部分代码

1.In Edit.cshtml 1.在Edit.cshtml中

<div class="form-group">
            <label asp-for="Movie.Genre" class="control-label"></label>
            <select  asp-for="Movie.Genre.Id" asp-items='(List<SelectListItem>)ViewData["Genres"]'></select>
        </div>

2.In Edit.cshtml.cs 2.在Edit.cshtml.cs中

Model Binding模型绑定

[BindProperty]
    public Movie Movie { get; set; }

    public Genre Genre { get; set; }

OnGetAsync() OnGetAsync()

 Movie = await _context.Movie
                     .Include(m => m.Genre)
                     .SingleOrDefaultAsync(m => m.Id == id);

 ViewData["Genres"] = _context.Genre.Select(g => new SelectListItem { Value = g.Id.ToString(), Text = g.Name }).ToList();

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

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