[英]Can we define class in Model:PageModel level by for loop in Asp.net core 6.0 ef?
I had a page which let user to add input field (max 10) by Javascript if they want.我有一个页面,允许用户根据需要通过 Javascript 添加输入字段(最多 10 个)。 Thus, I made a object class call "Items_PC" and define "Items_PC1" to "Items_PC10" for asp-for="" usage.
因此,我进行了 object class 调用“Items_PC”并将“Items_PC1”定义为“Items_PC10”以用于 asp-for="" 用法。
It is ok when the max number is 10, but i need to manually define it 100 times if the max number is 100, i don't think it is a smart way to do that and the coding is very ugly.当最大数量为 10 时没问题,但如果最大数量为 100,我需要手动定义它 100 次,我认为这不是一个聪明的方法,而且编码非常难看。 Is there any method to define it by for loop?
有什么方法可以通过for循环来定义它吗?
i use javascript to add the input field:我使用 javascript 添加输入字段:
var item = 1;
divtest.innerHTML = ... + 'asp-for"Items_PC' + item + '.model"' + ...
Index.cshtml:索引.cshtml:
@page
@model KelseyWeb.Pages.IndexModel
@{
}
<form id="form" method="post" autocomplete="off">
<select id="mySelect" name="values" style="width:95%; height:30px" class=" ms-3">
<option>Desktop/Notebook</option>
<option>Monitor</option>
<option>Others</option>
</select>
<input class="" style="width:90px; float:right; font-size:15px" type="button" onclick="add_RequestedItems_fields();" value="Add More" />
<div class="mb-3" id="1_RequestedItems_fileds">
</div>
</form>
@section Scripts{
<script>
var item = 0;
function add_RequestedItems_fields() {
if(item <10){
item++;
var objTo = document.getElementById('1_RequestedItems_fileds')
var divtest = document.createElement("div");
if (document.getElementById("mySelect").value.toString() == "Desktop/Notebook") {
divtest.innerHTML = '<div class="border mt-3 py-3 pe-3" style="display:block">' +
'<input type="text" asp-for="Items_PC' + item + '.Model" value="Desktop/Notebook"/>'+
'</div>';
}
}
objTo.appendChild(divtest)
}
</script>
}
Index.cshtml.cs索引.cshtml.cs
namespace KelseyWeb.Pages
{
public class IndexModel : PageModel
{
public Items_PC Items_PC1 { get; set; }
public Items_PC Items_PC2 { get; set; }
public Items_PC Items_PC3 { get; set; }
public Items_PC Items_PC4 { get; set; }
public Items_PC Items_PC5 { get; set; }
public Items_PC Items_PC6 { get; set; }
public Items_PC Items_PC7 { get; set; }
public Items_PC Items_PC8 { get; set; }
public Items_PC Items_PC9 { get; set; }
public Items_PC Items_PC10 { get; set; }
public void OnGet()
{
}
}
public class Items_PC
{
public int Unit { get; set; }
public string Model { get; set; } = null!;
public string Spec { get; set; } = null!;
public string WindowsVersion { get; set; } = null!;
public string Warranty { get; set; } = null!;
}
public class Items_Monitor
{
public int Unit { get; set; }
public string Model { get; set; } = null!;
public string Warranty { get; set; } = null!;
}
public class Items_Others
{
public int Unit { get; set; }
public string Model { get; set; } = null!;
public string Spec { get; set; } = null!;
}
}
I tried to use for loop and Error - CS1519 Invalid token 'for' in class, record, struct, or interface member declaration is received.我尝试使用 for 循环和错误 - CS1519 Invalid token 'for' in class, record, struct, or interface member declaration is received.
I want the code to be better.我希望代码更好。
I think you should modify the properties in your page model:我认为你应该修改你页面 model 中的属性:
public Items_PC Items_PC1 { get; set; }
public Items_PC Items_PC2 { get; set; }
public Items_PC Items_PC3 { get; set; }
public Items_PC Items_PC4 { get; set; }
public Items_PC Items_PC5 { get; set; }
public Items_PC Items_PC6 { get; set; }
public Items_PC Items_PC7 { get; set; }
public Items_PC Items_PC8 { get; set; }
public Items_PC Items_PC9 { get; set; }
public Items_PC Items_PC10 { get; set; }
To到
[BindProperty]
public List<Items_PC> Items_PCs { get; set; }
generate the input boxes without asp-xx attribute:生成没有 asp-xx 属性的输入框:
<input type="text" id="Items_PCs[0].Unit" name="Items_PCs[0].Unit/>
......
//other properties you need to insert
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.