Copied to clipboard

Flag this post as spam?

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


  • Emmanuelle Philippe 10 posts 51 karma points
    Sep 02, 2013 @ 14:52
    Emmanuelle Philippe
    0

    Basic MVC Form: Missing model?

    Hi

    I am quite new to MVC and I trying to add a simple form my website based on the “partial view” example (i.e. http://our.umbraco.org/documentation/reference/mvc/forms/turorial-partial-views).

    I believe I followed the latter pretty closely but for the past couple of days all I am getting is the error “The type or namespace name 'ContactViewModel' could not be found (are you missing a using directive or an assembly reference?)” and I was wondering if someone could tell what I doing wrong?

    My Platform: Windows 8, Umbraco6.1.4 set up on local IIS (not Express), Visual Studio 2012

    My process:

    1. Opened the website in VS 2012 and added folders Models and Controllers (not there initially)

    2. Added new class ContactViewModel.cs to Models folder (see code below)

    Note: When adding the new class to either I got prompted to add it to the App_Code instead but carried on regardless.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Web;

    public class ContactViewModel
    {
        [Required]
        public string Name { get; set; }
        [Required]
        public string Email { get; set; }
        [Required]
        public string Enquiry { get; set; }
    }

    3. Added new class ContactFormSurfaceController.cs to Controllers folder (source code below)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;

    public class ContactFormSurfaceController : Umbraco.Web.Mvc.SurfaceController
    {
    [HttpPost]
        public ActionResult sendEnquiry (ContactViewModel model)
    {
            //model not valid, return current umbraco page

    if (!ModelState.IsValid)
    {
    return CurrentUmbracoPage();
    }

    //if validation passes perform whatever logic
    //Do something...

    //the redirect to 'thank you' page
    return RedirectToUmbracoPage(1065);
    }

    }

    4. Using Umbraco backend, I then added a new Partial View (ContactForm.cshtml) with the following code:

    @model ContactViewModel
    @using (Html.BeginUmbracoForm<ContactFormSurfaceController>("sendEnquiry"))
    {
    @Html.EditorFor(x => Model)
    <input type="submit"/>
    }

    5. Lastly I created a new document type and template to display the partial view

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @{
    Layout = "Root.cshtml";
    }
    @Html.Partial("ContactForm")

     

    Any idea, what I am doing wrong?

Please Sign in or register to post replies

Write your reply to:

Draft