0

I'm trying to find a way to check results (searchTern) in 2 different table and pass to partial view . the error I'm getting is that partial view can take only 2 argument . how can I do it ?

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

The 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 
    }
Christian Phillips
  • 18,399
  • 8
  • 53
  • 82
Danny
  • 301
  • 1
  • 4
  • 21
  • possible duplicate of [Multiple Model in a Single View (# MVC3)](http://stackoverflow.com/questions/5763631/multiple-model-in-a-single-view-mvc3) – Amit Aug 19 '13 at 11:03

2 Answers2

1

You could set up your PartivalView to use a ViewModel which consits of both Models, and pass this instead.

e.g.

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

ViewModel:

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

Then, your partial will be typed to TheViewModel.

Take a look at a detailed example here, How to use ViewModels with MVC

EDIT: Modify TheViewModel and change how you pass to the Partial View

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: Or, if you want to keep the same code, you could use this...

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);
}
Christian Phillips
  • 18,399
  • 8
  • 53
  • 82
  • thanks, did you mean" @model Enumerable "? at the partial view ? I am using it – Danny Aug 19 '13 at 11:07
  • My viewmodel is straightforward and the partial should also be so @model IEnumerable public class TheViewModel { public int SId { get; set; } public string SynCity { get; set; } public string SynName { get; set; } //more string Parameters public string name { get; set; } public string cityName { get; set; } //more string Parameters } – Danny Aug 19 '13 at 11:18
  • I have an . An explicit conversion exists regarding this : TheViewModel viewModel = new TheViewModel { museum = model,sites=model2 }; – Danny Aug 19 '13 at 12:23
  • list did not change it – Danny Aug 19 '13 at 12:40
  • `TheViewModel viewModel = new TheViewModel { museum = model.ToList(), sites = model2.ToList()}` <-- see the ToList(). And also, make sure the Partial is expecting the model to be TheViewModel and not an IEnumerable – Christian Phillips Aug 19 '13 at 12:50
  • your code is working (Dependence in other view that use the viewmodel before) , but I'm getting there it will work – Danny Aug 19 '13 at 13:35
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 );

    }
Jaimin
  • 7,964
  • 2
  • 25
  • 32
  • thanks , i'm not sure I understand , the model is a variable in my case so why the class name ? – Danny Aug 19 '13 at 11:26