【问题标题】:sending two models to partial view将两个模型发送到局部视图
【发布时间】:2013-08-19 20:30:57
【问题描述】:

我正在尝试找到一种方法来检查 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 
    }

【问题讨论】:

标签: asp.net-mvc


【解决方案1】:

您可以将PartivalView 设置为使用由Models 组成的ViewModel,然后传递它。

例如

型号

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

【讨论】:

  • 谢谢,您的意思是“@model Enumerable”吗?在局部视图?我正在使用它
  • 我的视图模型很简单,部分也应该是这样 @model IEnumerable public class TheViewModel { public int SId { get;放; } 公共字符串 SynCity { 获取;放; } 公共字符串 SynName { 获取;放; } //更多字符串参数 public string name { get;放; } 公共字符串 cityName { 获取;放; } //更多字符串参数 }
  • 我有一个 .对此存在显式转换:TheViewModel viewModel = new TheViewModel { museum = model,sites=model2 };
  • 列表没有改变
  • TheViewModel viewModel = new TheViewModel { museum = model.ToList(), sites = model2.ToList()}
【解决方案2】:

试试这个,

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

    }

【讨论】:

  • 谢谢,我不确定我是否理解,模型在我的例子中是一个变量,所以为什么是类名?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-31
相关资源
最近更新 更多