0

WebフォームからMVCに移行しようとしていますが、正直なところ、うまくいきません。

データベースから値を入力する必要がありDropDownListますが、これを行う方法を理解するのに苦労しています。エンティティフレームワークを使用してモデルを作成し、それを参照する必要があると思いDropDownListますか?しかし、どうすればそのモデルを指すことができますか?また、データベーステーブルからモデルを作成する場合、selectコマンドを調整して、テーブル全体ではなく特定の値のみを取得するにはどうすればよいですか?

これは一種の幅広い質問であり、例はリストされていませんが、自分の問題に関して理解できる情報を見つけるのに非常に苦労しました。

4

3 に答える 3

1

これから始めます。まだ作成していない場合は、プロジェクトを作成して、モデルを準備できるようにする必要があります。

http://msdn.microsoft.com/en-us/data/gg685489

ここにドロップダウンリストを作成するための例があります

ViewBag.dropdownlist = new SelectList(db.tablename, "Valuefiled", "NameGField");

ここで、Valuefiled=値に使用するデータベース内の列の名前

"NameGField"=名前に使用するデータベース内の列の名前

表示するドロップダウンリストを取得する

@Html.DropDownList("dropdownlist")
于 2012-04-30T07:40:03.270 に答える
1

これはどう

あなたのViewModel

   public class CategoryViewModel
   {
    public Category Category { get; set; }   
    public IEnumerable<SelectListItem> CategoryTitles { get; set; }   
   }

あなたのコントローラー

 public ActionResult Create()
    {
       var categoryviewmodel = new CategoryViewModel();              
       categoryviewmodel.Category = new Category();
       var list = categoryRepository.AllCategoryTitles().ToList().Select(t => new SelectListItem
          {
                Text = t.CategoryName,
                Value = t.CategoryID.ToString()
          })
          .ToList();           
          list.Insert(0, new SelectListItem { Value = "0", Text = "Please Selext" });          

        categoryviewmodel.CategoryTitles = list;
        return View(categoryviewmodel);
    } 

あなたのリポジトリ

    public IQueryable<Category> AllCategoryTitles()
    {       
        var query = context.Categories.Where(m => m.ParentCategoryID == null && m.IsActive==true);
        return query;
    }

あなたの見解

  @Html.DropDownListFor(model => model.CategoryParentID, Model.CategoryTitles)
于 2012-04-30T08:18:47.523 に答える
0

viewModelを使用できます。これは、いくつかの仮定を伴うソリューションの例です。ドロップダウンリストを参照してください(ここのドロップダウンには、「InBound」タイプの部門がリストされています。

従業員モデル

public class EmployeeModel
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int DeptId { get; set; }
}

部門モデル

public class DepartmentModel
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Type { get; set; }
}

ViewModel(ビューに渡されます)

public class EmployeeViewModel
{
    public EmployeeModel Employee { get; set; }
    public IEnumerable<DepartmentModel> Departments { get; set; }
}

コントローラ

public ActionResult Index()
    {
        EmployeeViewModel vm = new EmployeeViewModel();

        //This is hardcoded. However you will write your own method to pull the department details with filtering
        List<DepartmentModel> departments = new List<DepartmentModel>() { new DepartmentModel { Id = 1, Name = "Accounts", Type = "InBound" }, new DepartmentModel { Id = 2, Name = "Finance", Type = "OutBound" }, new DepartmentModel { Id = 3, Name = "HR", Type = "InBound" } };
        vm.Departments = departments.Where(d => d.Type == "InBound"); 

        return View(vm);
    }

意見

    @model Test1.ViewModels.EmployeeViewModel
    @{
          ViewBag.Title = "Index";
     }

     <h2>Index</h2>
     @using (Html.BeginForm())
     {
         @Html.HiddenFor(model => model.Employee.Id);
      <table>
        <tr>
            <td>@Html.LabelFor(model => model.Employee.FirstName)</td>
            <td>@Html.EditorFor(model => model.Employee.FirstName)</td>
        </tr>
        <tr>
            <td>@Html.LabelFor(model => model.Employee.LastName)</td>
            <td>@Html.EditorFor(model => model.Employee.LastName)</td>
        </tr>
        <tr>
            <td>@Html.Label("Department")</td>
            <td>@Html.DropDownListFor(model => model.Employee.DeptId, new SelectList(Model.Departments, "Id", "Name"))</td>
        </tr>
    </table>
}
于 2012-04-30T08:19:11.523 に答える