2013-08-19 60 views
0

我試圖找到一種方法來檢查結果(searchTern)在2個不同的表中並傳遞到局部視圖。我得到的錯誤是部分視圖只能使用2個參數。我該怎麼做 ?發送兩個模型到局部視圖

public ActionResult index(string searchTerm) 
    { 
     var model = db.museum.OrderBy(c => c.SynCity) 
     .Where(r => searchTerm == null || r.SynCity.StartsWith(searchTerm)) 
       .Select(r => new TheViewModel 
           { 
            SynStyle = r.SynStyle, 
            SynAddress = r.SynAddress, 
            SynNeighborhood = r.SynNeighborhood, 
            SynCity = r.SynCity, 
            SynName = r.SynName, 

           }); 

     var model2 = db.sites.OrderBy(s => s.cityName) 
      .Where(d => d.cityName.StartsWith(searchTerm)) 
      .Select(d => new TheViewModel 
      { 
       cityName = d.cityName, 
       attendant1Phone = d.attendant1Phone, 
       address = d.address, 
       name = d.name, 
       phone = d.phone 
      }); 
     if (Request.IsAjaxRequest()) 
     { 
      return PartialView("_Guid", model, model2); 

     } 

     return View(model, model2); 
    } 

視圖模型

public class TheViewModel { 
    public int SId { get; set; } 
    public string SynCity { get; set; } 
    public string SynName { get; set; } 
    public string SynStyle { get; set; } 
    public string SynAddress { get; set; } 
    public string SynNeighborhood { get; set; } 
    public string name { get; set; } 
    public string cityName { get; set; } 
    //more string Parameters 
    } 
+0

([在一個單一的視圖(#MVC3)多模型]的可能重複http://stackoverflow.com/questions/5763631/multiple -model-in-a-single-view-mvc3) – Amit

回答

1

你可以設置你的PartivalView使用ViewModel這consits兩個Models,並且通過這個來代替。

例如

模式

public class Museum { 
    public string SynCity { get; set; } 
    public string SynName { get; set; } 
    public string SynStyle { get; set; } 
    public string SynAddress { get; set; } 
    public string SynNeighborhood { get; set; } 
    } 

public class Sites { 
    public string name { get; set; } 
    public string cityName { get; set; } 
    public string attendant1Phone { get; set; } 
    public string address { get; set; } 
    public string phone { get; set; } 
} 

視圖模型

public class TheViewModel { 
    public List<Museum> museum { get; set; } 
    public List<Sites> sites { get; set; } 
} 

那麼,你的部分將被輸入到TheViewModel

看看這裏一個詳細的例子,How to use ViewModels with MVC

編輯:修改TheViewModel和改變你如何傳遞到局部視圖

public ActionResult index(string searchTerm) 
{ 
    var model = db.museum.OrderBy(c => c.SynCity) 
    .Where(r => searchTerm == null || r.SynCity.StartsWith(searchTerm)) 
      .Select(r => new Museum        { 
           SynStyle = r.SynStyle, 
           SynAddress = r.SynAddress, 
           SynNeighborhood = r.SynNeighborhood, 
           SynCity = r.SynCity, 
           SynName = r.SynName, 

          }); 

    var model2 = db.sites.OrderBy(s => s.cityName) 
     .Where(d => d.cityName.StartsWith(searchTerm)) 
     .Select(d => new Sites 
     { 
      cityName = d.cityName, 
      attendant1Phone = d.attendant1Phone, 
      address = d.address, 
      name = d.name, 
      phone = d.phone 
     }); 

     TheViewModel viewModel = new TheViewModel { museum = model, sites = model2} 
    if (Request.IsAjaxRequest()) 
    { 
     return PartialView("_Guid", viewModel); 

    } 

    return View(viewModel); 
} 

EDIT2:或者,如果你想保持相同的代碼,你可以使用...

public ActionResult index(string searchTerm) 
{ 
    var vm = new TheViewModel(); 
    var model = db.museum.OrderBy(c => c.SynCity) 
    .Where(r => searchTerm == null || r.SynCity.StartsWith(searchTerm)) 
      .Select(r => vm 
          { 
           SynStyle = r.SynStyle, 
           SynAddress = r.SynAddress, 
           SynNeighborhood = r.SynNeighborhood, 
           SynCity = r.SynCity, 
           SynName = r.SynName 

          }); 

    var model2 = db.sites.OrderBy(s => s.cityName) 
     .Where(d => d.cityName.StartsWith(searchTerm)) 
     .Select(d => vm 
     { 
      cityName = d.cityName, 
      attendant1Phone = d.attendant1Phone, 
      address = d.address, 
      name = d.name, 
      phone = d.phone 
     }); 

    if (Request.IsAjaxRequest()) 
    { 
     return PartialView("_Guid", vm); 

    } 

    return View(vm); 
} 
+0

謝謝,你的意思是「@model Enumerable 」嗎?在局部視圖?我使用它 – Danny

+0

我的視圖模型很簡單,部分也應如此 @model的IEnumerable 公共類TheViewModel { 公衆詮釋SID {獲得;組; } 公共字符串SynCity {get;組; } public string SynName {get;組; } //更多字符串參數 public string name {get;組; } public string cityName {get;組; } //更多字符串參數 } – Danny

+0

我有一個。存在明確的轉換:TheViewModel viewModel = new TheViewModel {museum = model,sites = model2}; – Danny

0

試試這個,

Public Class Model 
{ 
    public string SynStyle { get; set; } 
    public string SynAddress{ get; set; } 
    public string SynNeighborhood { get; set; } 
    public string SynCity { get; set; } 
    public string SynName { get; set; } 
} 

Public Class Model2 
{ 
    public string cityName { get; set; } 
    public string attendant1Phone{ get; set; } 
    public string address { get; set; } 
    public string name { get; set; } 
    public string phone { get; set; } 
} 

Public class Model3 
{ 
    public Model _Model { get; set; } 
    public Model2 _Model2 { get; set; } 
} 

你的控制器

if (Request.IsAjaxRequest()) 
     { 
      Model3 model3 = new Model3(); 
      model3._Model = model; 
      model3._Mode2 = model2; 

     return PartialView("_Guid",model3); 

    } 
+0

謝謝,我不知道我明白,模型是我的情況下的變量,爲什麼類名? – Danny