简体   繁体   English

Razor 页面:从页面获取字符串列表到页面模型

[英]Razor Pages: Get List of Strings from Page into Page Model

My razor pages terminology isn't the greatest, so I apologize if I screwed up the wording.我的剃刀页面术语不是最好的,所以如果我搞砸了措辞,我深表歉意。

In my app, a user provides an order number and I give them the list of part numbers associated with it (among other things).在我的应用程序中,用户提供了一个订单号,我向他们提供了与之关联的部件号列表(除其他外)。 I use a foreach loop to create and populate enough input fields to hold all the part numbers.我使用 foreach 循环来创建和填充足够的输入字段以保存所有部件号。 Basically, the user is then able to change the part numbers (customer requirement) and then submit the form.基本上,用户然后能够更改部件号(客户要求),然后提交表单。

My problem comes with grabbing the new values from the page and bringing them into the page model in the POST request.我的问题是从页面中获取新值并将它们带入 POST 请求中的页面模型。 The integers I can grab easily using int lists, but string lists must be populated using .Add().我可以使用 int 列表轻松获取整数,但必须使用 .Add() 填充字符串列表。 Google Search and I both seem to be at a loss of how to grab those string values from the page and into the page model, especially since Google seems to prefer showing me problems involving dropdown lists rather than string lists.谷歌搜索和我似乎都不知道如何从页面获取这些字符串值并进入页面模型,特别是因为谷歌似乎更喜欢向我展示涉及下拉列表而不是字符串列表的问题。

Here is the string list I mentioned.这是我提到的字符串列表。 It is initialized in the constructor.它在构造函数中初始化。

[DisplayName("Part Number"), BindProperty, Required]
[StringLength(48, MinimumLength = 1, ErrorMessage = "Please enter a part number.")]
public List<string> PartNumberList { get; set; }

The relevant part of the page itself:页面本身的相关部分:

@foreach (var part in Model.PartInfoList)
            {
            <tr class="row table-bordered">
                <td class="col-4">
                    <input type="hidden" asp-for="OldPartIDList" value="@part.PART_ID" name="OldPartIDList"/> @*This holds the "old" part ID's so the order can still be looked up when the user wants to change the part ID.*@
                    <input type="text" class="form-control mb-2 mr-sm-2" asp-for="NewPartIDList" value="@part.PART_ID" id="@part.PART_ID" name="NewPartIDList" onchange="HighlightField(this)" />
                    @*The id here is used to determine if the field should be highlighted or not.*@
                    <span class="text-danger" asp-validation-for="NewPartIDList"></span>
                </td>
                <td class="col-4">
                    <input type="text" class="form-control mb-2 mr-sm-2" asp-for="PartNumberList" value="@part.PART_NUMBER" id="@part.PART_NUMBER" name="PartNumberList" onchange="HighlightField(this)" />
                    @*The id here is used to determine if the field should be highlighted or not.*@
                    <span class="text-danger" asp-validation-for="PartNumberList"></span>
                    @Model.PartNumberList.Add(part.PART_NUMBER);
                </td> 
                <td class="col-4">
                    <input type="text" class="form-control mb-2 mr-sm-2" asp-for="QuantityList" value="@part.QUANTITY" id="@part.QUANTITY" name="QuantityList" onchange="HighlightField(this)" />
                    @*The id here is used to determine if the field should be highlighted or not.*@
                    <span class="text-danger" asp-validation-for="QuantityList"></span>
                </td>
            </tr>
            }

You'll notice the:你会注意到:

@Model.PartNumberList.Add(part.PART_NUMBER);

in there.在那里。 I left this in to show that I tried this method, but it throws a syntax error that says: "Cannot implicitly convert type 'void' to 'object'."我留下这个来表明我尝试过这个方法,但它抛出了一个语法错误,上面写着:“不能将类型‘void’隐式转换为‘对象’。”

I just need to be able to compare a list of old part numbers (which I have already) with the input fields displayed on the screen to check for changes.我只需要能够将旧零件编号列表(我已经有了)与屏幕上显示的输入字段进行比较以检查更改。 If I find a change, I update the part number stored in the database to that part number.如果发现更改,我会将存储在数据库中的零件号更新为该零件号。 For those wondering why all this is necessary, this app will be used 100% in-house by people who will know what they are doing to the order and why.对于那些想知道为什么所有这些都是必要的人来说,这个应用程序将 100% 被内部使用,他们知道他们对订单做了什么以及为什么。

I don't know if there is a correct location to do the .Add() to the string list, or if a string list is even the correct way to go for this task.我不知道是否有正确的位置可以对字符串列表执行 .Add() 操作,或者字符串列表是否是执行此任务的正确方法。 Any tips on how to accomplish this task will be appreciated!任何有关如何完成此任务的提示将不胜感激!

Thank you all and have a fantastic day!谢谢大家,祝你有美好的一天!

Edit: I forgot to mention what happens if I try to submit the form (without the @Model line mentioned above).编辑:我忘了提及如果我尝试提交表单(没有上面提到的 @Model 行)会发生什么。 I get this exception:我得到这个例外:

InvalidCastException: Unable to cast object of type 'System.Collections.Generic.List`1[System.String]' to type 'System.String'. InvalidCastException:无法将“System.Collections.Generic.List`1[System.String]”类型的对象转换为“System.String”类型。

I imagine this is because I'm just trying to throw the strings into PartNumberList without doing .Add().我想这是因为我只是想将字符串放入 PartNumberList 而不执行 .Add()。

I found the problem, through pure chance.我发现了这个问题,纯粹是偶然的。 I don't know the why, but I at least can inform on the what.我不知道为什么,但我至少可以告知什么。

The problem lies in this line:问题在于这一行:

[StringLength(48, MinimumLength = 1, ErrorMessage = "Please enter a part number.")]

I commented it out and suddenly everything is working.我将其注释掉,突然间一切正常。 My guess is that it was trying to convert a string list to a string in order to validate it.我的猜测是它试图将字符串列表转换为字符串以对其进行验证。 I'll have to find another way to validate my list of part numbers.我必须找到另一种方法来验证我的零件编号列表。

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

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