C# 是否可以组合2个视图模型,每个模型填充2个不同的LINQ查询,然后返回到1个视图?
我试图在一个视图中显示两组数据: 第一个LINQ:C# 是否可以组合2个视图模型,每个模型填充2个不同的LINQ查询,然后返回到1个视图?,c#,asp.net-mvc,entity-framework,linq,asp.net-mvc-viewmodel,C#,Asp.net Mvc,Entity Framework,Linq,Asp.net Mvc Viewmodel,我试图在一个视图中显示两组数据: 第一个LINQ: var result = from add in db.Addresses join u in db.Users on add.UserID equals u.Id where CurrUser == add.UserID select new AddressViewModel
var result = from add in db.Addresses
join u in db.Users on add.UserID equals u.Id
where CurrUser == add.UserID
select new AddressViewModel
{
FirstAddressLine = add.FirstAddressLine,
SecondAddressLine = add.SecondAddressLine,
Town = add.Town,
PostCode = add.PostCode,
Distance = addDis
};
第二个LINQ:
var result = from c in db.Carts
join s in db.Sweets on c.SweetID equals s.SweetID
where c.CartID == cartId
select new ShoppingCartViewModel
{
SweetName = s.SweetName,
Qty = c.Qty,
Price = s.Price,
SweetTotal = (s.Price * c.Qty),
Singular = s.Singular,
CartTotal = cartTotal
};
两个视图模型如下所示:
public class AddressViewModel
{
public string FirstAddressLine { get; set; }
public string SecondAddressLine { get; set; }
public string Town { get; set; }
public string County { get; set; }
public string PostCode { get; set; }
public decimal Distance { get; set; }
}
public class ShoppingCartViewModel
{
public decimal? CartTotal { get; set; }
public decimal? SweetTotal { get; set; }
public bool? Singular { get; set; }
public decimal? Price { get; set; }
public int? RecordID { get; set; }
public string CartID { get; set; }
public int? SweetID { get; set; }
public int? PemixID { get; set; }
public int? Qty { get; set; }
public System.DateTime? DateCreated { get; set; }
public string SweetName { get; set; }
}
但我不确定将2个视图模型返回到1中的最佳实践是什么
编辑:如果我遗漏了明显的(仍在学习)内容,请道歉,但我不确定如何遵循您的答案。。。我已经创建了购物车的部分视图:\u shoppingcartportial,我已将其放置在共享视图文件夹中
当我尝试在签出视图中渲染部分视图时,我收到一个错误,它预期ShoppingCartViewModel,但我当前正在将AddressViewModel发送给它
我已经制作了两个的viewmodel:
public class CheckoutViewModel
{
public IEnumerable<Address> AVM { get; set; }
public IEnumerable<Cart> SCVM { get; set; }
}
@model Something.Models.MyMainViewModel
<div>
<div>
@Html.Partial("~/Views/MyMain/AddressPartial.cshtml", Model.Address)
</div>
<div>
@Html.Partial("~/Views/MyMain/ShoppingCartPartial.cshtml", Model.ShoppingCart)
</div>
</div>
public class MyMainViewModel : ViewModelBase
{
#region <Constructors>
public MyMainViewModel(IApplication application, int id) : base(application)
{
Address = Application.GetAddress(id); //<-- From the base class
ShoppingCart = Application.GetShoppingCart(id); //<-- From the base class
}
#endregion
#region <Properties>
public Address Address { get; set; }
public ShoppingCart ShoppingCart { get; set; }
#endregion
}
public class MainViewController : BaseController
{
#region <Actions>
[HttpGet]
public ActionResult DoSomething(int id)
{
var viewModel = new MyMainViewModel(application, id);
return View("MyView", viewModel);
}
#endregion
}
public class ViewModelBase
{
#region <Constructors>
public ViewModelBase(IApplication application)
{
Application = (MyApplication)application;
}
#endregion
#region <Properties>
protected MyApplication Application { get; set; }
#endregion
}
@model Something.Models.MyMainViewModel
@Html.Partial(“~/Views/MyMain/AddressPartial.cshtml”,Model.Address)
@Html.Partial(“~/Views/MyMain/shoppingcartportial.cshtml”,Model.ShoppingCart)
公共类MyMainViewModel:ViewModelBase
{
#区域
公共MyMainViewModel(IAApplication应用程序,int id):基础(应用程序)
{
Address=Application.GetAddress(id);//有一个包含两个子视图模型的主视图模型。类似这样的方法可以工作:
public class SomePageViewModel
{
private IAddressService _addressService;
private IShoppingCartService _shoppingCartService;
public SomePageViewModel(
IAddressService addressService,
IShoppingCartService shoppingCartService)
{
_addressService = addressService;
_shoppingCartService = shoppingCartService;
}
public async Task GetDataAsync()
{
Address = await _addressService.GetAddressViewModelAsync();
ShoppingCart = await _shoppingCartService.GetShoppingCartAsync();
}
public AddressViewModel Address { get; private set; }
public ShoppingCartViewModel ShoppingCart { get; private set; }
}
您的LINQ语句将分别包含在iadressservice
和IShoppingCartService
中(或者您可以直接调用respository)
希望这有帮助。创建一个包含其他两个视图模型的单视图模型:
public class FullViewModel {
public AddressViewModel AddressModel { get; set; }
public ShoppingCartViewModel ShoppingCartModel { get; set; }
}
然后把模型还给我
在此基础上,只需创建两个部分:
<div>
@Html.Partial("[path]", Model.AddressModel)
@Html.Partial("[path]", Model.ShoppingCartModel)
</div>
@Html.Partial(“[path]”,Model.AddressModel)
@Html.Partial(“[path]”,Model.ShoppingCartModel)
您必须在另一个ViewModel中添加两个ViewModel使用1个视图和2个部分:1个视图保存部分。2个部分用于数据。将(具体的)视图模型传递到每个-@{Html.RenderPartial(“您的视图”,您的_模型,ViewData);}事实上,您不必使用两个部分视图…您可以使用“MainViewModel”在没有任何局部视图的单个视图中。但是,当然,使用局部视图时代码会更干净
<div>
@Html.Partial("[path]", Model.AddressModel)
@Html.Partial("[path]", Model.ShoppingCartModel)
</div>