如何在MVC相同的观点显示两个partialviews? [英] How to display two partialviews in the same view in MVC?
问题描述
如何显示在同一视图两个部分看法?在我的控制器我有两个方法,这两个回报partialview。我在一个视图希望这些方法在一起。
How can I display two partial views in the same view? In my controller I have two methods that both returns partialview. I want these methods together in one view.
在我的控制器,我有这样的:
In my controller i have this:
public ActionResult CountCars()
{
var result = this.Data.Cars.All()
.Select(t => new CarsViewModel
{
CategoryName = t.Name,
CategoryCount = t.Category.Count()
});
return PartialView("_ChooseCarsPartialViewLayout", result.ToList());
}
public ActionResult ChooseCity()
{
var view = this.Data.Cities.All()
.Select(x => new CityViewModel
{
CityName = x.Name,
CountCities = x.City.Count()
});
return PartialView("_ChooseCarsPartialViewLayout", view.ToList());
}
_ChooseCarsPartialViewLayout
_ChooseCarsPartialViewLayout
@model IEnumerable<Project.ViewModels.City.CityViewModel> <div class="container">
<div class="well">
<div class="row">
<div class="col-md-12">
<div class="form-group">
<h4>?</h4>
<div class="checkbox checkbox-primary">
@for (int i = 0; i < Model.Count(); i++)
{
var cars = Model.ElementAt(i);
<label>@cars.CategoryName <span class="badge">@cars.CategoryCount</span></label>
@Html.RadioButton(cars.CategoryName,
cars.CategoryId,
new
{
id = "radio",
type = "checkbox"
})
}
</div>
</div>
<div class="form-group">
<h4>Hvor vil du jobbe?</h4>
<div class="checkbox checkbox-primary">
@for (int i = 0; i < Model.Count(); i++)
{
var city = Model.ElementAt(i);
<label>@city.CityName <span class="badge">@city.CountCities</span></label>
@Html.RadioButton(city.CityName,
city.CityId,
new
{
id = "radio",
type = "checkbox"
})
}
</div>
</div>
</div>
</div>
</div>
当我运行的解决方案,它只是显示说。
And when I run the solution it just display an error that says.
值不能为null或空。
参数名称:名称
Value cannot be null or empty. Parameter name: name
18号线:@ Html.RadioButton(cars.CategoryName,
Line 18: @Html.RadioButton(cars.CategoryName,
但是,如果我有两个不同的部分景色那么它的伟大工程,但我想有这个在同一partialview。
But if I had two different partial views then it works great, but I want to have this in the same partialview.
我也有这个Html.Action在_layout呈现partialviews
Also I have this Html.Action in _Layout to render the partialviews
@Html.Action("ChooseCity", "Home")
任何建议?
推荐答案
既然你有一个ViewModel名为 CityViewModel
(虽然我将其命名为 RadiosVM
我会创建一些EditorFor模板。
Since you have a ViewModel called CityViewModel
(though I'll name it RadiosVM
I would create a few EditorFor templates.
使用复合模型作为他的答案提到虽然它可以更通用的,可重复使用 user1672994。一个ViewModel应该封装所有的(也有例外)所需的数据呈现视图。
Use a composite model as user1672994 mentioned in his answer although it can be made more generic and reusable. A ViewModel should encapsulate all (there are exceptions) the data necessary to render a view.
public class RadiosVM
{
public string Name { get; set; }
public IEnumerable<RadioVM> Radios { get; set; }
}
public class RadioVM
{
public string Name { get; set; }
public int Count { get; set; }
}
public class SomePageVM // whatever
{
public RadiosVM CarRadios { get; set; }
public RadiosVM CityRadios { get; set; }
// ... other properties
}
不要旋转起来另一个控制器只显示一个模型(即效率低下),除非控制器做的事情,绝对无关,与当前的控制器和/或你打算缓存这一观点。
Don't spin up another controller just to display a model (that is inefficient), unless the controller is doing things that have absolutely nothing to do with the current controller and/or you plan on caching that view.
public class SomeController
{
public ActionResult SomePage()
{
var model = new SomePageVM();
model.CarRadios = GetCarRadios;
model.CityRadios = GetCityRadios;
return View(model);
}
private RadiosVM GetCarRadios()
{
var result = new RadiosVM()
{
Name = "Cars",
Radios = this.Data.Cars.All()
.Select(t => new RadioVM
{
CategoryName = t.Name,
CategoryCount = t.Category.Count()
})
.ToList();
}
return result;
}
public RadiosVM ChooseCity()
{
var result = new RadiosVM ()
{
Name = "Cars",
Radios = this.Data.Cities.All()
.Select(t => new RadioVM
{
CategoryName = t.Name,
CategoryCount = t.Category.Count()
})
.ToList();
}
return result;
}
查看:
SomePage.cshtml
SomePage.cshtml
@Model SomePageVM
<div class="well">
<div class="row">
<div class="col-md-12">
@Html.EditorFor(m => m.CarRadios)
<div/>
<div class="col-md-12">
@Html.EditorFor(m => m.CityRadios)
<div/>
// ...
/Views/Shared/EditorTemplates/RadiosVM.cshtml
要么
/Views/SomeController/EditorTemplates/RadiosVM.cshtml
/Views/Shared/EditorTemplates/RadiosVM.cshtml or /Views/SomeController/EditorTemplates/RadiosVM.cshtml
@Model RadiosVM
<div class="form-group">
<h4>@Html.DisplayFor(m => m.Name)</h4>
<div class="checkbox checkbox-primary">
@Html.DisplayFor(m => m.Radios)
</div>
/Views/Shared/EditorTemplates/RadioVM.cshtml
要么
/Views/SomeController/EditorTemplates/RadioVM.cshtml
/Views/Shared/EditorTemplates/RadioVM.cshtml or /Views/SomeController/EditorTemplates/RadioVM.cshtml
@Model RadioVM
<label>@Html.DisplayFor(m => m.Name)
<span class="badge">@Html.DisplayFor(m => m.Count)</span>
</label>
@Html.RadioButton(model.Name,
model.Id, // You have no code that references this, I don't know...
new
{
id = "radio", // so **ALL** radios are named 'id'?? not valid
type = "checkbox"
})
这篇关于如何在MVC相同的观点显示两个partialviews?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!