Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • sandeep 12 posts 112 karma points
    Jun 26, 2017 @ 18:14
    sandeep
    0

    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

    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);
            }
        }
    }
    

    and I have created a view InstitutionsList.cshtml

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @inherits Umbraco.Web.Mvc.UmbracoViewPage<IEnumerable<InstitutionsList>>
    @using EDEN_MVC_PROJECT.Models;
    
    @{ Layout = "EDENFeaturePageLayout.cshtml";
    }
    @{
        foreach (var inst in Model)
        {
            <p>@inst.Name</p>
        }
    }
    

    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.

  • Alex Skrypnyk 6133 posts 23952 karma points MVP 7x admin c-trib
    Jun 26, 2017 @ 22:04
    Alex Skrypnyk
    102

    Hi Sandeep

    There are a lot of mistakes in your code, please follow next code examples:

    Main view:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    
    @{ 
        Layout = "Master.cshtml";
    }
    @{
        @Html.Action("ReadInstitutions", "InstitutionsList", new { area = "" })
    }
    

    Partial view "/Views/InstitutionsList/InstitutionsList.cshtml":

    @model IEnumerable<InstitutionsList>
    
        @{
            foreach (var inst in Model)
            {
                <p>@inst.Name</p>
            }
        }
    

    InstitutionsListController :

    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 ]'

    Thanks,

    Alex

  • Alex Skrypnyk 6133 posts 23952 karma points MVP 7x admin c-trib
    Jun 26, 2017 @ 22:04
    Alex Skrypnyk
    1

    Never do like this:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @inherits Umbraco.Web.Mvc.UmbracoViewPage<IEnumerable<InstitutionsList>>
    
  • sandeep 12 posts 112 karma points
    Jun 26, 2017 @ 22:16
    sandeep
    0

    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 = ""} ?

  • Alex Skrypnyk 6133 posts 23952 karma points MVP 7x admin c-trib
    Jun 27, 2017 @ 09:16
    Alex Skrypnyk
    0

    Hi Sandeep

    Did you find out how to do it?

    Ask if something!

    Alex

  • sandeep 12 posts 112 karma points
    Jun 27, 2017 @ 12:41
    sandeep
    0

    Hi Alex,

    I will try this code after I reach office today. I will reply you in couple of hours. Thanks.

  • sandeep 12 posts 112 karma points
    Jun 30, 2017 @ 18:21
    sandeep
    0

    Hi Alex,

    I tried this code and it's working.

    Thanks

  • Alex Skrypnyk 6133 posts 23952 karma points MVP 7x admin c-trib
    Jun 26, 2017 @ 22:23
    Alex Skrypnyk
    0

    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/

Please Sign in or register to post replies

Write your reply to:

Draft