1

オブジェクトとアパートメントと呼ばれる 2 つのテーブルがあり、これらは と と呼ばれる外部キーで「接続」されていapartmentIDますObjectID。私のコントローラーとモデルは非常に単純です。

モデル:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Projekt.Models
{
    public class WrapperModel
    {

        public IEnumerable<Projekt.Models.objects> Objects { get; set; }
        public IEnumerable<Projekt.Models.apartments> Apartments { get; set; }


    }
}

そして、私のコントローラーは次のとおりです。

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Projekt.Models;

namespace Projekt.Controllers
{
    public class WrapperController : Controller
    {
        public ActionResult Index()
        {
            WrapperModel wrapperModel = new WrapperModel();
            return View(wrapperModel);
        }
    }
}

@foreach ループを使用して次のようなビューを作成したいと考えています。

  • 各オブジェクトの名前を取得し、その名前をそのDetails.cshtmlページにリンクします

  • Details.cshtmlオブジェクト リンクの横にあるページにリンクしているアパートメントの ID を表示します。

4

1 に答える 1

2

最初にモデルを初期化します

public ActionResult Index()
{
    // db is your DbContext or your entity that is represented your DB.
    YourDbContext db = new YourDbContext();

    WrapperModel wrapperModel = new WrapperModel();
    wrapperModel.Objects = db.Objects; // from your db
    wrapperModel.Apartments = db.Apartments;// from your db


    return View(wrapperModel);
}

見る

@model WrapperModel 

@foreach(var item in Model.Objects)
{
    // list items in html elements
    @Html.ActionLink(item.name, "Details", "Controller", new { id = item.id })
}

@foreach(var item in Model.Apartments)
{
    // list items in html elements
    // link to details page
}

コントローラ詳細アクション

public ActionResult Details(int id){}

上記のようなものを試すか、期待される結果になるようにそのコードを変更してください。そして、より具体的な質問をします

于 2013-02-28T09:33:02.060 に答える