sql-serverasp.net-mvcasp.net-mvc-5ado.net-entity-data-model

Get page content dynamically from Sql server database MVC 5


I want to get the info into an li element from SQL Server database in MVC 5 with ADO.NET entity data model.

This is my generated model.edmx

public partial class Entities : DbContext
    {
        public Entities()
            : base("name=Entities")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }

        public virtual DbSet<C__MigrationHistory> C__MigrationHistory { get; set; }

        public virtual DbSet<PackageType> PackageTypes { get; set; }
    }

I have a table named PackageTypes that has 3 columns Id, Name, and PackagesInDeal.

<li class="pricing-title">
                            @Html.DisplayFor(model => @Model.Name) @*Free*@
                        </li>

In this li element I need to get the Name values from the table.

I'm not very familiar on how can I add two functionalities to a controller

public ActionResult Create( int? id ) {
            ViewBag.UserId = new SelectList( db.AspNetUsers, "Id", "Email" );
            ViewBag.PackageTypeId = new SelectList( db.PackageTypes, "Id", "Name" );
            ViewBag.SelectedPackage = id;
            return View();
        }

This is the code contained by the controller, that displays 1,2, 3 or 4 deals depending on the id parameter.

The view has the following code:

 @if ( ( ViewBag.SelectedPackage ?? 0 ) <= 1 ) {
                <div class="col-lg-3 wow zoomIn" style="padding: 0;">
                    <ul class="pricing-plan list-unstyled selected" style="margin: 20px;">

@Html.DisplayFor(model => @Model.Name) @Free@

                        <li class="pricing-desc">
                            Basic package
                            <br />
                        </li>
                        <li class="pricing-price">
                            <span>00</span> / month
                        </li>
                        <li> <i class="fa fa-trophy"></i> aa </li>
                        <li> <i class="fa fa-globe"></i> bb </li>
                        <li> <i class="fa fa-suitcase"></i> cc</li>
                        <li class="selected"> <i class="fa fa-bell"></i>dd </li>
                        <li> <i class="fa fa-database"></i> ee </li>
                        <li> <i class="fa fa-envelope"></i> ff </li>
                        <li>
                            <div style="text-align: right; height: 110px;">
                            </div>
                        </li>
                        <li style="padding-top: 30px;">
                            <a class="btn btn-primary btn-xs" href="@Url.Action( "Create", "Deals", new { deal=Request.QueryString["deal"] } )" rel="1">Buy</a>
                        </li>
                    </ul>
                </div>
            }
@if ( ( ViewBag.SelectedPackage ?? 0 ) <= 2 ) {
                <div class="col-lg-3 wow zoomIn" style="padding: 0;">
                    <ul class="pricing-plan list-unstyled selected" style="margin: 20px;">
                        <li class="pricing-title">
                            Free + Domain
                        </li>
                        <li class="pricing-desc">
                            Basic package
                        </li>
                        <li class="pricing-price">
                            <span>10 </span> / month
                        </li>
                        <li> <i class="fa fa-trophy"></i> aa</li>
                        <li> <i class="fa fa-globe"></i> bb </li>
                        <li> <i class="fa fa-suitcase"></i> cc</li>
                        <li class="selected"> <i class="fa fa-bell"></i> dd</li>
                        <li> <i class="fa fa-database"></i> ee </li>
                        <li> <i class="fa fa-envelope"></i> ff </li>
                        <li>
                            @Html.Partial( "FreeDomain" )
                        </li>

                        <li style="padding-top: 30px;">
                            <a class="btn btn-primary btn-xs" href="@Url.Action( "Create", "Deals", new { deal=Request.QueryString["deal"] } )" rel="1">buy</a>
                        </li>
                    </ul>
                </div>
            }

So how can I retrieve the "Free" , "Free + Domain" and rest of the information in the li from the sql server database tables??


Solution

  • You can always use MVC scaffolding to generate controller / views for a given data model entity. http://www.asp.net/visual-studio/overview/2013/aspnet-scaffolding-overview