Copied to clipboard

Flag this post as spam?

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


  • pat 124 posts 346 karma points
    Nov 15, 2016 @ 11:24
    pat
    0

    partial views from a mvc project only display macroalias and name

    Hi all, I have created MVC project and created a Contact us form in that and set the controller inherits from SurfaceController and create model and view.

    in my view BeginUmbracoForm is underline in red and hover over it says HtmlHelper

    but when I rebuild project I don't get any errors. How do I solve this issue please.

    I have copied the cshtm file in to Views/Macropartials folder and copied dll of my project to bin folder then created a Macro in umbraco (7.5.3) and added macro to a page from RichTextEditor. I cannot see my form in web page it only shows Macroalias:Contactform

    any body know how to do this please.

  • Alex Skrypnyk 6175 posts 24186 karma points MVP 8x admin c-trib
    Nov 15, 2016 @ 11:39
    Alex Skrypnyk
    0

    Hi Pat,

    Can you show what do you inherit in your view? Show please top part of view.

    THanks,

    Alex

  • pat 124 posts 346 karma points
    Nov 15, 2016 @ 11:58
    pat
    0

    I have below in my view I cannot add both inherits and model in view it give me error. After adding Umbraco.Web BeginUmbracoForm problem solved but still form not display on page. log file say Exception: System.InvalidOperationException: The model item passed into the dictionary is of type 'Umbraco.Web.Models.PartialViewMacroModel', but this dictionary requires a model item of type 'iccoukumbracoMVC.Models.ContactFormViewModel'.

    `@using Umbraco.Web @@inherits Umbraco.Web.Mvc.UmbracoViewPage@ @model ContactFormViewModel

    @{ Html.EnableClientValidation(true); Html.EnableUnobtrusiveJavaScript(true); }

    @if (Convert.ToBoolean(TempData["IsSuccessful"])) {

    <h2>Request Sent</h2>
    <p>Thank you for contacting us, we will get back to you soon.</p>
    

    } else { using (Html.BeginUmbracoForm

        @Html.ValidationSummary(true)
        @Html.AntiForgeryToken()
    
        @Html.TextBoxFor(model => model.Name, new { placeholder = "Your Name" })
        @Html.ValidationMessageFor(model => model.Name )
    
        @Html.TextBoxFor(model => model.Email, new { placeholder = "Email" })
        @Html.ValidationMessageFor(model => model.Email)
    
        @Html.TextBoxFor(model => model.Telephone , new { placeholder="Telephone"})
        @Html.ValidationMessageFor(model => model.Telephone)
    
        @Html.TextAreaFor(model=> model.Message , new { placeholder = "Message"})
        @Html.ValidationMessageFor(model => model.Message)
        <input type="submit" value="Submit"/>
    }
    

    }`

  • Alex Skrypnyk 6175 posts 24186 karma points MVP 8x admin c-trib
    Nov 15, 2016 @ 12:05
    Alex Skrypnyk
    0

    Hi Pat,

    Try to remove

    @inherits Umbraco.Web.Mvc.UmbracoViewPage
    

    Leave like that:

    @using Umbraco.Web
    @model CommentViewModel
    
    @{ Html.EnableClientValidation(true); Html.EnableUnobtrusiveJavaScript(true); }
    

    Thanks,

    Alex

  • pat 124 posts 346 karma points
    Nov 15, 2016 @ 12:22
    pat
    0

    Hi Alex,

    still get error Error loading Partial View (file: ~/Views/MacroPartials/ContactForm.cshtml). Exception: System.InvalidOperationException: The model item passed into the dictionary is of type 'Umbraco.Web.Models.PartialViewMacroModel', but this dictionary requires a model item of type 'iccoukumbracoMVC.Models.ContactFormViewModel'.

    I have copied my dll file into bin folder of website hope that is correct ?

    I refereed this article https://creativewebspecialist.co.uk/2013/07/22/umbraco-mvc-what-on-earth-is-a-surface-controller/

  • Alex Skrypnyk 6175 posts 24186 karma points MVP 8x admin c-trib
    Nov 15, 2016 @ 12:37
    Alex Skrypnyk
    0

    Hi Pat,

    Can you show how did you call you partial?

    Maybe you just pass wrong model?

    THanks,

    Alex

  • pat 124 posts 346 karma points
    Nov 15, 2016 @ 12:48
    pat
    0

    Hi thanks for helping

    using System.ComponentModel.DataAnnotations;
    
    namespace myumbracoMVC.Models
    {
        public class ContactFormViewModel
        {
            [Required]
            public string Name { get; set; }
            [Required]
            [EmailAddress]
            public string Email { get; set; }
            [Required]
            public string Telephone { get; set; }
            [Required]
            public string Message { get; set; }
    
        }
    }
    

    and in my controller.cs file

    using System.Web.Mvc;
    using Umbraco.Web.Mvc;
    using System.Net.Mail;
    
    namespace myumbracoMVC.Controllers
    {
        public class ContactFormSurfaceController : SurfaceController
        {
            public ActionResult RenderContactForm()
            {
                return PartialView("ContactForm", new ContactFormViewModel());
            }
    
    
    
            [System.Web.Mvc.HttpPost]
            [ValidateAntiForgeryToken]
            public ActionResult SubmitContactForm(ContactFormViewModel model )
            {
                if(!ModelState.IsValid)
                {
                    return CurrentUmbracoPage();
                }
                MailMessage emailmsg = new MailMessage("email@domain", "emailxx@domain");
                emailmsg.Subject = "WebSite contact form request";
                emailmsg.Body ="Name : " + model.Name  + Environment.NewLine + " Email : " + model.Email + " Telephone : " + model.Telephone +   model.Message;
                try
                {
                    SmtpClient smtp = new SmtpClient();
                    smtp.Send(emailmsg);
                }
                catch (Exception ex)
                {
    
                }
                TempData["IsSuccessful"] = true;
                return RedirectToCurrentUmbracoPage();
            }
    
        }
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft