Compiler Error Message: CS0266: Cannot implicitly convert type Umbraco 5.1
Hi There,
I'm wanting to iterate through my model. I'm new to MVC and umbraco v5 and I was hoping to get some help with my syntax please?
Cannot implicitly convert type 'namepace.Models.myModel' to 'System.Collections.Generic.IEnumerable<namespace.Models.myModel>'. An explicit conversion exists (are you missing a cast?)
You're creating a single instance of `myModel`, while the declaration of the variable is a collection of `myModel`s (more specfically, `IEnumerable
You could get around this by simply doing:
namespace.Models.myModel someModel = new namespace.Models.myModel();
However, some things woth mentioning here:
- `namespace` is not a very good naming convention considering `namespace` is a reserved word in C# (and in fact is illegal to use as a namespace name, so not sure how you managed that; I think Umbraco mangles the `@using`s a bit though (not entirely sure about this))
- `myModel` need not be fully qualified if you import the namespace with your `using` directive
After making the changes you suggested I'm getting an another exception:
foreach statement cannot operate on avariable of type 'namespace.Model.myModel' does not contain a definition for 'GetEnumerator'.
Is this exception occuring because I have not cast the model propery? If that is the case then how do I cast it?
If I follow this tutorial here (http://shazwazza.com/post/Umbraco-v5-Surface-Controller-Forms.aspx) I get the model to work fine. However I need to render my own form elements in HTML 5. I thought that iterating throught the model would allow me to customise the elements like (@Html.LabelFor(model => someModel.Email): @Html.TextBoxFor(x => someModel.Email, new {type="email", @class = "testModel", @maxlength = "250", @aria_label="this is a test",@role="email"}) which it did in part until I tried to validate it.
Any ideas on how I can get around this ? do you have examples of custom model integration with umbraco?
You can't iterate over an enumerator for that type because it's not a collection (and doesn't offer enumeratable support) - it's just a type, not a type containing types. Just because a type has properties doesn't mean you can 'foreach' them.
In all likeliness, no amount of casting is going to turn the instance of that type into anything usable as a collection. First thing you need to do is understand your type, before anything.
Compiler Error Message: CS0266: Cannot implicitly convert type Umbraco 5.1
Hi There,
I'm wanting to iterate through my model. I'm new to MVC and umbraco v5 and I was hoping to get some help with my syntax please?
Cannot implicitly convert type 'namepace.Models.myModel' to 'System.Collections.Generic.IEnumerable<namespace.Models.myModel>'. An explicit conversion exists (are you missing a cast?)
@inherits RenderViewPage
@using Umbraco.Cms.Web;
@using namespace.Models;
@{ IEnumerable<namespace.Models.myModel> someModel = new namespace.Models.myModel();}
@using(Html.BeginUmbracoForm("HandleFormSubmit", "SurfaceForm"))
{
if (someModel != null){
<ol>
@foreach(var item in someModel){
<li>@item.Name</li>
}
</ol>
}
}
You're creating a single instance of `myModel`, while the declaration of the variable is a collection of `myModel`s (more specfically, `IEnumerable
You could get around this by simply doing:
However, some things woth mentioning here:
- `namespace` is not a very good naming convention considering `namespace` is a reserved word in C# (and in fact is illegal to use as a namespace name, so not sure how you managed that; I think Umbraco mangles the `@using`s a bit though (not entirely sure about this))
- `myModel` need not be fully qualified if you import the namespace with your `using` directive
Hi There,
After making the changes you suggested I'm getting an another exception:
foreach statement cannot operate on avariable of type 'namespace.Model.myModel' does not contain a definition for 'GetEnumerator'.
Is this exception occuring because I have not cast the model propery? If that is the case then how do I cast it?
If I follow this tutorial here (http://shazwazza.com/post/Umbraco-v5-Surface-Controller-Forms.aspx) I get the model to work fine. However I need to render my own form elements in HTML 5. I thought that iterating throught the model would allow me to customise the elements like (@Html.LabelFor(model => someModel.Email): @Html.TextBoxFor(x => someModel.Email, new {type="email", @class = "testModel", @maxlength = "250", @aria_label="this is a test",@role="email"}) which it did in part until I tried to validate it.
Any ideas on how I can get around this ? do you have examples of custom model integration with umbraco?
Sean
You can't iterate over an enumerator for that type because it's not a collection (and doesn't offer enumeratable support) - it's just a type, not a type containing types. Just because a type has properties doesn't mean you can 'foreach' them.
In all likeliness, no amount of casting is going to turn the instance of that type into anything usable as a collection. First thing you need to do is understand your type, before anything.
right. so that is your solution then?
thanks for your help.
is working on a reply...