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.

LenglBoy
  • 1,451
  • 1
  • 10
  • 24
hilda_sonica_vish
  • 727
  • 3
  • 10
  • 31
  • `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. –  Oct 18 '17 at 06:51
  • Suggest you look at [this answer](https://stackoverflow.com/questions/37990786/how-to-reduce-code-duplication-in-asp-net-mvc-view-when-working-with-flags-enum/37991402#37991402) for how to generate a strongly typed view for this –  Oct 18 '17 at 06:52
  • i want to do it using foreach. but from ur link, i didnt get any solution @stephen – hilda_sonica_vish Oct 18 '17 at 07:10
  • You have an `enum` and you want to select multiple values, therefore it should be `[Flags]` (having `List` is not the correct approach) In any case, your checkbox has `name="Roles"` which wont bind to your model. –  Oct 18 '17 at 07:14
  • And its not clear why you would want to select both `None` and `Additional_Photo` anyway –  Oct 18 '17 at 07:16
  • i might add more values in enums, i want multi select, what change i shoul make to bind it to model? – hilda_sonica_vish Oct 18 '17 at 07:20
  • with foreach , i wont be able to bind to model? – hilda_sonica_vish Oct 18 '17 at 07:20
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/156976/discussion-between-stephen-muecke-and-hilda-sonica). –  Oct 18 '17 at 07:21

2 Answers2

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>
}
Dhaval Pankhaniya
  • 1,996
  • 1
  • 15
  • 26
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>
                            }
hilda_sonica_vish
  • 727
  • 3
  • 10
  • 31