I have created a controller class and model class.
Within in the model class I have written below method to get values from umbraco.
public void GettingValueFromUmbraco(IPublishedContent content)
{
Name = content.GetProperty("EmpName").Value.ToString();
Email = content.GetProperty("email").Value.ToString();
Mobile = content.GetProperty("mobile").Value.ToString();
}
And I have called this method in Controller class.
public ActionResult Home()
{
UmbracoTest umbracoTest = new UmbracoTest();
umbracoTest.GettingValueFromUmbraco();
return View();
}
From controller we want to pass parameter value. Anybody please help me to pass the right value to the function.
Name = content.GetProperty("EmpName").Value.ToString();
Email = content.GetProperty("email").Value.ToString();
Mobile = content.GetProperty("mobile").Value.ToString();
You already have strongly typed model of page, just render it in Razor:
If you make your controller have the base class of RenderMvcController and pass RenderModel into your method you'll be able to access IPublishedContent from there.
public ActionResult Index(RenderModel umbracoModel)
{
var model = umbracoModel.Content.GetProperty("email");
}
Umbraco works on a naming convention basis so make sure that your Document Type has the same name as your controller.
Exactly am looking this one. As per your suggestion I have amened my code. Please help me to know is this the right way to implemet?
public class HomeController : Umbraco.Web.Mvc.RenderMvcController
{
public ActionResult Home(RenderModel renderModel)
{
UmbracoTest umbracoTest = new UmbracoTest();
umbracoTest.GettingValueFromUmbraco(renderModel);
return View();
}
}
namespace UmbracoStudy.Models
{
public class UmbracoTest
{
public string Name { get; set; }
public string Email { get; set; }
public string Mobile { get; set; }
public void GettingValueFromUmbraco(RenderModel content)
{
Name = content.Content.GetProperty("EmpName").Value.ToString();
Email = content.Content.GetProperty("email").Value.ToString();
Mobile = content.Content.GetProperty("mobile").Value.ToString();
}
}
}
That looks like it would work to me! You're most likely not going to need to pass the entire RenderModel through, so just do renderModel.Content like below.
public class HomeController : Umbraco.Web.Mvc.RenderMvcController
{
public ActionResult Home(RenderModel renderModel)
{
UmbracoTest umbracoTest = new UmbracoTest();
umbracoTest.GettingValueFromUmbraco(renderModel.Content);
return View();
}
}
public class UmbracoTest
{
public string Name { get; set; }
public string Email { get; set; }
public string Mobile { get; set; }
public void GettingValueFromUmbraco(IPublishedContent content)
{
Name = content.GetProperty("EmpName").Value.ToString();
Email = content.GetProperty("email").Value.ToString();
Mobile = content.GetProperty("mobile").Value.ToString();
}
}
public class HomeController : Umbraco.Web.Mvc.RenderMvcController
{
public ActionResult Home(RenderModel renderModel)
{
UmbracoTest umbracoTest = new UmbracoTest(renderModel.Content);
return View("/views/umbracoTest.cshtml", umbracoTest);
}
}
namespace UmbracoStudy.Models
{
public class UmbracoTest
{
public string Name { get; set; }
public string Email { get; set; }
public string Mobile { get; set; }
public UmbracoTest(IPublishedContent content)
{
Name = content.GetProperty("EmpName").Value.ToString();
Email = content.GetProperty("email").Value.ToString();
Mobile = content.GetProperty("mobile").Value.ToString();
}
}
}
Can you show code of your view? Because I don't see why we need controller now.
Below I have copied the code written in Controller, Model and View.
Controller:
public ActionResult Home(RenderModel renderModel)
{
UmbracoTest umbracoTest = new UmbracoTest();
var testModel=umbracoTest.GettingValueFromUmbraco(renderModel.Content);
return View("/Views/Home/Index.cshtml",testModel);
}
Model:
public UmbracoTest GettingValueFromUmbraco(IPublishedContent content)
{
UmbracoTest umb = new UmbracoTest
{
Name = content.GetProperty("EmpName").Value.ToString(),
Email = content.GetProperty("email").Value.ToString(),
Mobile = content.GetProperty("mobile").Value.ToString()
};
return umb;
}
How to get value from umbraco7 using C# code
I have created a controller class and model class. Within in the model class I have written below method to get values from umbraco.
And I have called this method in Controller class.
From controller we want to pass parameter value. Anybody please help me to pass the right value to the function.
Hi Rahulk,
Welcome to our community!!!!
Can you explane why do you need to get data in controller and push it to view?
With Umbraco you can access Umbraco related data directly in view, especially if it's current page data.
Did you look at documentation?
https://github.com/umbraco/UmbracoDocs/tree/master/Tutorials/Creating-Basic-Site
https://our.umbraco.org/documentation/
And of course ask, ask and ask. We are the friendliest community!
Thanks,
Alex
Hi Alex,
Thank you for your quick replay.
I am try to learn how I can get values from umbraco it into Model class. Just for learning umbraco things and C#
Please help me to pass the right parameter for this context.
Thanks Rahul
Hi Rahul,
Umbraco makes strongly typed models for you, just look at https://github.com/zpqrtbnk/Zbu.ModelsBuilder/wiki/Zbu.ModelsBuilder
Thanks,
Alex
You don't need to write such code at all:
You already have strongly typed model of page, just render it in Razor:
Thanks,
Alex
You are right. In razor I can render like that. But how I can access it in C# for doing some business logic in model class.
Thanks Rahul
Hi Rahul,
What exactly logic do you want to do?
Some read or write operations?
Thanks,
Alex
Hi Rahul,
I think you might be looking at RenderModel
https://our.umbraco.org/documentation/reference/routing/custom-controllers
If you make your controller have the base class of RenderMvcController and pass RenderModel into your method you'll be able to access IPublishedContent from there.
Umbraco works on a naming convention basis so make sure that your Document Type has the same name as your controller.
Hope this helps!
Hi Charlie,
Thank you very much.
Exactly am looking this one. As per your suggestion I have amened my code. Please help me to know is this the right way to implemet?
Thanks Rahul
Hi Rahul,
That looks like it would work to me! You're most likely not going to need to pass the entire RenderModel through, so just do renderModel.Content like below.
Charlie
Hi Charlie and Rahul,
Also you can use constructor:
Can you show code of your view? Because I don't see why we need controller now.
Thanks,
Alex
Thank you Charlie.
Thanks & Regards Rahul
Hi Alex,
As of now I didn't write the code for view. Definitely will share you once it is done or start the code.
Thanks & Regards Rahul
Glad to hear, wait for your reply.
Hi Alex, Charlie
Below I have copied the code written in Controller, Model and View.
Controller:
Model:
View:
Thanks & Regards Rahul
is working on a reply...