Copied to clipboard

Flag this post as spam?

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


  • Niker 16 posts 86 karma points
    19 days ago
    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 130 posts 531 karma points
    19 days ago
    Matt Barlow
    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 16 posts 86 karma points
    19 days ago
    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 44 posts 234 karma points
    19 days ago
    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 16 posts 86 karma points
    19 days ago
    Niker
    0

    I do not understand what ID and where?

  • Niker 16 posts 86 karma points
    19 days ago
    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