MVC-Viewmodel和LINQ,将值插入到一个表中,该值被另外两个表中的其他值标记

时间:2021-12-15 02:10:37

I am using MVC-Viewmodel with repository pattern with EF on my project.

我在我的项目中使用带有存储库模式的mvc模式。

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.

我有三个表格,问题,课程,主题。SubjectType和CoreValue与问题关联很多,这两个表不应该得到任何新的值,但是用户可以创建问题,因此当用户创建时,问题表将获得新的数据。我为CoreValue和SubjectType使用了两个下拉列表,以便用户在创建问题时可以选择CoreValue和SubjectType。

Here is my HTTPGET controller action:

下面是我的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);

    }  

And here is my Viewmodel:

这是我的视图模型:

public class CreateViewModel {

公开课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.

我使用存储库进行CRUD操作,使用视图模型处理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.

现在我必须在我的控制器代码HTTPPOST行动创建插入数据到我的数据库问题,和问题需要标记CoreValue ID和SubjectType ID。所以我没完没了开始编码HTTPOST行动创造,我想知道如果有人能帮帮我。

Thanks in advance!

提前谢谢!

Best Regards!

最好的问候!

1 个解决方案

#1


1  

This is how i would handle it :

我是这样处理的:

In your ViewModel, replace :

在您的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; }
}

In your HTTPGET put your list in Viewbags :

在您的HTTPGET中,将您的列表放在viewbag中:

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

}  

To use the viewbag in your view you can use this :

要在视图中使用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 ))

Your HTTPOST :

你的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);
}  

Hope this can help you :)

希望这能对你有所帮助。

Edit :

编辑:

in my repository I do :

在我的存储库中,我这样做:

 public void Add(Model.Models.LabExam.Examen entity)
    {
        using (var context = new PDSIDataContext())
        {
            var exam = BindModelExamenToRepExamen(entity);
            context.Examen.InsertOnSubmit(exam);
            context.SubmitChanges();
        }
    }

Binding methods (Repository.Examen represents my table, Repository is my project where I have a .dbml to represent my DB):

绑定方法(存储库。Examen表示我的表,Repository是我的项目,其中有.dbml表示我的DB):

     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
        };
    }

#1


1  

This is how i would handle it :

我是这样处理的:

In your ViewModel, replace :

在您的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; }
}

In your HTTPGET put your list in Viewbags :

在您的HTTPGET中,将您的列表放在viewbag中:

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

}  

To use the viewbag in your view you can use this :

要在视图中使用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 ))

Your HTTPOST :

你的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);
}  

Hope this can help you :)

希望这能对你有所帮助。

Edit :

编辑:

in my repository I do :

在我的存储库中,我这样做:

 public void Add(Model.Models.LabExam.Examen entity)
    {
        using (var context = new PDSIDataContext())
        {
            var exam = BindModelExamenToRepExamen(entity);
            context.Examen.InsertOnSubmit(exam);
            context.SubmitChanges();
        }
    }

Binding methods (Repository.Examen represents my table, Repository is my project where I have a .dbml to represent my DB):

绑定方法(存储库。Examen表示我的表,Repository是我的项目,其中有.dbml表示我的DB):

     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
        };
    }