0

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

ビューモデル

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

両方で構成されるPartivalViewa を使用するように設定し、代わりに this を渡すことができます。ViewModelModels

例えば

モデル:

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ます。

こちらの詳細な例をご覧ください。MVC で ViewModel を使用する方法

編集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

これを試して、

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

    }
于 2013-08-19T11:08:44.177 に答える