Dynamically get page content from Sql MVC 5 server database
I want to get information into an li element from a SQL Server database in MVC 5 with an 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 don't know very well how to add two functionality 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 a code containing a controller that displays 1,2, 3 or 4 trades 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 get "Free", "Free + Domain" and the rest of the information in the li from the SQL Server database tables?
source to share
You can always use MVC scaffolding to create controller / views for a given data model object. http://www.asp.net/visual-studio/overview/2013/aspnet-scaffolding-overview
source to share
So after a lot of research on the internet, I finally got what I wanted. So the controller looks like this:
public ActionResult Create(int? id)
{
List<PackageType> packageTypes = db.PackageTypes.ToList();
ViewBag.PackageTypesName = packageTypes;
ViewBag.SelectedPackage = id;
return View();
}
And the presentation
@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;">
<li class="pricing-title">
**@ViewBag.PackageTypesName[0].Name**
</li>
<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>
}
source to share