开发者

mvc 3 ef code first 4.1 -inserting data normalized table structure when the form contains all the fields for all tables

开发者 https://www.devze.com 2023-04-13 02:00 出处:网络
Question: When the user clicks submit what is the best strategy to insert the data into the correct tables?Should I instantiate 3 viewmodels in the httppost and use the Myform atributes to populate my

Question: When the user clicks submit what is the best strategy to insert the data into the correct tables?Should I instantiate 3 viewmodels in the httppost and use the Myform atributes to populate my 3 view models 1.mvr, 2.MvrMedication 3.AdminRoute Then save record for each?

Suppose that someone goes to a form that has data from all 3 view models below

 1. MVR class, 
 2. MvrMedication
 3. AdminRoute

So my viewmodel looks like

public class MyForm
{
   public virtual Mvr Mvr{get;set;}
   public virtual MvrMedication MvrMedication{get;set;}
   public virtual AdminRoute AdminRoute{get;set;}
}

Then In the view Editor for all my atributes. When the user clicks submit what is the best strategy to insert the data into the correct tables?

Below is some code just for reference

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
//Please use: http://blogs.msdn.com/b/efdesign/archive/2010/06/01/conventions-for-code-first.aspx

namespace MVR.Models.Tables
{
    public class Mvr
    {
        [Key]
        public virtual int MvrId { get; set; }
        public virtual ICollection<MvrMedication> MvrMedicationId { get; set; }

        public virtual DateTime DateOfReport { get; set; }
        public virtual DateTime DateOfVariance { get; set; }
        public virtual String   IndividualFirstHospitalNumber { get; set; }
        public virtual String   MallUnit { get; set; }
        public virtual Boolean  PhysicianNotified { get; set; }
        public virtual DateTime PhysicianDateNotified { get; set; }
        public virtual Boolean   PharmacistNotified { get; set; }
        public virtual DateTime PharmacistDateNotified { get; set; }
        public virtual DateTime PharmacistDateOfNotification { get; set; }
        public virtual Boolean  InitialSignature { get; set; }
        public virtual String   VarianceDescription { get; set; }

    }
}







using System;
using System.Collections.Generic;
using S开发者_StackOverflow社区ystem.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

namespace MVR.Models.Tables
{
    public class AdminRoute
    {
        [Key]
        public virtual int AdminRouteId { get; set; }
        public virtual MvrMedication MvrMedicationId { get; set; }
        public virtual string RouteName { get; set; }
    }

    }






    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.ComponentModel.DataAnnotations;
    namespace MVR.Models.Tables
    {
        public class MvrMedication
        {
            [Key]
            public virtual int MvrMedicationId{ get; set; }
            public virtual ICollection<AdminRoute> AdminRouteId { get; set; }

            public virtual string MvrMedicationName { get; set; }

        }
    }


Your view model looks fine, but I would recommend creating view models for the Mvr, MvrMedicine and AdminRoute classes to be more user-friendly. Usually, you're not supposed to see Id's in the view.

On your view side, just use the @Html.EditorFor for the inputs on the form and submit it to an [HttpPost] method in the controller

@model MvcProject.Models.MyForm

@using (Html.BeginForm()) {
    <fieldset>
        <legend>Mvr</legend>
        @Html.EditorFor(m => m.Mvr)
    </fieldset>

    <fieldset>
        <legend>MvrMedication</legend>
        @Html.EditorFor(m => m.MvrMedication)
    </fieldset>

    <fieldset>
        <legend>AdminRoute</legend>
        @Html.EditorFor(m => m.AdminRoute)
    </fieldset>

    <p>
        <input type="submit" value="Save" />
    </p>
}

The default @Html.EditorFor() is going to generate the default inputs for every property in the model. You can create your own custom editor templates for each view model or just define each input in the form, but editor templates are cleaner and reusable.

This is a good tutorial for editor templates. It's for MVC2 but the concepts are the same.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号