Hi Folks,
I have created a custom table in a database and I have created a model in Models folder.
InstitutionsList.cs ---Model
using Umbraco.Core.Persistence;
using Umbraco.Core.Persistence.DatabaseAnnotations;
namespace EDEN_MVC_PROJECT.Models
{
[TableName("customInstitutions")]
public class InstitutionsList
{
[Column("id")]
[PrimaryKeyColumn (AutoIncrement = true)]
public int Id { get; set; }
[Column("name")]
public string Name { get; set; }
[Column("state")]
public string State { get; set; }
}
}
I have created a controller - InstitutionsListController.cs
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Umbraco.Core.Persistence;
using Umbraco.Web.Mvc;
using EDEN_MVC_PROJECT.Models;
namespace EDEN_MVC_PROJECT.Controllers
{
public class InstitutionsListController : SurfaceController
{
[HttpGet]
public ActionResult ReadInstitutions()
{
var db = ApplicationContext.DatabaseContext.Database;
IEnumerable<InstitutionsList> institutions = db.Query<InstitutionsList>("Select * from dbo.customInstitutions");
return View("InstitutionsList",institutions);
}
}
}
I am getting error while getting data back from the table into view. It is complaining that
Cannot bind source type Umbraco.Web.Models.RenderModel to model type
System.Collections.Generic.IEnumerable`1[[EDENMVCPROJECT.Models.InstitutionsList,
EDENMVCPROJECT, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null]].
please help me out, I have also tried using partial views but it's giving me same error. I read lot of forums but no one has written how to write html views.
namespace MyControllers
{
public class InstitutionsListController : SurfaceController
{
[ChildActionOnly]
public ActionResult ReadInstitutions()
{
var db = ApplicationContext.DatabaseContext.Database;
IEnumerable<InstitutionsList> institutions =
db.Query<InstitutionsList>("Select * from dbo.customInstitutions");
return View("InstitutionsList", institutions);
}
}
}
Also, rename TableName, now I have an error - 'The table name is not valid. [ Token line number (if known) = 1,Token line offset (if known) = 19,Table name = customInstitutions ]'
It's not partial view, just view that returned by SurfaceController, if you want to rerun full page with Controller, you have to use RenderMvcController, please read more about Umbraco Mvc Controllers: https://our.umbraco.org/documentation/implementation/Controllers/
Get data from custom tables in a Umbraco view
Hi Folks, I have created a custom table in a database and I have created a model in Models folder.
InstitutionsList.cs ---Model
I have created a controller - InstitutionsListController.cs
and I have created a view InstitutionsList.cshtml
I am getting error while getting data back from the table into view. It is complaining that
please help me out, I have also tried using partial views but it's giving me same error. I read lot of forums but no one has written how to write html views.
Hi Sandeep
There are a lot of mistakes in your code, please follow next code examples:
Main view:
Partial view "/Views/InstitutionsList/InstitutionsList.cshtml":
InstitutionsListController :
Also, rename TableName, now I have an error - 'The table name is not valid. [ Token line number (if known) = 1,Token line offset (if known) = 19,Table name = customInstitutions ]'
Thanks,
Alex
Never do like this:
Hi Alex,
I will try out this code but why do we need to create a partial view? Can I get that list directly in the main view? Also what is new {area = ""} ?
Hi Sandeep
Did you find out how to do it?
Ask if something!
Alex
Hi Alex,
I will try this code after I reach office today. I will reply you in couple of hours. Thanks.
Hi Alex,
I tried this code and it's working.
Thanks
It's not partial view, just view that returned by SurfaceController, if you want to rerun full page with Controller, you have to use RenderMvcController, please read more about Umbraco Mvc Controllers: https://our.umbraco.org/documentation/implementation/Controllers/
is working on a reply...