3

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??

5
  • 1
    You should create a new ViewModel for displaying the data, rather than than using ViewBag Commented May 17, 2015 at 11:59
  • And can I retrieve data from 2 or more tables using the same controller in the same view? Commented May 17, 2015 at 12:43
  • 1
    In the action method you can fetch data from different tables/models and then assign that to the newly created view model dotnet-tricks.com/Tutorial/mvc/… Commented May 17, 2015 at 13:02
  • So basically I need to change my controller, get rid of ViewBag from it, and also change the logic in the view ? Can't there be any workaround to just add what I want and not delete(modify) existing code? Commented May 18, 2015 at 8:12
  • You can always use MVC scaffloding to generate controller / views for a given data model entity. asp.net/visual-studio/overview/2013/aspnet-scaffolding-overview Commented May 18, 2015 at 20:28

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

0

So after long research on the web I finally obtained what I've 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 view

@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>
            }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.