简体   繁体   English

如何使用viewmodels插入记录:Asp.Net Mvc

[英]How to insert record using viewmodels : Asp.Net Mvc

Introduction 介绍

I am working with the mvc project, implementation approach is code-first. 我正在与mvc项目一起工作,实现方法是代码优先的。 Domain Model i am using, have more than 70 fields.So i made ViewModels. 我正在使用的领域模型有70多个field.So我制作了ViewModels。

Need for making view model is due to creating form wizard which store information in server side(Session Variable). 需要创建视图模型是由于创建了将信息存储在服务器端的表单向导(会话变量)。

Domain Model 领域模型

public class RegisterationDM
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        [Key]
        public int RegisterationId { get; set; }
//other fields
}

Main View Model 主视图模型

public class RegistrationViewModel
{
        public PersonalViewModel PersonalViewModel {get;set; }
        public DetailViewmodel DetailedViewmodel { get; set; }
        public PhysicalDetailViewModel PhysicalDetailViewModel { get; set; }
        public RequirementViewModel RequirementViewModel { get; set; }
        public CreationInfoViewModel CreationInfoViewModel { get; set; }
}

Separate Classes 单独的班级

public class PersonalViewModel()
{
//fields
}
public class DetailViewmodel()
{
//fields
}
public class PhysicalDetailViewModel()
{
//fields
}
public class RequirementViewModel()
{
fields
}
public class CreationInfoViewModel()
{
//fields
}

Record Insertion Code 记录插入码

public ActionResult SaveInformation()
        {
            RegisterationDM regdm = new RegisterationDM();
            RegistrationViewModel regvm = new RegistrationViewModel();

            PersonalViewModel personalvm = (PersonalViewModel)Session["Personal"];
            DetailViewmodel detailvm = (DetailViewmodel)Session["Detail"];
            PhysicalDetailViewModel physicalvm = (PhysicalDetailViewModel)Session["Physical"];
            RequirementViewModel requirementvm = (RequirementViewModel)Session["Requirement"];

            CreationInfoViewModel createdinforvm = new CreationInfoViewModel();
            createdinforvm.CreatedBy = "";
            createdinforvm.CreatedDate = DateTime.Now;

            regvm.PersonalViewModel = personalvm;
            regvm.DetailedViewmodel = detailvm;
            regvm.PhysicalDetailViewModel = physicalvm;
            regvm.RequirementViewModel = requirementvm;
            regvm.CreationInfoViewModel = createdinforvm;
            //here assigning of view model to domain model
            db.Reg.Add(regdm);
            db.SaveChanges();
            return View();
        }

All Actions(Updated) 所有动作(更新)

public ActionResult Step1()
        {
            RegistrationViewModel regvm = new RegistrationViewModel();
            return View(regvm.PersonalViewModel);
        }
        [HttpPost]
        public ActionResult Step1(PersonalViewModel personalvm)
        {
            if (ModelState.IsValid)
            {
                //Store the wizard in session
                Session["Personal"] = personalvm;
                return RedirectToAction("Step2");
            }
            else
            { 
            return View(personalvm);
            }
        }

        public ActionResult Step2()
        {
            if (Session["Personal"] != null)
            {
                RegistrationViewModel regvm = new RegistrationViewModel();
                return View(regvm.DetailedViewmodel);
            }
            else
            {
                return RedirectToAction("Step1");
            }
        }
        [HttpPost]
        public ActionResult Step2(DetailViewmodel detailvm)
        {
            if (ModelState.IsValid)
            {
                //Store the wizard in session
                Session["Detail"] = detailvm;
                return RedirectToAction("Step3");
            }
            return View(detailvm);
        }

        public ActionResult Step3()
        {
            if (Session["Detail"] != null && Session["Personal"] != null)
            {
                RegistrationViewModel regvm = new RegistrationViewModel();
                return View(regvm.PhysicalDetailViewModel);
            }
            else
            {
                return RedirectToAction("Step1");
            }
        }
        [HttpPost]
        public ActionResult Step3(PhysicalDetailViewModel physicalsvm)
        {
            if (ModelState.IsValid)
            {
                //Store the wizard in session
                Session["Physical"] = physicalsvm;
                return RedirectToAction("Step4");
            }
            return View(physicalsvm);
        }

        public ActionResult Step4()
        {
            if (Session["Detail"] != null && Session["Personal"] != null && Session["Physical"] != null)
            {
                RegistrationViewModel regvm = new RegistrationViewModel();
                return View(regvm.RequirementViewModel);
            }
            else
            {
                return RedirectToAction("Step1");
            }
        }
        [HttpPost]
        public ActionResult Step4(RequirementViewModel requirementvm)
        {
            if (ModelState.IsValid)
            {
                Session["Requirement"] = requirementvm;
                return RedirectToAction("SaveInformation");
            }
            return View(requirementvm);
        }

Question

How can i add(record) using main view model.Should i map first? 如何使用主视图模型添加(记录)。我应该先映射吗?

I understand its not possible like that.So i ask, is there a proper way of doing that.What it might be?The best way, the right way or the wrong way? 我知道不可能那样做,所以我问,有没有这样做的正确方法,那可能是什么?最好的方法,正确的方法或错误的方法? I will prefer standard implementation even if it is hard to implement. 即使很难实现,我也会更喜欢标准实现。

If someone have ideas about this problem, please do help.Any kind of help or reference will be appreciated.Thanks for your time. 如果有人对这个问题有想法,请提供帮助。任何帮助或参考将不胜感激。感谢您的时间。

(Due to lack of knowledge, may be i made some mistakes.Down Voters are welcome but please leave comment so i can improve question.) (由于缺乏知识,可能是我犯了一些错误。欢迎选民投票,但请留下评论,以便我改善问题。)

By Defining your own Custom model binder would be suitable for such scenarios. 通过定义您自己的自定义模型活页夹将适用于此类情况。 I would recommend you to find some useful resources to get knowledge on how to implement this, Also this is one simple straight forward article CodeProject Custom Model Binder . 我建议您找到一些有用的资源以获取有关如何实现此目的的知识,这也是一篇简单明了的文章CodeProject Custom Model Binder Let me know if this was useful 让我知道这是否有用

We used Automapper to map fields. 我们使用了Automapper来映射字段。 It is very helpful. 这是非常有帮助的。 Keeps code clean. 保持代码干净。 It has customizable pre and post mapping functions too. 它也具有可定制的前后映射功能。

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

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