简体   繁体   English

我们可以在Model中定义class:Asp.net core 6.0 ef中的PageModel级别通过for循环吗?

[英]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.

相关问题 如何在asp.net core v 2.0中从javascript调用PageModel方法 - How to call PageModel Method from javascript in asp.net core v 2.0 我们可以将整个 model 传递给 javascript asp.net mvc - can we pass entire model to javascript asp.net mvc 在 Asp.net Core MVC 中序列化表单并将其转换为复杂类以使用 Ajax 发布整个模型的问题 - Problems Serializing a Form and Converting It to a Complex Class in Asp.net Core MVC to Post a Whole Model Using Ajax ASP.NET Core API 中的模型绑定始终为空 - Model binding in ASP.NET Core API are always null Asp.net core 如何一一显示IEnumerable模型 - Asp.net core how to display IEnumerable model one by one asp.net mvc - 如何在javascript中循环遍历模型数据 - asp.net mvc - how can you loop through model data in javascript 在ASP.NET Core中对复杂类型上的列表进行绑定的模型 - Model binding a list on a complex type in ASP.NET Core 如何在asp.net Core中将模型对象传递给javascript函数 - How to pass model object to javascript function in asp.net Core Ajax POST 不将 model 发布到 asp.net 核心 6 中的 controller - Ajax POST not posting model to the controller in asp.net core 6 在 Asp.Net Core 中使用 jquery 将图像预览到循环 html 输入? - Preview images to a loop html input with jquery in Asp.Net Core?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM