创建新的HTML元素使用ASP.NET Razor Pages。

huangapple go评论90阅读模式
英文:

Create new HTML element with ASP.NET Razor Pages

问题

我是ASP.NET Razor Pages的新手,我正在尝试每次单击按钮时创建一个新的下拉列表,以便将多个项目添加到我的数据库中。我使用ASP.NET Razor Pages

.cshtml

<div id="newRow">
    <select asp-for="ingredientsIDs" asp-items="Model.Options"></select>
</div>
<button type="button" id="addRow">Add ingredient</button>

所选项目保存在名为ingredientsIDs的模型属性中。

.cshtml.cs文件:

[BindProperty]
public List<int> ingredientsIDs { get; set; }
public List<SelectListItem> Options { get; set; }

public void OnGet()
{
    Options = _context.Ingredients.Select(x => new SelectListItem
    {
        Value = x.Id.ToString(),
        Text = x.NameOfIngredient
    }).ToList();
}

我遇到的第一个问题是它不显示下拉列表,而是显示一个。

第二个问题(我最关心的问题)是,当使用JS脚本时,我无法创建新的选择元素。我无法在那里使用asp-for。我发现这是不可能的,因为JS是基于客户端的,而asp-for是基于服务器的。但我不知道如何使其工作。

有人能帮助我复制/创建一个新的带有与模型属性绑定的选择元素吗?

英文:

I am new to ASP.NET Razor Pages and I am trying to create a new dropdown list every time I click on a button so I can add multiple things to my database.
I use ASP.NET Razor Pages

.cshtml

&lt;div id=&quot;newRow&quot;&gt;
    &lt;select asp-for=&quot;ingredientsIDs&quot; asp-items=&quot;Model.Options&quot;&gt;&lt;/select&gt;
&lt;/div&gt;    
&lt;button type=&quot;button&quot; id=&quot;addRow&quot;&gt;Add ingredient&lt;/button&gt;

The selected item is saved to model property called ingredientsIDs

.cshtml.cs file:

[BindProperty]
public List&lt;int&gt; ingredientsIDs { get; set; }
public List&lt;SelectListItem&gt; Options { get; set; }
public void OnGet()
    {
        Options = _context.Ingredients.Select(x =&gt; new SelectListItem
        {
            Value = x.Id.ToString(),
            Text = x.NameOfIngredient
        }).ToList();

    }

The first issue I am having is that it does not show a dropdown list but rather a

The second thing (the one I care about the most) is I am not able to create a new select element when using JS script. I am not able to put there the asp-for="". I have found out that it is not possible due to JS being client based and asp-for being server based. But I have no idea how to make it work.

Can someone please help me to copy/create new select element with bind to model property?

答案1

得分: 1

你的第一个问题是什么?

我遇到的第一个问题是它不显示一个下拉列表,而是一个

你没有提供一个完整的句子,但从你的问题中,我认为你需要通过在JavaScript中点击按钮来生成一个新的下拉列表。

asp-forasp-items 是razor中的标签助手,你不能直接使用JavaScript来生成它,以下是一个简单的示例可以满足你的需求。

<form method="post">
    <input value="1" type="hidden" disabled="disabled" id="index"/>
    <div id="newRow">
        <select id="ingredientsIDs[0]" name="ingredientsIDs[0]" asp-items="Model.Options"></select>
    </div>
   
    <button type="button" id="addRow" onclick="Add()">添加成分</button>
    <button>提交</button>
</form>

@section Scripts{
    <script>
        function Add(){
            let index = document.getElementById("index");
            let div = document.getElementById("newRow")
            let newSelect = document.createElement("select")
            newSelect.name = "ingredientsIDs["+index.value+"]"

            var sourceDropdown = document.getElementById("ingredientsIDs[0]")

            for (var i= 0; i < sourceDropdown.options.length; i++){
                var option = sourceDropdown.options[i];
                var newOption = document.createElement('option');
                newOption.value = option.value;
                newOption.text = option.text;
                newSelect.appendChild(newOption)
            }
           
            div.appendChild(newSelect);
            index.value++

        }
    </script>
}

Gif 演示

创建新的HTML元素使用ASP.NET Razor Pages。

英文:

What's your first issue?

The first issue I am having is that it does not show a dropdown list but rather a

you don't provide a whole sentence, But from your question, I think you need to generate a new dropdown list via click the button in JavasCript.

asp-for and asp-items are tag helpers in razor, you can't use JavaScript to generate it directly, Here is a simple demo can Meet your needs.

&lt;form method=&quot;post&quot;&gt;
    &lt;input value=&quot;1&quot; type=&quot;hidden&quot; disabled=&quot;disabled&quot; id=&quot;index&quot;/&gt;
    &lt;div id=&quot;newRow&quot;&gt;
        &lt;select id=&quot;ingredientsIDs[0]&quot; name=&quot;ingredientsIDs[0]&quot; asp-items=&quot;Model.Options&quot;&gt;&lt;/select&gt;
    &lt;/div&gt;
   
    &lt;button type=&quot;button&quot; id=&quot;addRow&quot; onclick=&quot;Add()&quot;&gt;Add ingredient&lt;/button&gt;
    &lt;button&gt;Submit&lt;/button&gt;
&lt;/form&gt;

@section Scripts{
    &lt;script&gt;
        function Add(){
            let index = document.getElementById(&quot;index&quot;);
            let div = document.getElementById(&quot;newRow&quot;)
           let newSelect = document.createElement(&quot;select&quot;)
            newSelect.name = &quot;ingredientsIDs[&quot;+index.value+&quot;]&quot;

            var sourceDropdown = document.getElementById(&quot;ingredientsIDs[0]&quot;)
            

            for (var i= 0; i &lt; sourceDropdown.options.length; i++){
                var option = sourceDropdown.options[i];
                var newOption = document.createElement(&#39;option&#39;);
                newOption.value = option.value;
                newOption.text = option.text;
                newSelect.appendChild(newOption)
            }
           
            div.appendChild(newSelect);
            index.value++

        }
    &lt;/script&gt;
}

Gif Demo

创建新的HTML元素使用ASP.NET Razor Pages。

huangapple
  • 本文由 发表于 2023年7月3日 22:32:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76605725.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定