0

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

2 回答 2

1

您可以设置您PartivalView的使用 a ViewModelwhich consits both 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.

在此处查看详细示例,如何将 ViewModels 与 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);
}
于 2013-08-19T11:02:48.730 回答
0

Try this,

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

Your controller

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

        return PartialView("_Guid",model3 );

    }
于 2013-08-19T11:08:44.177 回答