我想通过ADO.NET实体数据模型将信息从MVC 5中的SQL Server数据库中获取到li元素。

这是我生成的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; }
    }

我有一个名为PackageTypes的表,它有3列Id,Name和PackagesInDeal。

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

在这个li元素中,我需要从表中获取Name值。

我不太熟悉如何向控制器添加两个功能

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

这是控制器包含的代码,根据id参数显示1,2,3或4个交易。

该视图具有以下代码:

 @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.Name)@ @

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

那么如何从sql server数据库表中检索li中的“Free”,“Free + Domain”和其他信息呢?

===============>>#1 票数:2 已采纳

您始终可以使用MVC脚手架为给定的数据模型实体生成控制器/视图。 http://www.asp.net/visual-studio/overview/2013/aspnet-scaffolding-overview

===============>>#2 票数:0

因此,经过长期的网络研究,我终于获得了我想要的东西。 所以控制器看起来像这样

public ActionResult Create(int? id)
            {
            List<PackageType> packageTypes = db.PackageTypes.ToList();
            ViewBag.PackageTypesName = packageTypes;
            ViewBag.SelectedPackage = id;
            return 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>
            }

  ask by Andrei Maieras translate from so

未解决问题?本站智能推荐: