0

在我的 mvc3 应用程序中,我想为来自数据库的数据填充下拉列表,这里使用的是实体框架和数据库优先方法,所以请帮我这样做

4

3 回答 3

1

到目前为止,您没有提供任何代码,因此我对您一直在使用的数据一无所知。我将发布一些代码,您所要做的就是修改它以适应您的场景。

让我们使用一个简单的贷款申请解决方案。客户需要申请贷款,并且需要提供银行详细信息。他必须从下拉列表中选择一家银行。

让我们从名为Bank. 这代表您的数据来自您的数据库表。让我们打电话给桌子Banks

public class Bank
{
     public int Id { get; set; }

     public string Name { get; set; }
}

您调用的表Banks将如下所示:

Id | int | not null | primary key
Name | varchar(50) | not null

取决于您需要做什么,我通常有一个服务层,它调用我的银行存储库以带回数据。但是看到你只需要带回数据就可以了,我们可以跳过服务层。

public class BankRepository : RepositoryBase<Bank>, IBankRepository
{
     public IEnumerable<Bank> FindAll()
     {
          return DatabaseContext.Banks;
     }
}

您的数据库上下文将如下所示:

public class DatabaseContext : DbContext
{
     public DbSet<Bank> Banks { get; set; }
}

这就是您的数据检索方法的样子。它可能不是完整的解决方案,但网上有很多示例。去谷歌吧。

让我们进入 Web 应用程序。

您的视图/页面将使用视图模型而不是您的域模型。您使用视图模型在视图/页面上表示您的数据。因此,在您的创建视图中,您将传入一个带有银行列表的创建应用程序视图模型。IBankRepository将通过称为依赖注入的技术提供一个实例。我用Autofac这个。

public class ApplicationViewModel
{
     public int BankId { get; set; }

     public IEnumerable<Bank> Banks { get; set; }
}

您的控制器的操作方法将填充视图模型并将其发送到您的视图。

public class ApplicationController : Controller
{
     private readonly IBankRepository bankRepository;

     public ApplicationController(IBankRepository bankRepository)
     {
          this.bankRepository = bankRepository;
     }

     public ActionResult Create()
     {
          ApplicationViewModel viewModel = new ApplicationViewModel
          {
               Banks = bankRepository.FindAll()
          };

          return View(viewModel);
     }
}

然后,您的视图将接收此视图模型并使用它做它需要做的事情。在这种情况下,它将填充您的银行下拉菜单。

@model YourProject.ViewModels.Applications.ApplicationViewModel

@using (Html.BeginForm())
{
     <tr>
          <td class="edit-label">Bank: <span class="required">**</span></td>
          <td>
               @Html.DropDownListFor(
                    x => x.BankId,
                    new SelectList(Model.Banks, "Id", "Name", Model.BankId),
                    "-- Select --"
               )
               @Html.ValidationMessageFor(x => x.BankId)
          </td>
     </tr>
}

我不知道你的经验是什么,但从你的问题来看,你似乎仍然需要做很多研究。网上有大量的例子。您将需要处理Entity Framework code first和的样本ASP.NET MVC。投入一些时间,您将在以后获得回报。

我希望这行得通,祝你好运。该解决方案可能不是您想要的,但它可以帮助您朝着正确的方向前进。

于 2013-07-15T07:57:47.930 回答
0

假设您有这样的课程

public class ProductBrand
    {
        //Eg. Nokia, Hp, Dell
        /// <summary>
        /// Second Level to Category
        /// Has Foreign Key to category table
        /// </summary>
        [Key]    
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int ProductBrandId { get; set; }

        [Display(Name = "Category")]
        public int ProductCategoryId { get; set; }
        public virtual ProductCategory ProductCategory { get; set; }

        [Required(ErrorMessage = "This field is required")]
        [StringLength(300, ErrorMessage = "This field must not be greater than 300 xters long")]
        [Display(Name="Name")]
        public string BrandName { get; set; }

        public IList<Product> Products { get; set; }

        [Display(Name = "Status")]
        public bool ActiveStatus { get; set; }


    }

这意味着该模型有一个外键名称 ProductCategoryId 然后在您创建的产品品牌视图中,您将需要一个包含所有产品类别的下拉列表可供选择。

像这样创建一个actionresult

  public ActionResult CreateProductBrand() {

            ViewBag.ProductCategoryId = new SelectList(context.ProductCategories, "ProductCategoryId", "CategoryName");
            return View();
        }

然后在你对应的视图中调用viewbag,如下所示:

  @using (Html.BeginForm()) {
        @Html.AntiForgeryToken()
        @Html.ValidationSummary(true)


            <table style="width:400px" class="post-form">
                <tr>
                    <td>Category</td>
                    <td>
                        <div class="editor-field">

                            @Html.DropDownList("ProductCategoryId", String.Empty)
                            @Html.ValidationMessageFor(model => model.ProductCategoryId)
                        </div>
                    </td>
                </tr>
            </table>
         <table style="width:400px" class="post-form">
                <tr>
                    <td>Brand</td>
                    <td>
                        <div class="editor-field">
                            @Html.EditorFor(model => model.BrandName)
                            @Html.ValidationMessageFor(model => model.BrandName)
                        </div>
                    </td>
                </tr>
                <tr>
                    <td>Status</td>
                    <td>

                        <div class="editor-field">
                            @Html.EditorFor(model => model.ActiveStatus)
                            @Html.ValidationMessageFor(model => model.ActiveStatus)
                        </div>
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td>
                        <input type="submit" class="button blue" value="Save" />
                        <a href="@Url.Action("ProductBrand", "ProductSetup")" class="button">Cancel
                        </a>
                    </td>
                </tr>
            </table>


    }
于 2013-07-15T08:32:26.893 回答
0

视图模型:

    public List<Item> _Items { get; set; }
    public int_newID { get; set; }

模型项目

    public int_id { get; set; }
    public string _name { get; set; }

控制器:用数据填充 _Items 并将该列表发送到您的视图

看法:

    @Html.DropDownListFor(c => c._newID, new SelectList(Model._Items , "_id", "_name"),--Select Item--")
于 2013-07-15T09:26:28.673 回答