1

私は 2 つのモデルを持つ非常に単純なデータ構造を持っています。最初のテーブルには UserName、UserQuestion、および userLocationID が含まれ、別のテーブルには LocationName と LocationID が含まれています。最初のテーブルの locationID は、2 番目のテーブルの LocationName に関連付けられています。ただし、関係を指定していません。ここで使用される code first メソッドを使用してデータ構造をセットアップしました。

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

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

私は MVC とドット ネット フレームワークの初心者です。. そして、私はこの答えを見てきましたが、自分のニーズに合わせて変更することはできないようです. 本当に基本的なことをお願いしている場合はお詫び申し上げます。

4

1 に答える 1

0

1 つのコントローラー、1 つのビュー、3 つの C# クラスの例を挙げることができます。このコードを使用するには、Visual Studio で空の MVC2 プロジェクトを作成し、Entity Framework dll バージョン 4.1 への参照を追加します。これらのファイルをどこに置くかについて助けが必要な場合は、Steve Sanderson の 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();
    }
}

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

Views\Home\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>
于 2011-06-29T20:01:15.203 に答える