dannyriv
dannyriv

Reputation: 87

Access a Model class from Razor Page .cshtml file

I am trying to create a simple drop-down menu populating it with categories from a local database. I am using .NET Core 2.2. I am following a demo that uses an earlier version of ASP.NET MVC where they use ViewBag to transfer a list from the controller to view. For some reason, ViewBag doesn't work giving me a compiling error of "ViewBag does not exist in the current context". After a little bit of research, I opted to use ViewData which seems to work from the controller but I am having a little bit of trouble displaying the category Name from my model as when I run the page the drop-down options display "Model.Visportfolio.Category" rather than the category Name itself.

I am fairly new to HTML so I tried replicating the code from the demo but I am having trouble accessing the fields of my Category class. I tried using ViewBag as from my research, ViewData and ViewBag are "interchangeable" but no luck.

HTML for where I call my drop-down menu, code does not compile as it says CategoryName and CategoryId does not contain the definition and no accessible extension method

    <form asp-controller="my_dropdown" asp-action="CreatePortfolio" method="post" class="form-horizontal" role="form">
        <div class="form-group">
            <div class="row">
                <div class="alert-danger" asp-validation-summary="ModelOnly"></div>
                <div class="col-xs-12 col-sm-6 col-md-6 col-lg-4">
                    <label asp-for="CategoryName" class="control-label"></label>
                    <select asp-for="CategoryId"
                        class="form-control"
                        asp-items="@(new SelectList(ViewBag.categorylist, "CategoryId", "CategoryName"))">
                    </select>
                    @Html.DropDownList("categorylist",
                        new SelectList((System.Collections.IEnumerable) ViewData["categorylist"]), "Select Category","CategoryName" )
                </div>
            </div>
        </div>
        <div class="form-group">
            <div class="row">
                <div class="col-xs-12 col-sm-6 col-lg-4">
                    <input id="Submit1" type="submit" value="submit" />
                </div>
            </div>
        </div>
    </form>

This is the code behind the html

public void OnGet()
        {

            // ----- Getting Data From Database Using EntityFrameworkCore ----
            categorylist = (from category in _context.Category
                            select category).ToList();

            // ----- Inserting Select Item in List -----
            categorylist.Insert(0, new Category { CategoryId = 0, CategoryName = "Select" });

            // ----- Assigning categorylist to ViewBag.ListOfCategory -----
           //  ViewBag.categortyList = categorylist;
            ViewData["categorylist"] = categorylist;
        }

Model

namespace Visportfolio.Models
{
    [Table("Category")]
    public class Category
    {
        [Key]
        public int CategoryId { get; set; }
        public string CategoryName { get; set; }
    }
}

Upvotes: 0

Views: 1695

Answers (1)

Bosco
Bosco

Reputation: 1554

You are looking for

new SelectList(ViewData[categoryList], "Value", "Text")

SelectList has alot of overloads that you can use see here

So use

@Html.DropDownList("categorylist",
                    new SelectList((System.Collections.IEnumerable) ViewData["categorylist"], "CategoryId", "CategoryName"), "Select Category","CategoryName" )

Upvotes: 1

Related Questions