2011-01-31 12 views
1

私は2つのモデルで非常に単純なデータ構造を持っています。最初のUserName、UserQuestionおよびuserLocationIDと、LocationNameおよびLocationIDを含む別のテーブルでは、最初のテーブルのlocationIDは、2番目のテーブルのLocationNameに関連付けられています。しかし、私は関係を特定していない。私はhereでコードの最初のメソッドを使用してデータ構造を設定しました。MVC3の関連IDを使用してフォームに選択ボックスを挿入

ユーザーが名前と質問を入力するための2つのテキスト入力と、2番目のテーブルのすべてのlocationNamesが設定された選択ボックスを持つフォームを作成したいとします。しかし私はそれを可能にするモデルを作るように見えません。別のViewModelを作成する必要はありますか?

これを行う方法を説明する簡単なチュートリアルを知っている人はいますか?

私はMVCとドットネットフレームワークでかなり新しいです。 。そして私はthis answerを見ましたが、私のニーズに合わせて変更することはできません。だから私が本当に基本的なものを求めているなら、謝罪してください。

答えて

0

私は1つのコントローラで、1つのビューと3つのC#クラスの例を挙げることができます。このコードを使用するには、Visual Studioで空のMVC2プロジェクトを作成し、Entity Framework dllバージョン4.1への参照を追加します。これらのファイルを置く場所について助けが必要な場合は、Steve Sanderson's MVC2 bookをお勧めします。

public class User 
{ 
    public int ID { get; set; } 
    public string UserName { get; set; } 
    public string Question { get; set; } 

    public virtual Location Category { get; set; } 
} 

public class Location 
{ 
    public int ID { get; set; } 
    public string LocationName { get; set; } 
} 

リポジトリ

using System.Data.Entity; 
using System.Collections.Generic; 
using System.Linq; 

public class Repository : System.Data.Entity.DbContext 
{ 
    public DbSet<User> User { get; set; } 
    public DbSet<Location> Locations { get; set; } 

    public Repository() 
    { 
     this.Database.Connection.ConnectionString = 
      @"Server=.;Database=Test;Integrated Security=SSPI"; 

     if (!this.Database.Exists()) 
     { 
      this.Database.Create(); 
      this.Locations.Add(new Location { LocationName = "Queensway" }); 
      this.Locations.Add(new Location { LocationName = "Shepherds Bush" }); 
      this.SaveChanges(); 
     } 
    } 

    public IEnumerable<Location> GetLocations() 
    { 
     return this.Locations.Where(x => x.ID > -1); 
    } 

    public Location GetLocation(int id) 
    { 
     return this.Locations.First(x => x.ID == id); 
    } 

    public void SaveUser(User user) 
    { 
     this.User.Add(user); 
     this.SaveChanges(); 
    } 
} 

コントローラ\ HomeContoller.cs:

using System.Web.Mvc; 

public class HomeController : Controller 
{ 
    Repository repo = new Repository(); 

    [HttpGet] 
    public ActionResult Index() 
    { 
     return View(); 
    } 

    [HttpPost] 
    public ActionResult Index(User user, int categoryId) 
    { 
     user.Category = repo.GetLocation(categoryId); 
     repo.SaveUser(user); 
     return View(); 
    } 
} 

ビュー\ホーム\ Index.aspx

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<User>" %> 

<html> 
<body> 
    <% using (Html.BeginForm()) 
     {%> 
    Username: <%: Html.TextBoxFor(model => model.UserName) %><br /> 
    Question: <%: Html.TextBoxFor(model => model.Question) %><br /> 
    Location: <select name="categoryId"> 
     <% foreach (var location in new Repository().GetLocations()) 
      {%> 
     <option value="<%= location.ID %>"> 
      <%= location.LocationName %></option> 
     <%} %> 
    <br /> 
    </select> 
    <p> 
     <input type="submit" value="Create" /> 
    </p> 
    <% } %> 
</body> 
</html> 
関連する問題