Click here to Skip to main content
15,880,364 members
Home / Discussions / C#
   

C#

 
GeneralC# 12: New opportunities for auto-foot-wounding Pin
Richard Deeming16-Nov-23 0:24
mveRichard Deeming16-Nov-23 0:24 
GeneralRe: C# 12: New opportunities for auto-foot-wounding Pin
Richard MacCutchan16-Nov-23 1:11
mveRichard MacCutchan16-Nov-23 1:11 
GeneralRe: C# 12: New opportunities for auto-foot-wounding Pin
jschell16-Nov-23 5:18
jschell16-Nov-23 5:18 
QuestionC#, Winforms, Coding Pin
Hoàng Anh Quốc4-Nov-23 0:00
Hoàng Anh Quốc4-Nov-23 0:00 
AnswerRe: C#, Winforms, Coding Pin
Richard MacCutchan4-Nov-23 0:11
mveRichard MacCutchan4-Nov-23 0:11 
AnswerRe: C#, Winforms, Coding Pin
Andre Oosthuizen10-Nov-23 6:28
mveAndre Oosthuizen10-Nov-23 6:28 
QuestionHow to customize the default error message "The value '' is invalid" for dynamically generated form Pin
Fokwa Divine1-Nov-23 4:30
Fokwa Divine1-Nov-23 4:30 
AnswerRe: How to customize the default error message "The value '' is invalid" for dynamically generated form Pin
Richard Deeming1-Nov-23 5:47
mveRichard Deeming1-Nov-23 5:47 
The problem is, you're validating the Units_Service property, not the individual items within it.

And applying a regular expression validation to anything other than a string property makes no sense.

Probably the simplest option would be to create a viewmodel for each item in the list - for example:
C#
public class UnitsServiceViewModel
{
    [Required(ErrorMessage = "You must enter a value.")]
    [RegularExpression("^[0-9]*$", ErrorMessage = "The value must be numeric.")]
    public string Value { get; set; }
}

public class OuterViewModel
{
    public List<UnitsServiceViewModel> Units_Service { get; set; }
    
    internal List<int> Units_Service_Parsed
    {
        get
        {
            if (Units_Service is null) return null;
            
            List<int> value = new(Units_Service.Count);
            foreach (UnitsServiceViewModel vm in Units_Service)
            {
                int.TryParse(vm.Value, out int i);
                value.Add(i);
            }
            
            return value;
        }
        set
        {
            if (value is null)
            {
                Units_Service = null;
            }
            else
            {
                List<UnitsServiceViewModel> list = new(value.Count);
                foreach (int i in value)
                {
                    list.Add(new() { Value = i.ToString() });
                }
                
                Units_Service = list;
            }
        }
    }
}
Razor
@for (int i = 0; i < Model.Units_Service.Count; i++)
{
    <div class="row">
        <div class="col-md-3">
            @Html.LabelFor(model => model.Units_Service[i].Value, Model.Workload_Category_Name[i])
        </div>
        <div class="col-md-4">
            @Html.TextBoxFor(model => model.Units_Service[i].Value, new { style = "width: 15%", MaskedTextBox = "9999", inputmode = "numeric" })
            @Html.ValidationMessageFor(model => model.Units_Service[i].Value)
        </div>
    </div>
}




"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer

GeneralRe: How to customize the default error message "The value '' is invalid" for dynamically generated form Pin
Fokwa Divine1-Nov-23 8:51
Fokwa Divine1-Nov-23 8:51 
AnswerRe: How to customize the default error message "The value '' is invalid" for dynamically generated form Pin
jschell2-Nov-23 4:30
jschell2-Nov-23 4:30 
GeneralRe: How to customize the default error message "The value '' is invalid" for dynamically generated form Pin
Fokwa Divine3-Nov-23 6:34
Fokwa Divine3-Nov-23 6:34 
Questionc Pin
Gorla Bhagyasree31-Oct-23 23:19
Gorla Bhagyasree31-Oct-23 23:19 
AnswerRe: c Pin
Richard Deeming31-Oct-23 23:56
mveRichard Deeming31-Oct-23 23:56 
AnswerRe: c Pin
Pete O'Hanlon1-Nov-23 21:26
mvePete O'Hanlon1-Nov-23 21:26 
QuestionC# How to grab html from selection of web browser control Pin
Tridip Bhattacharjee from Unknown26-Oct-23 11:15
Tridip Bhattacharjee from Unknown26-Oct-23 11:15 
SuggestionRe: C# How to grab html from selection of web browser control Pin
Richard Deeming26-Oct-23 21:47
mveRichard Deeming26-Oct-23 21:47 
GeneralRe: C# How to grab html from selection of web browser control Pin
Tridip Bhattacharjee from Unknown27-Oct-23 3:38
Tridip Bhattacharjee from Unknown27-Oct-23 3:38 
GeneralRe: C# How to grab html from selection of web browser control Pin
Richard Deeming27-Oct-23 3:51
mveRichard Deeming27-Oct-23 3:51 
QuestionADP Enterprise API help Pin
Mike Olmon 202125-Oct-23 12:10
Mike Olmon 202125-Oct-23 12:10 
AnswerRe: ADP Enterprise API help Pin
Richard Deeming25-Oct-23 22:32
mveRichard Deeming25-Oct-23 22:32 
GeneralRe: ADP Enterprise API help Pin
jschell26-Oct-23 4:58
jschell26-Oct-23 4:58 
QuestionIs there a way to Convert binding dataMember? Pin
Geanny Martin24-Oct-23 2:50
Geanny Martin24-Oct-23 2:50 
AnswerRe: Is there a way to Convert binding dataMember? Pin
Alan N24-Oct-23 4:21
Alan N24-Oct-23 4:21 
NewsEvergrowth.AspForMarkDigExtension is out there now... Pin
James McCullough19-Oct-23 15:25
professionalJames McCullough19-Oct-23 15:25 
GeneralRe: Evergrowth.AspForMarkDigExtension is out there now... Pin
OriginalGriff19-Oct-23 18:22
mveOriginalGriff19-Oct-23 18:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.