Copied to clipboard

Flag this post as spam?

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


  • Niker 21 posts 91 karma points
    Jun 07, 2019 @ 11:00
    Niker
    0

    Hi, I wanted to send the model to the controller, unfortunately I get the error: "Can not bind source type Umbraco.Web.Models.RenderModel is model type Umbraco12.Models.Home."

    ViewModel:

    namespace Umbraco12.Models
    {
        public class Home : RenderModel
        {
            public Home(IPublishedContent content, CultureInfo culture) : base(content, culture)
            {
            }
    
            public string Topic { get; set; }
        }
    }
    

    Controller:

      public class HomeController : Umbraco.Web.Mvc.RenderMvcController
        {
            // GET: Home
            [HttpGet]
            public ActionResult Home(RenderModel model)
            {
    
                var home = new Home(model.Content, model.CurrentCulture);
                //Do some stuff here, then return the base method
                return View("Home", home);
            }
    
            [HttpPost]
            public ActionResult HomePost(Home model)
            {
    
                //Do some stuff here, then return the base method
                return View("Home", model);
            }
        }
    

    View:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<Umbraco12.Models.Home>
        @using ContentModels = Umbraco.Web.PublishedContentModels;
        @{
        Layout = "Master.cshtml";
        }
        @using (Html.BeginForm("HomePost", "Home", FormMethod.Post))
        {
        <table cellpadding="0" cellspacing="0">
            <tr>
                <th colspan="2" align="center">Person Details</th>
            </tr>
            <tr>
                <td>@Umbraco.Field("topic")</td>
                <td>
                    @Html.TextBoxFor(m => m.Topic)
                </td>
            </tr>
    
            <tr>
                <td></td>
                <td><input type="submit" value="Submit" /></td>
            </tr>
        </table>
        }
    
  • Matt Barlow | jacker.io 164 posts 740 karma points c-trib
    Jun 07, 2019 @ 12:02
    Matt Barlow | jacker.io
    0

    If you want to post data back to the server, you should be using a surface controller, there is a tutorial here:

    https://our.umbraco.com/Documentation/Reference/Templating/Mvc/Forms/tutorial-sensitive-data

  • Niker 21 posts 91 karma points
    Jun 07, 2019 @ 12:05
    Niker
    0

    I used the Surface Controller and now I have an error "No parameterless constructor defined for this object"

    Controller:

       public class HomeSurController : SurfaceController
        {
            // GET: HomeSur
            [HttpPost]
            public ActionResult HomePost(Home model)
            {
    
                //Do some stuff here, then return the base method
                return View("Home", model);
            }
        }
    

    View

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<Umbraco12.Models.Home>
        @using ContentModels = Umbraco.Web.PublishedContentModels;
        @{
        Layout = "Master.cshtml";
        }
       @using (Html.BeginUmbracoForm("HomePost", "HomeSur", System.Web.Mvc.FormMethod.Post, new { @class = "YourclassName" }))
        {
        <table cellpadding="0" cellspacing="0">
            <tr>
                <th colspan="2" align="center">Person Details</th>
            </tr>
            <tr>
                <td>@Umbraco.Field("topic")</td>
                <td>
                    @Html.TextBoxFor(m => m.Topic)
                </td>
            </tr>
    
            <tr>
                <td></td>
                <td><input type="submit" value="Submit" /></td>
            </tr>
        </table>
        }
    
  • Jonathan Distenfeld 105 posts 618 karma points
    Jun 07, 2019 @ 13:08
    Jonathan Distenfeld
    0

    Hello Niker,

    are you sure you are passing a model to your action?

    I assume this error occures because you expect a homemodel as parameter but it's not beeing received.

    Please try passing an id instead and use the id to get the model in the HomePost Action to see if this causes the problem.

    Regards, Jonathan

  • Niker 21 posts 91 karma points
    Jun 07, 2019 @ 13:16
    Niker
    0

    I do not understand what ID and where?

  • Niker 21 posts 91 karma points
    Jun 07, 2019 @ 17:22
    Niker
    0

    Ok, rozwiązałem problem. Wystarczyło dodać taki konstruktor w viewmodelu Home:

    public Home() : base(new UmbracoHelper(UmbracoContext.Current).TypedContent(UmbracoContext.Current.PageId)) { }
    
Please Sign in or register to post replies

Write your reply to:

Draft