Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Sean 141 posts 179 karma points
    Apr 11, 2012 @ 13:03
    Sean
    0

    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>
     }

    }

  • Grant Thomas 291 posts 324 karma points
    Apr 11, 2012 @ 15:34
    Grant Thomas
    0

    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

  • Sean 141 posts 179 karma points
    Apr 12, 2012 @ 05:10
    Sean
    0

    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

  • Grant Thomas 291 posts 324 karma points
    Apr 12, 2012 @ 13:17
    Grant Thomas
    0

    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.

  • Sean 141 posts 179 karma points
    Apr 12, 2012 @ 14:12
    Sean
    0

    right. so that is your solution then?

    thanks for your help.

Please Sign in or register to post replies

Write your reply to:

Draft