Copied to clipboard

Flag this post as spam?

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


  • Sonja 133 posts 621 karma points
    Sep 11, 2018 @ 06:40
    Sonja
    0

    getting a runtime error

    Hi,

    I have partial view

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<UmbracoMVC0.Models.CommentViewModel>
    
    @using Umbraco.Web.Mvc
    @using UmbracoMVC1.Controllers
    @using Umbraco.Web
    
    @Model UmbracoMVC0.Models.CommentViewModel
    
    @using (Html.BeginUmbracoForm<BlogPostSurfaceController>("CreateComment"))
    {
        @Html.EditorFor(x => Model)
        <input type="submit" />
    }
    

    When I run it I get runtime error

    Umbraco.Web.Mvc.ModelBindingException: 'Cannot bind source type Umbraco.Web.Models.RenderModel`1[[Umbraco.Web.PublishedContentModels.Home, AppWeball.generated.cs.8f9494c4.moxirk3w, Version=0.0.0.1, Culture=neutral, PublicKeyToken=null]] to model type UmbracoMVC0.Models.CommentViewModel.'

    the model is:

    public class CommentViewModel
        {
            //[Required]
            public string Name { get; set; }
    
            //[Required]
            public string Email { get; set; }
    
            //[Required]
            [Display(Name = "Enter a comment")]
            public string Comment { get; set; }
    
             }
    

    How do I make this work? please advice

  • Alex Skrypnyk 6150 posts 24110 karma points MVP 8x admin c-trib
    Sep 11, 2018 @ 11:50
    Alex Skrypnyk
    0

    Hi Sonja

    It looks like you are running this code on a Home page. So you have to use "Umbraco.Web.PublishedContentModels.Home" as a model:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<Umbraco.Web.PublishedContentModels.Home> 
    

    Alex

  • Sonja 133 posts 621 karma points
    Sep 11, 2018 @ 12:23
    Sonja
    0

    Thanks Alex, but is there possibility somehow to use my custom model?CommentViewModel

  • Nik 1612 posts 7258 karma points MVP 7x c-trib
    Sep 11, 2018 @ 13:20
    Nik
    0

    Hi Sonja,

    How are you calling your partial view?

    Thanks,

    Nik

  • Alex Skrypnyk 6150 posts 24110 karma points MVP 8x admin c-trib
    Sep 11, 2018 @ 13:58
    Alex Skrypnyk
    0

    You are welcome, Sonja. You can use CommentViewModel of course, but you have to pass a model of CommentViewModel type to your partial. Do not use Umbraco helper for rendering partial view.

  • Sonja 133 posts 621 karma points
    Sep 12, 2018 @ 04:55
    Sonja
    0

    Nik this is how I'm calling the partial view in the home page

    <section >
        @Html.Partial("BlogCommentForm")
    </section>
    
  • Sonja 133 posts 621 karma points
    Sep 12, 2018 @ 05:06
    Sonja
    0

    Alex I've tried this in the partial view

    @using UmbracoMVC0.Controllers
    @using Umbraco.Web
    
    @Model UmbracoMVC0.Models.CommentViewModel
    
    @using (Html.BeginUmbracoForm<BlogPostSurfaceController>("CreateComment"))
    {
        @Html.EditorFor(x => Model)
        <input type="submit" />
    }
    

    and I'm getting displayed on the web page

    Umbraco.Web.Models.RenderModel`1[Umbraco.Web.PublishedContentModels.Home] UmbracoMVC0.Models.CommentViewModel CurrentCulture

    and there is a textbox with en-us and button Submit Query and when I press the button I'm getting

      Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
    
    Exception Details: System.MissingMethodException: No parameterless constructor defined for this object.
    

    Can you please tell me where shall i define this missing parmeter

  • Alex Skrypnyk 6150 posts 24110 karma points MVP 8x admin c-trib
    Sep 12, 2018 @ 08:34
    Alex Skrypnyk
    100

    Hi Sonja,

    Try this way to call partial:

    <section >
        @Html.Partial("BlogCommentForm", new CommentViewModel())
    </section>
    
  • Sonja 133 posts 621 karma points
    Sep 12, 2018 @ 12:09
    Sonja
    0

    Hi Alex It is displaying the form just befor e the form it is displaying the model name and I don't need the model name. Where should I look for the problem? how do i fix this? Thanks for the form fields

  • Sonja 133 posts 621 karma points
    Sep 12, 2018 @ 12:43
    Sonja
    0

    The model name displayed twice I solved this with removing @Model UmbracoMVC0.Models.CommentViewModel

    now it is ok. Thanks

  • Alex Skrypnyk 6150 posts 24110 karma points MVP 8x admin c-trib
    Sep 12, 2018 @ 12:48
    Alex Skrypnyk
    0

    Hi Sonja

    Thanks for getting back!! Great that this issue has been solved. Have a great day!

    Alex Skrypnyk

Please Sign in or register to post replies

Write your reply to:

Draft