Copied to clipboard

Flag this post as spam?

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


  • Sean Hynes 10 posts 142 karma points
    May 23, 2017 @ 11:39
    Sean Hynes
    0

    Contact form - cannot bind source type to model type

    Hi,

    New to Umbraco so built a contact form with a tutorial. When I deployed the site to Azure the first time (with an sdf database sitting in AppData) I got the error "cannot bind source type to model type". I managed to fix it by changing ModelsBuilder.ModelsMode to AppData in the web.config file and it works fine.

    As an exercise I then created a second web app in Azure but moved the database to Azure's sql database. Connection string works, but I get the same error "cannot bind source type to model type" now that I can't seem to fix.

    I'm not sure why I'm getting the error (i.e. what I've coded that causes the issue) but also how to fix it.

    I understand the error is due to my contact form. Here's the code. Any help appreciated

    Html.Action("ShowForm", "ContactSurface")
    

    Contact form partial view

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<EsportsGold.standardwebsite.ContactModel>
    @using (Html.BeginUmbracoForm<EsportsGold.standardwebsite.ContactSurfaceController>("HandleFormPost"))
    
    {
        <div id="contactForm">
            <label for="emailFrom"></label>@Html.TextBoxFor(x => x.Email, new { @class = "emailFrom", placeholder = "Your Email" })
            <br />
            <label for="contactName"></label>@Html.TextBoxFor(x => x.Name, new { @class = "contactName", placeholder = "Name" })
            <br />
            <label for="contactTopic"></label>@Html.TextBoxFor(x => x.Topic, new { @class = "contactTopic", placeholder = "Topic" })
            <br />
            <label for="contactText"></label>
            <br />
            @Html.TextAreaFor(x => x.Message, new { @class = "contactText", placeholder = "Message" })
            <br />
            <input type="submit" value="Submit" />
        </div>
    }
    

    ContactSurfaceController

    using System;
    using System.Collections.Generic;
    using System.Web;
    using System.Web.Mvc;
    using Umbraco.Web.Mvc;
    using System.Linq;
    using System.Xml.XPath;
    using Umbraco.Core.Services;
    
    namespace EsportsGold.standardwebsite
    {
        public class ContactSurfaceController : SurfaceController
        {
    
            public ContactSurfaceController()
            {
            }
    
            public ActionResult ShowForm() {
                ContactModel myModel = new ContactModel();
                List<SelectListItem> ListOfGames = new List<SelectListItem>();
                XPathNodeIterator iterator = umbraco.library.GetPreValues(1089);
                iterator.MoveNext();
                XPathNodeIterator preValues = iterator.Current.SelectChildren("preValue", "");
                while (preValues.MoveNext()){
                    string preValue = preValues.Current.Value;
                    ListOfGames.Add(new SelectListItem {
                        Text = preValue,
                        Value = preValues.Current.GetAttribute("id", "")
                    });
                    myModel.ListOfGames = ListOfGames;
                }
                return PartialView("ContactFormView", myModel);
            }
    
    
            public ActionResult HandleFormPost(ContactModel model){
    
                var newComment = Services.ContentService.CreateContent(model.Name + " - " + DateTime.Now, CurrentPage.Id, "ContactForm");
                newComment.SetValue("umbracoNaviHide", false);
                newComment.SetValue("emailFrom", model.Email);
                newComment.SetValue("contactName", model.Name);
                newComment.SetValue("contactText", model.Message);
                newComment.SetValue("contactTopic", model.Topic);
    
                Services.ContentService.SaveAndPublishWithStatus(newComment);
                return RedirectToCurrentUmbracoPage();
            }
        }    
    }
    
  • Stefano 61 posts 313 karma points c-trib
    May 23, 2017 @ 12:41
    Stefano
    0

    Have you tried specifying the model as simply EsportsGold.standardwebsite.ContactModel ?

    Eg @inherits EsportsGold.standardwebsite.ContactModel in the first line of the view?

  • Sean Hynes 10 posts 142 karma points
    May 23, 2017 @ 17:23
    Sean Hynes
    0

    Hi Stefano

    Thanks for the suggestion. Just tried it.

    If I change the view to @inherits EsportsGold.standardwebsite.ContactModel then i get the following error

    Server Error in '/' Application.

    Compilation Error

    Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

    Compiler Error Message: CS0115: 'ASP.PageViewsPartialsContactFormView_cshtml.Execute()': no suitable method found to override

    Source Error:

    Line 47: } Line 48:
    Line 49: public override void Execute() { Line 50:
    Line 51: #line 3

    "D:\home\site\wwwroot\Views\Partials\ContactFormView.cshtml"

    Which if I understand correctly means the name now doesn't match and can't be executed?

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies