Hi! I'm having troubles creating a custom controller to pass a model to my view. I have followed this http://our.umbraco.org/documentation/Reference/Mvc/custom-controllers but i cant get it to work. At the moment my viewdata and viewbag doesnt pass any data and my "@Model.name" causes an error witch is "'Umbraco.Web.Models.RenderModel' does not contain a definition for 'name'", even though I have the name-attribute in my model. I don't know what to do to make this work, can someone please help me?
This is my model:
namespace UmbracoAzureFredrik.Models
{
public class Artist
{
public string name { get; set; }
}
}
This is my controller:
namespace UmbracoAzureFredrik.Controllers
{
public class VisaController : Umbraco.Web.Mvc.RenderMvcController
{
public override ActionResult Index(RenderModel model)
{
Artist amodel = new Artist();
amodel.name = "Felix";
ViewData["test"] = "test viewdata";
ViewBag.test = "test viewbag";
return CurrentTemplate(amodel);
}
}
}
My web application must interact with data managed in Touchscreen desktop computers, where web environment is not possible.
I have been trying diferent packages, but all is still a bit too complex for me. It is being difficult to find precise updated information to do simple things...
Consistent documentation has been an issue I agree. I have offered my services to the Umbraco HQ to help build up documentation but I am still waiting for a reply.
Anyhow, my view and controller snippets are below. My model is a class library which I access using a repository pattern.
How you access your model I guess is up to you.
Here's my view, which imherits from Umbraco but I cast it to my model to give me something like a strongly typed view. Because I'm using a custom model and controller and I don't inherit from the RenderModel in my model I have to do this.
Here's my controller, which inherits from Umbraco but I use my own actions and return my custom model. Each action maps to a template and a controller maps to a doctype.
[PluginController("LuminousAcuity")]
public class OrganisationController : RenderMvcController
{
X509Certificate2 _certificate;
string _pFXSerialNo;
string _consumerKey;
string _userAgent;
public OrganisationController()
{
ConfigHelper configHelper = new ConfigHelper("/");
Issue with custom controller
Hi! I'm having troubles creating a custom controller to pass a model to my view. I have followed this http://our.umbraco.org/documentation/Reference/Mvc/custom-controllers but i cant get it to work. At the moment my viewdata and viewbag doesnt pass any data and my "@Model.name" causes an error witch is "'Umbraco.Web.Models.RenderModel' does not contain a definition for 'name'", even though I have the name-attribute in my model. I don't know what to do to make this work, can someone please help me?
This is my model:
This is my controller:
This is my view:
I have a similar issue with custom model and controllers
My error seems to return RenderModel to the view and ignore the custom model I passed in
Hi I am interested in the solution of this issue? Did you get it? Karl
We couldn't make it work so we used javascript instead.
I managed to create a custom model and controller
In the view I used the approach of using a single doc type per controller and multiple templates, with one for each controller action.
I really wanted to use a partial view or a macro but currently that's not supported
You need to change the first line of your view :
@inherits Umbraco.Web.Mvc.UmbracoViewPage<UmbracoAzureFredrik.Models.Artist>
But i think this will break your base layout.
Can I ask why you are using custom view models ?
Maybe you should have a look at the ZBU Models builder. This generates strongly typede models for your document types :
https://github.com/zpqrtbnk/Zbu.ModelsBuilder
And in this video Stéphane explains it in this video : http://uhangout.co.uk/video/NTzb7qQlNzM
Dave
I want to connect with EF Models (in existing database) and render data to Umbraco Views.
Dave, as you explain -sending the models with @inherits UmbracoViewPage<>- fails because the Models must inherit from RenderModel.
Now I am trying to create a ViewModel class that map this. I will explain it later.
I have been through similar issues and managed to get a working package out there using custom model and controllers with a strongly typed view.
I was also asked why I am using the Umbraco pipeline and using a custom model but when building an integration package I kind of need to.
If you want I can did out the source to show you.
I would appreciate that Steve!
My web application must interact with data managed in Touchscreen desktop computers, where web environment is not possible.
I have been trying diferent packages, but all is still a bit too complex for me. It is being difficult to find precise updated information to do simple things...
Consistent documentation has been an issue I agree. I have offered my services to the Umbraco HQ to help build up documentation but I am still waiting for a reply.
Anyhow, my view and controller snippets are below. My model is a class library which I access using a repository pattern.
How you access your model I guess is up to you.
Here's my view, which imherits from Umbraco but I cast it to my model to give me something like a strongly typed view. Because I'm using a custom model and controller and I don't inherit from the RenderModel in my model I have to do this.
@inherits Umbraco.Web.Mvc.UmbracoViewPage<dynamic>
@using Umbraco.Web.Models
@using LuminousAcuity.Model
@{
Layout = null;
List<Organisation> organisations = (List<Organisation>)Model;
RenderModel m = new RenderModel(Umbraco.Content(UmbracoContext.Current.PageId));
IPublishedContent p = m.Content.Children.Where(x => x.DocumentTypeAlias == "Organisation").FirstOrDefault();
//I have to use a querystring format for the URL because Umbraco doesnt do custom route if you have custom model amd controller
}
<h2>@m.Content.GetPropertyValue("title")</h2>
<section>
<article>
<ul>
@{
foreach (Organisation org in organisations)
{
<li><a href="@[email protected]">@org.Name</a>
</li>
}
}
</ul>
</article>
</section>
Here's my controller, which inherits from Umbraco but I use my own actions and return my custom model. Each action maps to a template and a controller maps to a doctype.
[PluginController("LuminousAcuity")]
public class OrganisationController : RenderMvcController
{
X509Certificate2 _certificate;
string _pFXSerialNo;
string _consumerKey;
string _userAgent;
public OrganisationController()
{
ConfigHelper configHelper = new ConfigHelper("/");
_pFXSerialNo = configHelper.GetConfigSetting("PFXCertStoreSerialNo");
_consumerKey = configHelper.GetConfigSetting("ConsumerKey");
_userAgent = configHelper.GetConfigSetting("UserAgent");
_certificate = CertificateHelper.FindBySerialNumber(_pFXSerialNo);
}
//
// GET: /Organisations/
public ActionResult Organisations()
{
using (IOrganisationRepository<Organisation> r = new OrganisationRepository(_userAgent, _consumerKey, _certificate))
{
IEnumerable<Organisation> organisations = r.GetAll();
return CurrentTemplate(organisations);
}
}
}
Thanks, I will test it.
is working on a reply...