简体   繁体   English

如何提交包含现有选择的多个复选框选择并将新修改的项目保存在ASP.NET MVC中

[英]How to submit multiple checkbox selections containing existing selections and save the newly modified items in ASP.NET MVC

I have a model called Course and another model called SelectedCourse . 我有一个名为Course的模型和另一个名为SelectedCourse模型。 These two models represent two different tables and have exclusive properties to their own and hence not the same type. 这两个模型代表两个不同的表,并且具有各自的专有属性,因此不是同一类型。

I would like to display List<Course> on my page in the form of multiple check-boxes. 我想以多个复选框的形式在页面上显示List<Course> The courses that are available in the List<SelectedCourse> should be marked as checked. List<SelectedCourse>中可用的课程应标记为已选中。

How do I build my ViewModel to achieve this? 如何构建ViewModel来实现这一目标?

For Post, I would like to return the list of my ViewModel back to the controller and the POST action. 对于Post,我想将ViewModel的列表返回给控制器和POST操作。 And then be able to only delete/add the items that were modified (selected/un-selected) to the table representing List<SelectedCourse> . 然后只能将已修改(选中/未选中)的项目删除/添加到代表List<SelectedCourse>的表中。

How do I achieve this? 我该如何实现?

(Because I don't know how to create my ViewModel, I cannot provide cshtml or controller actions to prove effort) (由于我不知道如何创建ViewModel,因此无法提供cshtml或控制器操作来证明工作量)

Your ViewModel could contain a list of eg CourseViewModel objects, which contains the properties of the Course you are using in the view, but also a bool for representing whether the course is selected. 您的ViewModel可以包含一个列表,例如CourseViewModel对象,该对象列表包含您在视图中使用的Course的属性,还包含一个bool用于表示是否选择了该课程。 See this question for an example. 有关示例,请参见此问题

Your action method (POST) could take the list of CourseViewModels as argument, which allows the form to post that list of ViewModels back to the controller. 您的操作方法(POST)可以将CourseViewModels的列表作为参数,这允许表单将ViewModels的列表发回到控制器。 See this question for an example. 有关示例,请参见此问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM