0

Yes, I already searched the existing question but none of them have checkboxes in loop and those answers are not working for me.HELP This is my Razor View:-

@model List<propertyMgmt.Models.Property>
<div class="container">
    <div class="row">
      <label>Number of Properties to be Featured:</label>        
      @Html.DropDownList("Sortby", new SelectListItem[]
      {
        new SelectListItem() { Text = "Select Number Of Property", Value = "0" },
        new SelectListItem() { Text = "One", Value = "1" },
        new SelectListItem() { Text = "Two", Value = "2" },
        new SelectListItem() { Text = "Three", Value = "3" },
        new SelectListItem() { Text = "Four", Value = "4" },
        new SelectListItem() { Text = "Five", Value = "5" } },
        new { @onchange = "propertyNumberSelected(this.value)" })
    </div> 
    <div id="propertyList" style="display:none">
      <label>Select properties to be Featured:</label><br />
      <table class="table">
        <tr>
          <th>
            @Html.Label("Property Description")
          </th>
          <th>
            @Html.Label("Property Cost")
          </th>
          <th>
            @Html.Label("Property Image")
          </th>
          <th>
            @Html.Label("Featured??")
          </th>
        </tr>
        @for (int i=0;i<Model.Count)
        {
        <tr>
          <td>
            @Html.DisplayFor(m => m[i].PropertyDescription)
          </td>
          <td>
            @Html.DisplayFor(m => m[i].PropertyCost)
          </td>
          <td>
            <img src="~/Images/@item.PropertyImage" height="40px" width="40px" />
          </td>
          <td>
            @Html.CheckBoxFor(m => m[i].IsFeatured, new {value=item.Id, 
            @onclick = "propertyCheckBoxSelect(this)" })
          </td>
        </tr>
        }
      </table>
    </div>
</div>

The above Dropdown List value should be equal to the checkbox that can be allowed to be checked.After checking other checkboxes should be disabled. I have checked the other questions and their solution but I cant seem to make it work.Any help would be appreciated.

This is my ViewModel:-

