[英]Global model in controller ASP.NET Core MVC 6
I have Home / Index where show list of Current Tasks, Completed Tasks and form for creating new task.我有主页/索引,其中显示当前任务列表、已完成任务和用于创建新任务的表单。
I created HomeIndexViewModel
for pass models (Completed tasks, Current Tasks and TaskCreateViewModel for form) to Index View and there call (@Model.CompletedTasks, @Model.CurrentTasks and @Model.FormCreate)我为传递模型(完成的任务、当前任务和表单的 TaskCreateViewModel)创建了HomeIndexViewModel
到索引视图,然后调用(@Model.CompletedTasks、@Model.CurrentTasks 和@Model.FormCreate)
But in CreatedTaskViewModel I want to get information about validation errors and render them in View.但是在 CreatedTaskViewModel 中,我想获取有关验证错误的信息并将它们呈现在视图中。 I init in Controller HomeIndexViewModel
and get access from Index(Action) and Create(Action).我在 Controller HomeIndexViewModel
中初始化并从 Index(Action) 和 Create(Action) 获取访问权限。
Approach worked, but I am not sure what it's good idea.方法有效,但我不确定这是个好主意。
public class HomeIndexViewModel
{
public List<TaskModel> CompletedTasks { get; set; } = new List<TaskModel>();
public List<TaskModel> CurrentTasks { get; set; } = new List<TaskModel>();
public CreateTaskViewModel FormCreate { get; set; } = new CreateTaskViewModel();
}
public class HomeController : Controller
{
private readonly ITaskRepository _taskRepository;
private HomeIndexViewModel homeIndexViewModel;
public HomeController(IConfiguration configuration)
{
_taskRepository = new TaskRepository(configuration.GetConnectionString("AppDB"));
homeIndexViewModel = new HomeIndexViewModel()
{
CompletedTasks = _taskRepository.GetList("completed");
CurrentTasks = _taskRepository.GetList("current");
};
public ActionResult Index()
{
return View(homeIndexViewModel);
}
public ActionResult Create(CreateTaskViewModel task)
{
if (ModelState.IsValid)
{
_taskRepository.Create(task);
}
return View(nameof(Index), homeIndexViewModel);
}
I think you could write a service and inject it to your controller:我认为您可以编写一个服务并将其注入您的 controller:
public interface ISomeService
{
public HomeIndexViewModel GetHomeIndexViewModel(IConfiguration configuration, ITaskRepository taskRepository)
{
//some codes
HomeIndexViewModel homeIndexView = new HomeIndexViewModel()
{
//some codes
};
return homeIndexView;
}
}
public class SomeService : ISomeService
{
public HomeIndexViewModel GetHomeIndexViewModel(IConfiguration configuration, ITaskRepository taskRepository)
{
//some codes
HomeIndexViewModel homeIndexView = new HomeIndexViewModel()
{
//some codes
};
return homeIndexView;
}
}
In your Startup Class:在你的创业班:
public void ConfigureServices(IServiceCollection services)
{
.....
services.AddTransient<ISomeService, SomeService>();
.....
}
In your Controller:在你的控制器中:
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
private readonly ISomeService _someService;
private readonly ITaskRepository _taskRepository;
private readonly IConfiguration _configuration;
public HomeController(ILogger<HomeController> logger, ISomeService someService, ITaskRepository taskRepository, IConfiguration configuration)
{
_logger = logger;
_someService = someService;
_taskRepository = taskRepository;
_configuration = configuration;
}
public IActionResult Index()
{
var homeindexviewmodel = _someService.GetHomeIndexViewModel(_configuration,_taskRepository);
//you could get the homeindexviewmodel in other controllers with the same method
return View();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.