asp.net-mvc - MVC-Viewmodel 和 LINQ,将值插入表中,该值被另外两个表中的另外两个值标记

标签 asp.net-mvc asp.net-mvc-3 linq entity-framework mvvm

我在我的项目中使用带有存储库模式的 MVC-Viewmodel 和 EF。

我有 3 个表,Question、CoreValue、SubjectType。
SubjectType 和 CoreValue 与 Question 相关联是多对多的,这两个表不应该获取任何新值,但是用户可以创建问题,因此 Question 表将在用户创建时获取新数据。我为 CoreValue 和 SubjectType 使用两个下拉列表,以便用户在创建问题时可以选择 CoreValue 和 SubjectType。

这是我的 HTTPGET Controller 操作:

    // 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);

    }  

这是我的 View 模型:

公共(public)类 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; }

}

我使用 Repository 进行 CRUD 操作,使用 View 模型处理 UI 中的数据。

现在我必须在我的 Controller 中编写 HTTPPOST Action Create 代码,以便将问题数据插入我的数据库,并且问题需要使用 CoreValue ID 和 SubjectType ID 进行标记。所以我正在考虑开始编写 HTTPOST 操作 Create,我想知道是否有人可以帮助我解决这个问题。

提前致谢!

最好的祝福!

最佳答案

这就是我将如何处理它:

在您的 ViewModel 中,替换:

public class CreateViewModel {

public string QuestionText { get; set; }
public string Sname { get; set; }
public string Cname { get; set; }

public int SubjectTypesID { get; set; }
public int CoreValuesID { get; set; }
}

在您的 HTTPGET 中,将您的列表放入 Viewbags :
public ActionResult Create()
{

    CoreValueRepository Crep = new CoreValueRepository();
    SubjectTypeRepository Srep = new SubjectTypeRepository();

    CreateViewModel model = new CreateViewModel();
    ViewBag.SubjectTypes = Srep.getall();
    ViewBag.CoreValues = Crep.getall();

    return View(model);

}  

要在您的 View 中使用 viewbag,您可以使用:
@Html.DropDownList("SubjectTypesID ", new SelectList(ViewBag.SubjectTypes as  System.Collections.IEnumerable, "SID", "Sname", Model.SubjectTypesID ))

@Html.DropDownList("CoreValuesID ", new SelectList(ViewBag.CoreValues as  System.Collections.IEnumerable, "CID", "Cname", Model.CoreValuesID ))

你的 HTTPOST :
[HTTPOST]
public ActionResult Create(CreateViewModel model)
{
    //Now with your model you have the Id of CoreValue and SubjectType
    //You could do
 if (ModelState.IsValid)
 {
   QuestionRep.Add(model);
   return RedirectToAction("Index");
 }


   return View(model);
}  

希望这可以帮到你 :)

编辑 :

在我的存储库中,我这样做:
 public void Add(Model.Models.LabExam.Examen entity)
    {
        using (var context = new PDSIDataContext())
        {
            var exam = BindModelExamenToRepExamen(entity);
            context.Examen.InsertOnSubmit(exam);
            context.SubmitChanges();
        }
    }

绑定(bind)方法(Repository.Examen 代表我的表,Repository 是我的项目,我有一个 .dbml 来代表我的数据库):
     private static Repository.Examen BindModelExamenToRepExamen(Model.Models.LabExam.Examen modelExamen)
    {
        return new Repository.Examen
        {
            ID_Examen = modelExamen.ID,
            ID_Examen_Type = modelExamen.ID_Examen_Type,
            Date_Prescription = modelExamen.Date_Prescription,
            Realise_Le = modelExamen.Realise_Le,
            Statut = modelExamen.Statut,
            Fait = modelExamen.Fait,
            ID_Examen_Sous_Type = modelExamen.ID_Examen_Sous_Type,
            ID_Examen_Sous_Sous_Type = modelExamen.ID_Examen_Sous_Sous_Type,
            ID_Patient = modelExamen.ID_Patient,
            Commentaires = modelExamen.Commentaires
        };
    }

关于asp.net-mvc - MVC-Viewmodel 和 LINQ,将值插入表中,该值被另外两个表中的另外两个值标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9364721/

相关文章:

c# - 缓存 IEnumerable 与 List 以进行延迟初始化

c# - 语言特性 vs 框架特性

css - ASP.NET Core MVC - 未应用内联 css 样式

jQuery AJAX - Controller 上的数据参数为空

sql - 我应该在什么情况下使用 Entity SQL?

javascript - 在 View 中将 .Net 对象转换为 JSON 对象

asp.net-mvc - 使用 MVC 路由为 Controller 设置别名

asp.net-mvc - 自动更新 Entity Framework 模型

c# - FluentValidation ModelState.IsValid 始终为真

jquery - MVC 中的空值多选级联下拉列表