MVC-Viewmodel 和 LINQ,将值插入表中,并且该值由另外两个表中的另外两个值标记
我在项目中使用 MVC-Viewmodel 和 EF 的存储库模式。
我有 3 个表:问题、核心值、主题类型。 subjectType 和 CoreValue 与 Question 相关联,这两个表不应获取任何新值,但用户可以创建问题,因此 Question 表将在用户创建问题时获取新数据。我对 CoreValue 和主题类型使用两个下拉列表,以便用户在创建问题时可以选择 CoreValue 和主题类型。
这是我的 HTTPGET 控制器操作:
// GET: /Admin/Create
public ActionResult Create()
{
CoreValueRepository Crep = new CoreValueRepository();
SubjectTypeRepository Srep = new SubjectTypeRepository();
CreateViewModel model = new CreateViewModel();
List<SubjectType> subjectypes = Srep.getall();
List<CoreValue> corevalues = Crep.getall();
model.SubjectTypes = new SelectList(subjectypes, "SID", "Sname");
model.CoreValues = new SelectList(corevalues, "CID", "Cname");
return View(model);
}
这是我的 Viewmodel:
public class CreateViewModel {
public string QuestionText { get; set; }
public string Sname { get; set; }
public string Cname { get; set; }
public SelectList SubjectTypes { get; set; }
public SelectList CoreValues { get; set; }
}
我使用存储库进行 CRUD 操作,使用视图模型处理 UI 中的数据。
现在,我必须在控制器中编写 HTTPPOST Action Create 代码,以将问题数据插入数据库,并且需要使用 CoreValue ID 和 subjectType ID 标记问题。所以我正在考虑开始编写 HTTPOST 操作 Create 的代码,我想知道是否有人可以帮助我解决这个问题。
提前致谢!
此致!
I am using MVC-Viewmodel with repository pattern with EF on my project.
I have 3 tables, Question, CoreValue, SubjectType.
SubjectType and CoreValue are many to many associated with Question and these two tables are not suppose to get any new values, but users can create questions so Question table will get new data when a user creates it. I use two dropdownlists for CoreValue and SubjectType so that the user can choose a CoreValue and a SubjectType when they create a Question.
Here is my HTTPGET controller action:
// GET: /Admin/Create
public ActionResult Create()
{
CoreValueRepository Crep = new CoreValueRepository();
SubjectTypeRepository Srep = new SubjectTypeRepository();
CreateViewModel model = new CreateViewModel();
List<SubjectType> subjectypes = Srep.getall();
List<CoreValue> corevalues = Crep.getall();
model.SubjectTypes = new SelectList(subjectypes, "SID", "Sname");
model.CoreValues = new SelectList(corevalues, "CID", "Cname");
return View(model);
}
And here is my Viewmodel:
public class CreateViewModel
{
public string QuestionText { get; set; }
public string Sname { get; set; }
public string Cname { get; set; }
public SelectList SubjectTypes { get; set; }
public SelectList CoreValues { get; set; }
}
I use Repository for CRUD operations and viewmodels for handling data in UI.
Now I have to code the HTTPPOST Action Create in my controller for inserting Question data to my database, and the questions need to be tagged with CoreValue ID and SubjectType ID. So I was thinkin about to start coding the HTTPOST action Create, and I was wondering if someone could help me out with this.
Thanks in advance!
Best Regards!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这就是我处理它的方式:
在你的 ViewModel 中,替换:
在你的 HTTPGET 中将你的列表放入 Viewbags 中:
要在你的视图中使用 viewbag,你可以使用这个:
你的 HTTPOST :
希望这可以帮助你:)
编辑:
在我的存储库中我这样做:
绑定方法(Repository.Examen 代表我的表,Repository 是我的项目,其中我有一个 .dbml 来代表我的数据库):
This is how i would handle it :
In your ViewModel, replace :
In your HTTPGET put your list in Viewbags :
To use the viewbag in your view you can use this :
Your HTTPOST :
Hope this can help you :)
Edit :
in my repository I do :
Binding methods (Repository.Examen represents my table, Repository is my project where I have a .dbml to represent my DB):