1

I'm trying to build a view like following

enter image description here

For Model I added like this

public class UsrViewModel
{
    public IEnumerable<User> SysUser { get; set; }
}

Public class User
{            
     public int ID {get; set:}
     public bool isActive {get; set:}
     public string user {get; set:}
     public string location {get; set:}
     public string usertype {get; set:}
     public string Type {get; set:}
     public int UserOrder {get; set:}
     public bool isVisible {get; set:}
}

then i created view like below, but here I'm getting Following error

 @model ProjectName.ViewModels.UsrViewModel
    @{
    }
    <div class="content-page">

        <div class="content">
            <div class="container">
                <div class="row">
                    <div class="col-xs-12">
                        @using (Html.BeginForm("Users", "Sample", FormMethod.Post))
                        {
                                    <table class="table table-striped table-bordered" id="SysUser ">
                                        <tr>
                                           ...
                                        </tr>
                                        @if (Model != null)
                                        {
                                        for (int i = 0; i < Model.SysUser.Count; i++)
                                            {
                                            <tr>
                                                <td>
                                                    [i]
                                                </td>
                                                ....

                                            </tr>
                                            }
                                        }

                                    </table>

                                </div>
                                <div class="col-sm-offset-1 col-sm-11">
                                    <button type="submit" class="btn btn-purple waves-effect waves-light btn-wd-130">Save</button>
                                    <button type="submit" class="btn btn-default waves-effect waves-light btn-wd-130">Clear</button>
                                </div>                 
                        }
                    </div>
                    <!-- end row -->

                </div> 
            </div>

        </div>
    </div>

Operator '<' cannot be applied to operands of type 'int' and 'method group'

in this line for (int i = 0; i < Model.SysUser.Count; i++)

kez
  • 2,273
  • 9
  • 64
  • 123
  • The model in your view is `SysUser` but the model you have shown is `UsrViewModel` (show the correct code). And what is property `DeviceSensors`? –  Nov 15 '16 at 04:15
  • Sorry for the wrong details, now oky I think – kez Nov 15 '16 at 04:16
  • Re your first edit, assuming the model is really `@model UsrViewModel`, then its because `SysUser` is `IEnumerable` (it needs to be `IList` –  Nov 15 '16 at 04:18
  • 1
    There is no `Count` property in `IEnumerable` but there is a method `Count()` so you could try using that - or convert it to a List as Stephen suggested – Beno Nov 15 '16 at 04:20
  • So you're proposing to chage `IEnumerable<'reUser> SysUser` to `IList SysUser` ? – kez Nov 15 '16 at 04:20
  • Yes, It needs to be `IList` –  Nov 15 '16 at 04:25
  • Alternatively, you need a custom `EditorTemplate` for `User` and just generate the view form controls for each `User` using `@Html.EditorFor(m => m.SysUser)` (no loop required) –  Nov 15 '16 at 04:27
  • @StephenMuecke oky thanks , I have non related question, If My model Null , and I want to show a default row as first row , what should I do ? – kez Nov 15 '16 at 04:35
  • That's the wrong approach - your model should never be `null` because you should have populated it in the controller. And if you have no 'existing' items, and wanted to show a 'default' row, then you populate the collection with one 'default' `User`. But that then suggests you want the ability to add a `User`, which is also the wrong approach. If you want the ability to dynamically add (and delete) `Users` from the collection, refer [this answer](http://stackoverflow.com/questions/28019793/submit-same-partial-view-called-multiple-times-data-to-controller/28081308#28081308) –  Nov 15 '16 at 04:39
  • @StephenMuecke If I'm following , In that question'`s in your answers Option 1, is this correct approach https://bitbucket.org/snippets/Common_Admin/44obp – kez Nov 15 '16 at 05:06
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/128116/discussion-between-stephen-muecke-and-kez). –  Nov 15 '16 at 05:08

2 Answers2

3

try with:

  for (int i = 0; i < Model.SysUser.Count(); i++)
Jaimin Dave
  • 1,224
  • 10
  • 18
  • Unlikely to work since OP has a form for editing, so the property would need to be `IList` to generate form controls in a collection. –  Nov 15 '16 at 04:23
  • i dont get it. IEnumerable has Count() method. and he is using Model.SysUser.Count in for loop condition so getting error: "Operator '<' cannot be applied to operands of type 'int' and 'method group' – Jaimin Dave Nov 15 '16 at 04:31
  • I'm saying that in OP's case, it is not the solution - it will make the error disappear, but the code will then never be able to bind to the model when the form is submitted (it wont solve anything) –  Nov 15 '16 at 04:33
  • `.Count()` works if the data type is a IEnumerable. For a standard list it's simply `.Count` – SendETHToThisAddress Nov 30 '22 at 18:54
0

Have you tried to use ForEach() instead For()? Like this:

ForEach(var thisUser from Model.SysUser){
<tr>
      <td>
          @thisUser.ID
      </td>
      ....

</tr>

}
ihavenokia
  • 147
  • 15