1

I'm new to Umbraco and MVC - can anyone help with the following?

I have a master template (Root.cshtml) which has templates nested below it. In some of these I want to use custom models, however Root.cshtml needs access to the Umbraco helper.

So the nested pages inherit from Umbraco.Web.Mvc.UmbracoViewPage<CustomModel>, whilst Root.cshtml needs to inherit from something more general.

I've tried having my custom models inherit from Umbraco.Web.Models.RenderModel however I keep being told that either my custom model does not contain a constructor that takes 0 arguments or, or if I give it one, that 'Umbraco.Web.Models.RenderModel' does not contain a constructor that takes 0 arguments

What am I doing wrong and how should I accomplish this nesting?

namespace CRuMbraco.Web.Models {
public class RegisterModel : RenderModel {
    [Required]
    [Display(Name = "Customer code")]
    public string CustomerCode { get; set; }

    [Required]
    [DataType(DataType.EmailAddress)]
    [Display(Name = "Email address")]
    public string Email { get; set; }

    [Required]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [DataType(DataType.Password)]
    [Display(Name = "Confirm password")]
    [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
    public string ConfirmPassword { get; set; }

    [DataType(DataType.EmailAddress)]
    [Display(Name = "Confirm email")]
    [Compare("Password", ErrorMessage = "The e-mail and confirmation e-mail do not match.")]
    public string ConfirmEmail { get; set; }

    [Required]
    [Display(Name = "FirstName")]
    public string FirstName { get; set; }

    [Required]
    [Display(Name = "LastName")]
    public string LastName { get; set; }
}

}

4

2 に答える 2

2

All views that use the master template should @inherit from UmbracoTemplatePage.

UmbracoTemplatePage actually inherits from Umbraco.Web.Mvc.UmbracoViewPage<RenderModel> and has two additional properties CurrentPage and Umbraco, the latter as you say is an instance of the UmbracoHelper class.

I'm fairly sure that you cannot replace this with a custom model because an instance of model is determined by the Umbraco routing.

However, if you wish the views to have a custom model you should child actions instead. These will allow you pass you custom objects into the page independently of the RenderModel. If you want something to be available throughout all pages/views regardless of whether they are views, partial views or child actions you could create a global filter that sets data to the Viewbag from the session for example.

There is some documentation here which may help talk you through the Umbraco MVC basics.

于 2013-01-09T13:43:52.523 に答える
0

Actually you can use a UmbracoViewPage like so:

Umbraco.Web.Mvc.UmbracoViewPage<RegisterModel>

To do this your custom model will need to extend the RenderModel, like:

public class RegisterModel: RenderModel 
{
    public RegisterModel(IPublishedContent content, CultureInfo culture)
        : base(content, culture)
    {
    }
    // my properties here
}

You can instantiate you're custom model by hi-jacking the route, see: http://our.umbraco.org/documentation/Reference/Mvc/custom-controllers

So, you'll end up with something like:

public class HomeController : Umbraco.Web.Mvc.RenderMvcController
{
    public override ActionResult Index(RenderModel model)
    {
        var customUmbracoModel = new RegisterModel(model.Content, model.CurrentCulture);

        // do some extra form stuff here

        return base.Index(customUmbracoModel );
    }
}
于 2013-07-09T02:39:44.780 に答える