1

I have a edit form which should have checkbox.

My Model for the page is

public class MyModel{
   public string Name {get;set;}
   public List<AdType> AdTypeList { get; set; }
}


Enum AdType{
  [Display(Name = "None")]
        None,

        [Display(Name = "Additional")]
        Additional_Photo,
}

So i have to check the check box with respect to the data coming from the database. And also update should happen if i make changes and submit. To work that way what changes i need to make in my html helper for the checkbox?

foreach (AdType role in Enum.GetValues(typeof(AdType)))
{
    <label>
        <input name="Roles" type="checkbox" value="@role" checked="@(Model != null)" />
        @{var x = EHelper.GetDisplayValue<AdType>(role);}
        @x
    </label>
}

I am new to mvc, please parden if i m doing something stupid.

8
  • checked="true" or checked="false" or checked="anythingAtAll" all mean the same thing - that the checkbox will be checked - its the presence of the attribute that determines if its checked. Commented Oct 18, 2017 at 6:51
  • Suggest you look at this answer for how to generate a strongly typed view for this Commented Oct 18, 2017 at 6:52
  • i want to do it using foreach. but from ur link, i didnt get any solution @stephen Commented Oct 18, 2017 at 7:10
  • You have an enum and you want to select multiple values, therefore it should be [Flags] (having List<AdType> is not the correct approach) In any case, your checkbox has name="Roles" which wont bind to your model. Commented Oct 18, 2017 at 7:14
  • And its not clear why you would want to select both None and Additional_Photo anyway Commented Oct 18, 2017 at 7:16

2 Answers 2

1

you can put if else condition in model to create condition based html control

foreach (AdType role in Enum.GetValues(typeof(AdType)))
{
    <label>
        @if(Model != null)
        {
            <input name="Roles" type="checkbox" value="@role" checked="checked" />
        }
        else
        {
            <input name="Roles" type="checkbox" value="@role" />
        }
        @{var x = EHelper.GetDisplayValue<AdType>(role);}
        @x
    </label>
}
Sign up to request clarification or add additional context in comments.

2 Comments

Stephen mentions, i wnt be able to bind it to model with name=Roles
@hildasonica, It will if you add a property List<AdType> Roles (but as I noted previously, your approach makes no sense)
1
@foreach (AdType role in Enum.GetValues(typeof(AdType)))
                            {
                            <label  class="col-sm-12">
                                <input name="AdTypeList" type="checkbox" value="@role" checked="@(Model != null && Model.AdTypeList!= null && Model.AdTypeList.Any(i => i.HasFlag(role)))" />
                                @{var text = EHelper.GetDisplayValue<AdType>(role);}
                                @text
                            </label>
                            }

1 Comment

@stephen, Now its fine ryt?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.