英文:
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
<div id="newRow">
<select asp-for="ingredientsIDs" asp-items="Model.Options"></select>
</div>
<button type="button" id="addRow">Add ingredient</button>
The selected item is saved to model property called ingredientsIDs
.cshtml.cs file:
[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();
}
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-for
和 asp-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 演示
英文:
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.
<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()">Add ingredient</button>
<button>Submit</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 Demo
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论