public class PropertyViewModel
    {
    public int? Id { get; set; }
    [Required]
    [DataType(DataType.MultilineText)]
    [DisplayName("Property Description")]
    public string PropertyDescription { get; set; }
    [Required]
    [DisplayName("Property Cost")]
    public double PropertyCost { get; set; }      
    [DisplayName("Property Image")]
    public string PropertyImage { get; set; }
    [DisplayName("Property Owner")]
    public string PropertyOwner { get; set; }
    [Required]
    public string Country { get; set; }
    [Required]
    public string State { get; set; }
    [Required]
    public string City { get; set; }
    [Required]
    [DisplayName("Emergency Number")]
    public string EmergencyNumber { get; set; }
    [Required]
    [DisplayName("Fax Number")]
    public string FaxNumber { get; set; }
    [Required]
    [DisplayName("Invest Amount")]
    public double InvestAmount { get; set; }
    [Required]
    [DisplayName("Minimum Invest Amount")]
    public double MinimumInvestAmount { get; set; }
    public DateTime CreatedDate { get; set; }
    public DateTime? ModifiedDate { get; set; }
    public string CreatedBy { get; set; }
    public string ModifiedBy { get; set; }
    public bool IsActive { get; set; }

    public bool IsFeatured { get; set; }//CHECKBOX

    public string Version { get; set; }
}
  • You do realize that your code for `CheckBoxFor()` will not bind to anything so its not clear what the purpose of its is (and your generating invalid html because of duplicate `id` attributes) –  Nov 16 '17 at 06:28
  • And format your code property with indenting so its readable. –  Nov 16 '17 at 06:30
  • Hello again Stephen :) I indented the code. :| Could you elaborate on the not binding and duplicate Id thing? – Nitesh Shrestha Nov 16 '17 at 06:46
  • Each checkbox has `name="item.IsFeatured"` (which has no relationship to your model), has `id="item.IsFeatured" (which is invalid html because there are duplicates) and `value="someNumber"` (which even if you generated the correct `name` attribute could not bind to a `bool` property. –  Nov 16 '17 at 06:49
  • Give you checkboxes class names, handle the `.click()` event - i.e. `$('.className').click(function() {...`, get the value of the dropdownlist and compare it with the number of checkboxes which are selected. but what if a user select say 5 in the dropdownlist, and then checks 5 checkboxes but then selects 2 in the dropdownlist? –  Nov 16 '17 at 06:52
  • Yes but your checkbox will not bind to it! –  Nov 16 '17 at 06:57
  • Is this what you are saying? @Html.CheckBoxFor(modelItem=>item.IsFeatured, new {value=item.Id, @onclick = "propertyCheckBoxSelect(this)",class="SomeClassName" }) – Nitesh Shrestha Nov 16 '17 at 06:59
  • What does not last comment mean. Did you not understand my comments. You have `name` attributes that have no relationship to you model, therefore cannot bind to it, and your overwriting the `value` attribute so it cannot bind to your `bool` property anyway –  Nov 16 '17 at 07:01
  • I go to you page. Instead of just selecting the 2 items I want, you annoy be by first making me select a value from a dropdown. Then I decide I want 4 but now you annoy me even more by making me choose from the dropdown again. (and what happens when I change my mind again and un-check one - are you expecting me to change the dropdown again?) Why are you doing this? –  Nov 16 '17 at 07:17
  • This page is only supposed to be controlled by an Administrator.After checking the checkboxes,only the checked set of data along with their required values should go to a UI page where only the images will be displayed in a carousel..........And that is why Iam trying to do this But I do realize the problem that you have shown Me. – Nitesh Shrestha Nov 16 '17 at 07:41
  • And what is the correct way to use checkbox for.I do not understand the name attribute that you keep telling me about – Nitesh Shrestha Nov 16 '17 at 07:42
  • First read [this answer](http://stackoverflow.com/questions/30094047/html-table-to-ado-net-datatable/30094943#30094943) to understand how your `name` attributes must be for model binding, and then [this one](https://stackoverflow.com/questions/29542107/pass-list-of-checkboxes-into-view-and-pull-out-ienumerable/29554416#29554416) for an example of using a checkbox in a loop –  Nov 16 '17 at 07:45
  • And then just let the administrator select as may checkboxes as they want. Having the driopdownlist is crazy –  Nov 16 '17 at 07:47
  • Ok.Thanks.Will ask again if I still cant find a solution.Thanks Stephen. – Nitesh Shrestha Nov 16 '17 at 07:47
  • Stephen,I studied both the links you suggested and I understand how the foreach loop does not bind my data to anything.I understand that for loop is necessary.But both the links have used a List<> in their viewmodels which is being used in the for loop.I do not have any kind of list in my ViewModel, so how am I supposed to use for loops!....I am going to edit the question and add few more things. Hope you are still awake! – Nitesh Shrestha Nov 16 '17 at 09:50
  • Your model just needs to be `List` instead of `IEnumerable` to use a `for` loop (or yu can leaver it as `IEnumereble` and use an `EditorTemplate` as per the 2nd option in the first link –  Nov 16 '17 at 09:52
  • It makes not difference if the model in the view is a collection that you iterate, or its and object that contains a property which is a collection (as is the case in those answer) - you would just use `@fr (int i = 0; i < Model.Count; i++) { @Html.CheckboxFor(m => m[i].IsSelected) }` –  Nov 16 '17 at 09:54
  • This is what you wanted me to do yes? :- @Html.CheckBoxFor(m=>m[i].IsFeatured, new {@onclick = "propertyCheckBoxSelect(this)"}) I used for loop with Model.Count and @model List – Nitesh Shrestha Nov 16 '17 at 10:32
  • Sorry, I do not understand your comment. –  Nov 16 '17 at 10:34
  • Now: How do I give the Image Source.This is not it :|: – Nitesh Shrestha Nov 16 '17 at 10:34
  • If `src="~/Images/@item.PropertyImage"` worked, then so will that. But comments are not for asking new questions (I'm not going to write your whole application here). If your having other issues, then ask a specific question about it –  Nov 16 '17 at 10:38
  • I have edited the question again when you said You dont understand my comment.Check it once if you would and if that is what you wanted me to do.And about the image... @item used to work but now m[i] does not.But i will ask or search a different question for the image thingy. – Nitesh Shrestha Nov 16 '17 at 10:41
  • You still need to remove the `new { value=item.Id })` from `CheckBoxFor()` method in order for it to bind. (and of course get rid of the dropdownlist forget your idea of disabling checkboxes) –  Nov 16 '17 at 10:44
  • i already removed the value=item.id :D forgot to remove it from here :D The for loop works.Thanks for all the help and all your time. Still have a lot of questions but i will ask another question some other time,if i dont find the needed solution. Thank You Again. – Nitesh Shrestha Nov 16 '17 at 10:55

0 Answers0