Sunday 23 February 2014

Umbraco MVC - Partial View and Models


The model item passed into the dictionary is of type 'Umbraco.Web.Models.RenderModel', but this dictionary requires a model item of type 'UmbracoBaseFinal.Models.EnquiryModel'.

That is one annoying error, took me ages to find the solution, I knew what the problem is as it basically spells it out for you, but the solution was more difficult to find.  Anyway here it is.


In your main view you have a call like this to your partial view:


@Html.Partial("EnquiryForm", new EnquiryModel())
OR
 @Html.Partial("~/Views/Partials/EnquiryForm.cshtml", new EnquiryModel())


Which is all nice and simple.

Next we have the partial view, my example here, my Model has the properties for the contact form, but this

@inherits Umbraco.Web.Mvc.UmbracoViewPage

@using (Html.BeginUmbracoForm("HandleContactSubmit", "Enquiry"))
{
Enquiry Form

            @Html.LabelFor(x => Model.ContactName)
            @Html.TextBoxFor(x => Model.ContactName)
            @Html.ValidationMessageFor(x => Model.ContactName)
}

Your model should inherit from RenderModel, for example this is what I am using for my contact form at the moment.

using System.ComponentModel.DataAnnotations;
using System.Globalization;
using Umbraco.Core.Models;
using Umbraco.Web;
using Umbraco.Web.Models;

namespace UmbracoBaseFinal.Models
{
    public class EnquiryModel : RenderModel
    {
        [Required]
        public string
ContactName { get; set; }

        [Required]
        [RegularExpression(@"[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?")]
        public string Email { get; set; }

        [Required]
        [Display(Name = "Enter a comment")]
        public string Comment { get; set; }

        public EnquiryModel() : this(new UmbracoHelper(UmbracoContext.Current).TypedContent(UmbracoContext.Current.PageId)) { }
        public EnquiryModel(IPublishedContent content, CultureInfo culture) : base(content, culture) { }
        public EnquiryModel(IPublishedContent content) : base(content) { }
    }

}

Umbraco 7 - Setting it up in MVC

1. Created a Blank MVC Project(I used 4 in my case)

2. Deleted all files in the solution and remove all references

3. Right click on your solution and Click 'Manage Nuget Packages'

4. Search for Umbraco 7, find the "Umbraco CMS" and install it.

5. Setup the site in IIS as usual then the rest will prompt you through when you start the site.


Happy Playing, hopefully this simplified the process for people who havent set it up before.


Tuesday 18 February 2014

MVC 4 - Populate a DropDownList

I'll keep this simple as most other examples seem not to be :D

For example in your controller:

 public ActionResult MyDropDownList()
        {
            ViewBag.MyType = GetDropDownList();

            return View("");
        }

        public static List GetDropDownList()
        {
            var ls = new List
            {
                new SelectListItem() {Text = "Bob", Value = "0"},
                new SelectListItem() {Text = "Jeff", Value = "1"},
                new SelectListItem() {Text = "Rob", Value = "2"}
            };

            return ls.OrderBy(x => x.Text).ToList();
        }

In your razor script/cshtml:

@Html.DropDownList("MyType", MyController.GetDropDownList())

Hopefully that will help you to get your populated dropdown list :)


Additional

You can put this in your razor script for example:

var amountList = new List();
    for (var i = 0; i <= 10; i++)
    {
        amountList.Add(new SelectListItem { Text = i.ToString(CultureInfo.InvariantCulture), Value = i.ToString(CultureInfo.InvariantCulture) });
    }
    amountList = amountList.ToList();
}


Then for display purposes:



                        @Html.DropDownListFor(model => model.ReceptionRooms,  amountList)
                        @Html.ValidationMessageFor(model => model.ReceptionRooms) 



This will display a dropdown from 1-10, the beauty of this also is that you can reuse the same list for multiple controls.

Sunday 16 February 2014

Umbraco 7 - Cannot Delete Nodes/Content Etc

Umbraco 7 Error occures when you have WebDAV module enabled, never really used it myself, so that's where my knowledge ends :D

Umbraco tries to call using DELETE method, if you check your net tab(using firebug) then you will see the error:


http://umbraco.local/umbraco/UmbracoApi/Content/DeleteById?id=1066

In the response text, you will notice:

IIS 8.0 Detailed Error - 405.0 - Method Not Allowed

The Fix:





Once yous save the web.config, the app pool should recycle anyway and then it should work.

Thursday 6 February 2014

Basic Interfaces

Hi All,

Its been a while since I posted, so I thought I would get back to my blogging now I have a bit more free time. After pondering for many an hour over what interfaces do, it seems I have came to these conclusions:

- They do not implement any functionality
- They are more a scaffolding for which allows them to be easily swapped around with different classes
- They are using to overcome multiple inheritance issues.
- They use polymorphism Here is a basic example which will hopefully make sense: As you will see we have 2 interfaces setup Itest1 and Itest2.

These are called using instance.FirstMethod(); and instance.SecondMethod()




using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Interfaces_Test
{
    class Program : Itest2
    {
        public static void Main()
        {
            Console.WriteLine("Hello Interfaces");
            var itest = new Program();
            Itest2 instance = itest;
            instance.FirstMethod();
            instance.SecondMethod();
           
        }

        void Itest1.FirstMethod()
        {
            Console.WriteLine("First Method Call");
            Console.ReadLine();
        }

        void Itest2.SecondMethod()
        {
            Console.WriteLine("Second Method Call");
            Console.ReadLine();
        }

    }

    internal interface Itest1
    {
        void FirstMethod();
    }

    internal interface Itest2 : Itest1
    {
        void SecondMethod();
      
    }
}

Ok so thats a couple of basics, obviously play around with these to get a better understanding in visual studio. The next step I would suggest is changing the code to something like this. You will notice I have removed the declaration for Itest1, this is not required as its already defined by the Itest2 interface.

NOTE: This code is designed to error, you will receive something along these lines, Error 1 'Interfaces_Test.Program' does not implement interface member 'Interfaces_Test.Itest1.FirstMethod()' c:\users\xxxxxx\documents\visual studio 2012\Projects\Interfaces Test\Interfaces Test\Program.cs 11 11 Interfaces Test

Hopefully you will see what is going on at this point and the penny will drop, so to speak, basically you cant implement Itest2 unless you have implemented Itest1.FirstMethod(), if you comment those lines back in (CTRL + K, CTRL + U key chords) then you will see the program will run successfully.



using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Interfaces_Test
{
    class Program : Itest2
    {
        public static void Main()
        {
            Console.WriteLine("Hello Interfaces");
            var itest = new Program();
            Itest2 instance = itest;
            instance.FirstMethod();
            instance.SecondMethod();
           
        }

        //void Itest1.FirstMethod()
        //{
        //    Console.WriteLine("First Method Call");
        //    Console.ReadLine();
        //}

        void Itest2.SecondMethod()
        {
            Console.WriteLine("Second Method Call");
            Console.ReadLine();
        }

    }

    internal interface Itest1
    {
        void FirstMethod();
    }

    internal interface Itest2 : Itest1
    {
        void SecondMethod();
      
    }
}

I am still learning a lot about interfaces myself, but I thought this could be of interest and hopefully help people who hate those mind boggling examples that give them a headache.

If anything is incorrect here then please let me know and I will correct it accordingly, thanks.

Good Luck :)