logo
down
shadow

MVC4 Enum Binding


MVC4 Enum Binding

By : Jorge Gonçalves
Date : November 28 2020, 01:01 AM
This might help you You may create a view model for your view with VIEW SPECIFIC Properties.Have a string property for your state. set the value of that in your action and use that.
code :


Share : facebook icon twitter icon
C# Enum Flag - two ways binding - enum - class -enum

C# Enum Flag - two ways binding - enum - class -enum


By : user9769
Date : March 29 2020, 07:55 AM
will help you You could make the Week have a property or field of type WeekDays that keeps track of which flags are active. And then all your boolean properties just check against that enum value, and on set update it correctly. This allows you to do this:
code :
Week w = new Week();
w.Monday = true;
Console.WriteLine(w.Days); // Monday
w.Tuesday = true;
w.Wednesday = true;
Console.WriteLine(w.Days); // Monday, Tuesday, Wednesday
public class Week
{
    public WeekDays Days
    { get; set; }

    public bool Monday
    {
        get { return (Days & WeekDays.Monday) != 0; }
        set { SetDaysFlag(WeekDays.Monday, value); }
    }

    public bool Tuesday
    {
        get { return (Days & WeekDays.Tuesday) != 0; }
        set { SetDaysFlag(WeekDays.Tuesday, value); }
    }

    public bool Wednesday
    {
        get { return (Days & WeekDays.Wednesday) != 0; }
        set { SetDaysFlag(WeekDays.Wednesday, value); }
    }

    public bool Thursday
    {
        get { return (Days & WeekDays.Thursday) != 0; }
        set { SetDaysFlag(WeekDays.Thursday, value); }
    }

    public bool Friday
    {
        get { return (Days & WeekDays.Friday) != 0; }
        set { SetDaysFlag(WeekDays.Friday, value); }
    }

    public bool Saturday
    {
        get { return (Days & WeekDays.Saturday) != 0; }
        set { SetDaysFlag(WeekDays.Saturday, value); }
    }

    public bool Sunday
    {
        get { return (Days & WeekDays.Sunday) != 0; }
        set { SetDaysFlag(WeekDays.Sunday, value); }
    }

    /// <summary>
    /// Set or unset the flag on the <c>Days</c> property.
    /// </summary>
    /// <param name="flag">The flag to set or unset.</param>
    /// <param name="state">True when the flag should be set, or false when it should be removed.</param>
    private void SetDaysFlag (WeekDays flag, bool state)
    {
        if (state)
            Days |= flag;
        else
            Days &= ~flag;
    }
}
How to handle enum as string binding failure when enum value does not parse

How to handle enum as string binding failure when enum value does not parse


By : Prasun
Date : March 29 2020, 07:55 AM
Does that help Following up on Simply Ged's answer above, AFAICS, this actually cannot be done as the model binding exceptions are swallowed (https://github.com/aspnet/Mvc/issues/3898)
ModelState contains model binding errors and you can get some information out of that. As we currently use only JSON serialization, I ended up implementing a filter to check the ModelState errors for JsonSerializationException. It is not perfect though as eg. to get the requested value (that failed the binding) out of the JsonSerializationException you need to parse the inner exception message.
Binding SubState enum based on MainState enum

Binding SubState enum based on MainState enum


By : user1953084
Date : March 29 2020, 07:55 AM
wish help you to fix your issue Like @ASh already said in the Comments, i suggest you to create a Property which formats the whole State.
Property:
code :
public string CurrentStateText
{
    get
    {
        if (CurrentState.Equals(STATE.IDLE))
            return CurrentState;

        else if (CurrentState.Equals(STATE.CALIBRATE))
            return String.Format("{0} - {1}", CurrentState, CalibrateTrackState);

        else if (CurrentState.Equals(STATE.GO_HOME))
            return String.Format("{0} - {1}", CurrentState, HomeingState);

        else return "Invalid STATE!";
    }
}
<Label Content="{Binding CurrentStateText}" />
set { _currentState = value; OnPropertyChanged("CurrentState"); OnPropertyChanged("CurrentStateText"); }
set { _calibrateTrackState = value; OnPropertyChanged("CalibrateTrackState"); OnPropertyChanged("CurrentStateText"); }
etc.
Mvc binding issue from json to enum (customexception from int to enum)

Mvc binding issue from json to enum (customexception from int to enum)


By : Virendra Kumar
Date : March 29 2020, 07:55 AM
Does that help Yes, I got the same problem. The weird problem is that if you sent back:
Enum to Checkboxes in the Model C# MVC4

Enum to Checkboxes in the Model C# MVC4


By : Chanda Nsofu
Date : March 29 2020, 07:55 AM
Does that help Here is the small prototype I did for you with Enums and CheckBoxes and its validation.
Let your ENUM be -
shadow
Privacy Policy - Terms - Contact Us © ourworld-yourmove.org