0

How to dynamically checked checkbox based on database value which are present or not null

@foreach (abc.Models.xyz entry in entrydetails)
        {
        <div style="height:auto !important;max-height:160px; overflow-y:auto;width:79%;">   
        @{
           List<abc.Models.Singer> singerobjlist = abc.Service.Class1.singerlist();        
            }   

           @foreach (abc.Models.Singer singerobj in singerobjlist)
           {    
          <div class="boxcheck">
          <input type="checkbox" value='@singerobj.Singer1' name="Singer1l"/>
         <label>@singerobj.Singer1</label>
          </div>
           }

         </div>
        }

Dear All... I have declared foreach loop at the top which contain all entry in database corresponding to singer1-singer7. Now below foreach loop I have declared another foreach loop which will dynamically display all singer name in view page. I want to do that all those singer whose not null in outer foreach loop that singer name must be checked by inner foreach loop.

2 Answers 2

2

Try this

<input type="checkbox" value='@singerobj.Singer1' name="Singer1l"
@(singerobj.Singer1!=null ? Html.Raw(" checked=\"checked\"") : null)
/>
Sign up to request clarification or add additional context in comments.

1 Comment

Works fine, tanks
0

Use this

<div class="boxcheck">
   @Html.CheckBoxFor(m=>m.singerobj.Singer1)
  <label>@singerobj.Singer1</label>
</div>

1 Comment

my all singer name should be displayed which are checked or unchecked... please help to us

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.