I'm writing a website in Umbraco 4.11.0 using MVC and I'm trying to add a form to a page, so far I've learnt almost everything from the MVC documentation on this page: http://our.umbraco.org/documentation/Reference/Mvc.
To create the form I've assumed that I need to use a custom controller in order to handle everything when submitting the form etc, I've used this page to help me in that respect: http://our.umbraco.org/documentation/Reference/Mvc/custom-controllers but I keep recieving an error when loading the page, the error is
The model item passed into the dictionary is of type 'Umbraco.Web.Models.RenderModel', but this dictionary requires a model item of type 'MyMvcApp.Models.StatusViewModel'.
using MyMvcApp.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MyMvcApp.Controllers
{
public class StatusController : Umbraco.Web.Mvc.RenderMvcController
{
//
// GET: /Status/
[HttpGet]
public PartialViewResult Index()
{
return new PartialViewResult();
}
[HttpPost]
[ValidateAntiForgeryToken(Salt="StatusViewForm")]
public PartialViewResult Index(StatusViewModel model)
{
if (ModelState.IsValid)
{
}
return new PartialViewResult();
}
}
}
And my model:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace MyMvcApp.Models
{
public class StatusViewModel
{
[Required]
public string StatusText { get; set; }
public IEnumerable<HttpPostedFileBase> Files { get; set; }
}
}
I've put my partial into a folder called 'Status' within the 'Views' folder as from looking at the documentation it tells me that the controler requires it to be there?
Thanks in advance for any help you can give me, much appretiated!
I've got the same error. It looks like the controller is not being hit at all. I've already made sure the controller derives from Umbraco.Web.Mvc.RenderMvcController
Also included the dll from MVC containing the controller in the Umbraco Framework, but no luck.
HI Luke, unless the problem is because I'm not using App_Code, but a separate compiled assembly, your solution on your page doesn't work for me as the model isn't the right type - Umbraco.Web.Models.RenderModel doesn't have the properties from the model and so exceptions are generated.
Could I just ask if you've tried it in the app_code folder? If it works there then you have something wrong with your assembly. Also when you're callign the form, are you providing the model you want to use with it like this: `@Html.Partial("CommentForm", newLukeAlderton.CommentFormModel())`?
Edit: I'm using Umbraco 4.11.x but it should work in 4.10.x and upwards.
MVC Using a custom controller and viewmodel
I'm writing a website in Umbraco 4.11.0 using MVC and I'm trying to add a form to a page, so far I've learnt almost everything from the MVC documentation on this page: http://our.umbraco.org/documentation/Reference/Mvc.
To create the form I've assumed that I need to use a custom controller in order to handle everything when submitting the form etc, I've used this page to help me in that respect: http://our.umbraco.org/documentation/Reference/Mvc/custom-controllers but I keep recieving an error when loading the page, the error is
The model item passed into the dictionary is of type 'Umbraco.Web.Models.RenderModel', but this dictionary requires a model item of type 'MyMvcApp.Models.StatusViewModel'.
I'm using
on the partial view and I've also tried using
but it gives the same error!
My Partial contains the following code:
And my Controller:
And my model:
I've got the same error. It looks like the controller is not being hit at all. I've already made sure the controller derives from Umbraco.Web.Mvc.RenderMvcController
Also included the dll from MVC containing the controller in the Umbraco Framework, but no luck.
I found out how to get it working and made a blog post about this topic here: http://lukealderton.co.uk/blog/blog/2012/december/mvc-forms-in-umbraco/
Thanks for sharing :)
No problem, hope it helped!
HI Luke, unless the problem is because I'm not using App_Code, but a separate compiled assembly, your solution on your page doesn't work for me as the model isn't the right type - Umbraco.Web.Models.RenderModel doesn't have the properties from the model and so exceptions are generated.
Where version of Umbraco are you using?
Could I just ask if you've tried it in the app_code folder? If it works there then you have something wrong with your assembly. Also when you're callign the form, are you providing the model you want to use with it like this: `@Html.Partial("CommentForm", newLukeAlderton.CommentFormModel())`?
Edit:
I'm using Umbraco 4.11.x but it should work in 4.10.x and upwards.
Luke's solution on the Wayback Machine: https://web.archive.org/web/20130209074430/http://lukealderton.co.uk/blog/blog/2012/december/mvc-forms-in-umbraco/
Or the actual page: http://lukealderton.com/blog/posts/2012/december/mvc-forms-in-umbraco/ ;)
is working on a reply...