2016-04-26 24 views
1

2つのモデルを1つのビューで表示するビューの条件を追加する方法と場所に問題があります。1つのビューで2つのモデルをif条件付きで表示

これはこれはこれはこれは

public ActionResult Index(){ 
    ViewBag.service_provider = dc.service_provider; 
    ViewBag.pictures = dc.pictures; 
    return View(); 
} 
私のコントローラである

public int SPID { get; set; } 
public string Sp_email { get; set; } 
public string Sp_password { get; set; } 
public string Sp_name { get; set; } 

SERVICE_PROVIDERモデルです

public int PIC_ID { get; set; } 
public string pic_name { get; set; } 
public Nullable<int> belong_id { get; set; } 
public byte[] pic { get; set; } 

画像モデルであるビュー

@foreach (service_provider SP in ViewBag.service_provider) { 
    <tr> 
     <td>@SP.Sp_email</td> 
     <td>@SP.Sp_name</td> 
     <td>@SP.city.Cityname</td> 
    </tr>}@foreach (picture img in ViewBag.pictures){ 
    <tr> 
     <td><img src="data:image/png;base64,@Convert.ToBase64String(img.pic,0,img.pic.Length)" width="100" /> 
     </td> 
    </tr> 
} 

です

service_providerの詳細を表示するには、belongs_id in pictureの画像がSPID service_providerになるようにしてください。 condition.I'mは、データベースの最初のアプローチを使用している場合、私は追加しないところが、私は理解できませんでした

+0

あなたは、その画像のプロパティで、サービスプロバイダをカプセル化するのviewmodelを作成する必要があります(S )。 – Crowcoder

+0

もう1つのモデルを他のモデルから継承するのはなぜですか? – Auguste

+0

完全に関係のないメモでは、Webページから再度表示するファイルをデータベースに保存することは、通常は悪い方法です。これは、クライアントとサーバーの間の多くのネットワークIOとデータベースIOを生成します。主にクライアントが結果をキャッシュしないため、Webサーバーが効率的にファイルを返すことができないためです。より良いアプローチには、ファイルパスを含むデータベースを使用してクライアントから依然としてアクセス可能なディスク上のファイルを保持すること、またはWebクライアントがキャッシュできるように画像ごとの静的URLに基​​づいてサイト内にファイルハンドラを書き込むことが含まれます。 – Igor

答えて

0

試してみてください。

@foreach (picture img in ViewBag.pictures) 
{ 
    if(img.PIC_ID == ViewBag.service_provider.SPID) 
{ 
<tr><td><img src="data:image/png;base64,@Convert.ToBase64String(img.pic,0,img.pic.Length)" width="100" /></td></tr> 
} 
} 

ところで - たぶん、より良い方法 - サーバー側のチェックがあるとONLY写真を返すこと、あなた必要??

+0

Microsoft.CSharp.RuntimeBinder.RuntimeBinderExceptionが発生しました – Oshink

4

ViewBagの代わりに強く型付けされたモデルを使用した方がずっと簡単です。モデルまたはコントローラにロジックを追加することができます。私は個人的に読むとテストが簡単です。また、構文エラーを防止します。

Models.cs

public class Picture { 
    public int PIC_ID { get; set; } 
    public string pic_name { get; set; } 
    public Nullable<int> belong_id { get; set; } 
    public byte[] pic { get; set; } 

    // added - your FK pointing back to the corresponding service_provider 
    public int SPID { get; set; } 
    // added - the corresponding service_provider 
    public service_provider ServiceProvider { get; set; } 
} 

public class service_provider { 
    public int SPID { get; set; } 
    public string Sp_email { get; set; } 
    public string Sp_password { get; set; } 
    public string Sp_name { get; set; } 

    // added 
    public ICollection<Picture> Pictures {get;set;} 
} 

Controller.cs

public ActionResult Index(){ 
    var service_provider = dc.service_provider.Inculde(x => x.Pictures); 
    // var pictures = dc.pictures; 

    // code to add the pictures to the correct service_provider instance 
    // ideally this should already be reflected in your model. 
    // If you are using EF you should be modeling this using proper data relations 
    // that would allow you to execute an .Include statement on the retrieval 
    // and pass the model directly into the View without the need for an additional call to pictures as the service_provider would then already contain the relations 
    return View(service_provider); 
} 

View.cshtml

@model IEnumerable<service_provider> 
@*This following line indicates that it is a list of service_provider hense the IEnumerable*@ 
@foreach (service_provider SP in Model){ 
    <tr> 
     <td>@SP.Sp_email</td> 
     <td>@SP.Sp_name</td> 
     <td>@SP.city.Cityname</td> 
    </tr> 
    @*Iterate over the pictures on each model. 
    You can still split this if you want all service providers and then all pictures.*@ 
    @foreach (picture img in SP.Pictures){ @*changed to still look at the model*@ 
     <tr><td><img src="data:image/png;base64,@Convert.ToBase64String(img.pic,0,img.pic.Length)" width="100" /></td></tr> 
    } 
} 
+0

私はデータベースの最初のアプローチに従っています。 ViewModelを適用することはできますか? – Oshink

+0

@Oshink - これは2つの異なる概念です。データベースはまずEntity Framework用で、ViewModelはMVC用です。しかし、はい、Entity Frameworkでモデルを作成し(コードを最初に使用するか、データベースを最初に使用するか)、そのモデルをMVCコードの厳密に型指定されたビューに渡すことができます。 – Igor

+1

これはかなり近いですが、あなたのコントローラとしてこれを持つことができます: 'var service_provider = dc.service_provider.Include(sp => sp.Pictures);ビュー(service_provider)を返す; '。もちろん、これは、ピクチャナビゲーションプロパティが自動的に作成されるようなモデルを作成したときに外部キー制約があったか、そうでない場合は、手動でNavigationプロパティを手動で追加することを前提としています。 –

関連する問題