C# 在ViewModelBuilder中生成ViewComponent
我希望将视图模型的构建保持在其各自控制器的视图模型生成器(从这里开始称为vmb)中。以HomeController为例,我将从其构造函数HomeViewModelBuilder中实例化C# 在ViewModelBuilder中生成ViewComponent,c#,asp.net-core,asp.net-core-viewcomponent,C#,Asp.net Core,Asp.net Core Viewcomponent,我希望将视图模型的构建保持在其各自控制器的视图模型生成器(从这里开始称为vmb)中。以HomeController为例,我将从其构造函数HomeViewModelBuilder中实例化 public class HomeController : Controller { private readonly HomeViewModelBuilder _viewModelBuilder; public HomeController(IUserManagerService userMa
public class HomeController : Controller
{
private readonly HomeViewModelBuilder _viewModelBuilder;
public HomeController(IUserManagerService userManagerService, IEmployeeService employeeService, IExampleServiceZ exampleServiceZ)
{
_viewModelBuilder = new HomeViewModelBuilder(userManagerService, employeeService, exampleServiceZ);
}
public ActionResult Index()
{
var model = _viewModelBuilder.BuildHomeViewModel(CurrentUserId);
return View("Index", model);
}
}
并将从repo中提取数据所需的任何服务传递给这个vmb的构造函数
由于.NETCore没有附带子操作,因此我需要决定如何继续移植asp.NETMVC5应用程序。某些子动作视图调用自己的子动作。这不是关于嵌套视图组件的问题,而是关于如何使用视图组件的问题,请记住,我想在我的生成器中进行构建。目前,我们正在对jquery使用一种肮脏的攻击,如下所示:
视图上包含子操作的容器
<div id="subsections-container">
@*This is a place holder for the $.get*@
</div>
他正在构建视图组件的内部调用。我想在vmb中这样做
第二个问题是,我有一个ajax表单,希望刷新容器中的数据
视图中的窗体:
@using (@Ajax.BeginForm("Update", "SubSection", null, new AjaxOptions { HttpMethod = "Post", UpdateTargetId = "subsections-container", OnBegin = "blockModal()", OnComplete = "hideModal();" }, new { id = "frm-subsection-update", @class = "form-horizontal" }))
{
}
这个动作击中了控制器
[HttpPost]
[Authorize(Roles = RoleName.SubSectionUpdate)]
public ActionResult Update(SubsectionUpdateViewModel model)
{
var subsection = new SubsectionDto();
if (model.Id > 0)
subsection = _subsectionService.Get(model.Id);
subsection.InjectFrom(model);
_subsectionService.Update(subsection);
return RedirectToAction("Index", new { });
}
@await Component.InvokeAsync("WhatsNew",Model.WhatsNewViewComponentViewModel)
如果这不再是一个动作,而是一个viewcomponent,我在这里返回什么
他正在构建视图组件的内部调用。我想在vmb中这样做
在HomeViewModel
中添加WhatsNewViewComponentViewModel
属性,并将创建WhatsNewViewComponentViewModel
的责任交给HomeViewModelBuilder
在Home
控制器的Index.cshtml
中
[HttpPost]
[Authorize(Roles = RoleName.SubSectionUpdate)]
public ActionResult Update(SubsectionUpdateViewModel model)
{
var subsection = new SubsectionDto();
if (model.Id > 0)
subsection = _subsectionService.Get(model.Id);
subsection.InjectFrom(model);
_subsectionService.Update(subsection);
return RedirectToAction("Index", new { });
}
@await Component.InvokeAsync("WhatsNew",Model.WhatsNewViewComponentViewModel)
WhatsNewViewComponent
:
public class WhatsNewViewComponent : ViewComponent
{
public IViewComponentResult Invoke(WhatsNewViewComponentViewModel model)
{
return View(model);
}
}
如果这不再是一个动作,而是一个viewcomponent,我在这里返回什么
从操作返回ViewComponent
[HttpPost]
[Authorize(Roles = RoleName.SubSectionUpdate)]
public ActionResult Update(SubsectionUpdateViewModel model)
{
.......
return ViewComponent("VCName", VCModel);
}
我的子操作在另一个控制器中,因此我所做的是在父控制器的vmb中声明并实例化该控制器的vmb,以便在Home/Index的生成器方法中,我还有SubsectionIndexViewComponent=_subsectionViewModelBuilder.BuildIndexViewModel(id),我假设在想要返回视图组件的Update方法中,我需要构建VCModel。也许在相应的视图组件中构建视图组件模型会更好。我需要考虑的事情。此外,Re#er正在共享/组件中重新创建我的视图,我希望它保留在相关控制器的“视图”文件夹中。@AlanBall 1)在VC中构建VCModel也是一个好主意,因为它具有可重用性。2) 创建
~Views/Home/Components/WhatsNew/
文件夹并将Default.cshtml
放入其中。