0

I am using asp.net MVC 4 and razor syntax. I have a custom model, imagine this:

namespace MyProject.SampleModel
{
    public class MyCustomModel
    {
        public bool MyBoolean1 { get; set; }
        public bool MyBoolean2 { get; set; }
    }
}

so from the view, in the header, I do:

@model MyProject.SampleModel.MyCustomModel

(...)
@Html.CheckBoxFor(m => m.MyBoolean1 ) <---- ERROR HERE: MyBoolean1 is not recognized
(...)

But in line CheckBoxFor, in the lambda, my MyBoolean1 is not recognized. Why?

First attempt:

It seems like replacing

@model MyProject.SampleModel.MyCustomModel 

with:

@using MyProject.SampleModel 

there are no compilation errors but in runtime an error is raised:

Compiler Error Message: CS1963: An expression tree may not contain a dynamic operation
tereško
  • 58,060
  • 25
  • 98
  • 150
Willy
  • 9,848
  • 22
  • 141
  • 284

1 Answers1

1

replace

@using MyProject.SampleModel 

with

@model MyProject.SampleModel.MyCustomModel

As the error states, lambda expressions do not support dynamic models. By defining the model you are strongly typing it.

Using your faulty statement, you are referencing just a namespace, which will per se work, if you elimenate your lambda expressions from the code, but you won't have a strongly typed view any longer.

Kind regards

//EDIT:

We found out, that the core of the question is: how to use 2(3, 4, 5 ... n) models in one view.

Lets assume you have model 1:

public class MyCustomModel
{
    public bool MyBoolean1 { get; set; }
    public bool MyBoolean2 { get; set; }
}

And model 2:

public class SecondModel
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

You need to create a third class which combines these two models.

public class ExampleViewModel
{
    public MyCustomModel BoolValues { get; set; }
    public SecondModel Names { get; set; }
}

Now you just return an ExampleViewModel to your view in your controller action and use

@model MyProject.SampleModel.ExampleViewModel

and access your properties in this way:

@foreach (var item in Model.BoolValues)
{
    <div>@item.Myboolean1</div>
}

@foreach (var item in Model.Names)
{
    <div>@item.FistName</div>
}
Marco
  • 22,856
  • 9
  • 75
  • 124
  • On runtime below error is raised by doing what you have said:Compiler Error Message: CS0118: 'MyProject.SampleModel' is a 'namespace' but is used like a 'type' – Willy Sep 20 '13 at 19:49
  • I didn't realise you were referencing a namespace instead of a class and just saw the `using` vs. `model` error. I edited my answer for better understanding. – Marco Sep 20 '13 at 19:54
  • If I delete the line the namespace and the import to the view as @model MyCustomModel then works. The problem here is that imagine I have another custom model and I want to import as well: the problem is that you cannot import more than one model...and now I have this problem. – Willy Sep 20 '13 at 19:58
  • Yes you can. What you need is a `ViewModel` Just google it or use one of the questions here on SO as a reference http://stackoverflow.com/questions/16548376/asp-net-mvc-how-exactly-to-use-view-models. I will edit my answer to give a quick heads up for you – Marco Sep 20 '13 at 20:01
  • I have tried yet as well but without success. Lambdas in lines @Html.CheckBoxFor(m => m.MyBoolean1) are not working. Also I have another problem, in my script block I have something like @Model.SecondModel for example and it is not working. – Willy Sep 20 '13 at 20:23
  • Ok, for lambdas @Html.CheckBoxFor(m => m.BoolValues.MyBoolean1) works but now I have problems in the script block @Model.SecondModel – Willy Sep 20 '13 at 20:28
  • 1
    Do NOT use 2 models seperatly in one view. Build the ViewModel like I explained and use the ViewModel. – Marco Sep 20 '13 at 20:29
  • And If I need to access to a custom model, for example, SecondModel.FirstName in the script zone, how to do this? – Willy Sep 20 '13 at 21:15
  • I have explained the basics in my answer, even on how to access FirstName. Only in this case it is called `Names.FirstName` because I have instantiated it this way. – Marco Sep 20 '13 at 21:29
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/37759/discussion-between-serv-and-user1624552) – Marco Sep 20 '13 at 22:32