将两个模型发送到局部视图

时间:2013-08-19 11:00:16

标签: asp.net-mvc

我正在尝试找到一种方法来检查2个不同表中的结果(searchTern)并传递给局部视图。我得到的错误是部分视图只能采用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);
    }

ViewModel

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 
    }

2 个答案:

答案 0 :(得分:1)

您可以将PartivalView设置为ViewModel使用同时包含Models的{​​{1}},然后传递此内容。

e.g。

模型

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; } 
}

ViewModel

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);
}

答案 1 :(得分: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 );

    }