简体   繁体   中英

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. 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. The integers I can grab easily using int lists, but string lists must be populated using .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'."

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.

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. 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). I get this exception:

InvalidCastException: Unable to cast object of type 'System.Collections.Generic.List`1[System.String]' to type 'System.String'.

I imagine this is because I'm just trying to throw the strings into PartNumberList without doing .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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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