Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Saving data
Message
 
 
À
11/12/2012 18:22:17
Information générale
Forum:
ASP.NET
Catégorie:
MVC
Titre:
Versions des environnements
Environment:
C# 4.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Divers
Thread ID:
01559354
Message ID:
01559366
Vues:
44
>I keep things as flat as possible and use POCO objects as much as possible. This simplifies the model binding.
>
>Look at the MVC Music Store sample app to see how to do model binding. http://www.asp.net/mvc/tutorials/mvc-music-store. This is pretty much the way I do it.
>
>
I spent some time making the Client object in EF - it was not that easy. As a result, this object now is a bit Complicated and this is its code:
using System;

using System.ComponentModel.DataAnnotations;
using System.ComponentModel;

using DataAnnotationsExtensions;
using System.ComponentModel.DataAnnotations.Schema;
using System.Collections.Generic;

namespace CardNumbers.Objects
{
    [ComplexType]
    public class PhoneInfo
    {
        [DataType(DataType.PhoneNumber)]
        
        [DisplayName("Phone")]
        [RegularExpression(@"^((\(\d{3}\)|\d{3})\s?)?\d{3}[-\s]?\d{4}\s*$", ErrorMessage = "Please enter valid Phone Number")]
        public virtual string Phone { get; set; }

        [StringLength(5)]
        [DisplayName("Ext")]
        public virtual string Ext { get; set; }

        public bool HasValue
        {
            get
            {
                return (Phone != null || Ext != null);
            }
        }

    }

      [ComplexType]
      public class ContactDetail
      {
          //Constructor
          public ContactDetail()
          {
              phoneInfo = new PhoneInfo();
          }

          [StringLength(100)]
          [DisplayName("Contact Name")]
          [DisplayFormat(NullDisplayText = "")]
          public virtual string Contact { get; set; }

          [Email]
          [StringLength(100)]
          [DisplayName("Email")]
          public virtual string Email { get; set; }

          public virtual PhoneInfo phoneInfo { get; set; }
          public bool HasValue
          {
              get
              {
                  return (Contact != null || Email != null || phoneInfo.HasValue);
              }
          }
      }

/// <summary>
/// Client class (Client No, Client Name, Address, Contact1, Contact2 info, Created By, Modified By (operator and date)
/// </summary>
    public class Client
    {
        public Client()
        {
            Contact1 = new ContactDetail();
            Contact2 = new ContactDetail();
     }

        [Key]
        [Column("ClientId",TypeName = "int")]
        public virtual int Id { get; set; }
        [Required]
        [DisplayName("Client No")]
        [Column("client_no", TypeName = "smallint")]
        public virtual Int16 Number { get; set; }
        
        [Required]
        [Column("client_name", TypeName = "varchar")]
        [DisplayName("Client Name")]
        [MaxLength(30, ErrorMessage = "Client Name should not be longer than 30 characters" )]
        [MinLength(3, ErrorMessage = "Client Name is too short")]
        public virtual string Name { get; set; }

        [DataType(DataType.MultilineText)]
        public virtual string Address { get; set; }

        public virtual ContactDetail Contact1 {get; set;}
        public virtual ContactDetail Contact2 {get; set;}

        [ForeignKey("EnteredByOperator")]
        public string EnteredBy { get; set; }

        [InverseProperty("ClientsEnteredBy")]
        public virtual Operator EnteredByOperator { get; set; }

        [ForeignKey("ModifiedByOperator")]
        public string ModifiedBy { get; set; }

        [InverseProperty("ClientsUpdatedBy")]
        public virtual Operator ModifiedByOperator { get; set; }

        [DataType(DataType.DateTime)]
        [DisplayName("Created on")]
        public DateTime EnteredOn { get; set; }

        [DataType(DataType.DateTime)]
        [DisplayName("Modified on")]
        public DateTime? ModifiedOn { get; set; }

        public virtual ICollection<ClientOrder> ClientOrders { get; set; }
        
        public virtual ICollection<Reorder> Reorders { get; set; }
    }
}
and in my view I've been using the object directly as a model but I may want to switch to view model as Rick suggests:
@using WebDemo.Helper
@model CardNumbers.Objects.Client
   @using (Html.BeginForm(null, null, FormMethod.Post, new { id = "sform", title = "Client Info" }))
   { 
<fieldset>
    <legend>Client Info</legend>

    @Html.ValidationSummary(true)

    <input type="hidden" id="fntype" name="fntype">
    @Html.HiddenFor(m => m.Id)
    @Html.EditorFor(m => m.Number, EditorTemplate.TextBox)

    @Html.EditorFor(m => m.Name, EditorTemplate.TextBox)

    @Html.EditorFor(m => m.Address, EditorTemplate.EditBox)
        
    <div id="ContactsInfo">        
        
        <div id="Contact1">
            
         @Html.EditorFor(m=>m.Contact1)
         
        </div>
        
        <div id="Contact2">
            
           @Html.EditorFor(m => m.Contact2)
        </div>
    </div>
  
    <div id="SaveCancel" class="float-right">
        <button  id="btnSave">Save</button>
        <button  id="btnCancel">Cancel</button>
    </div>
</fieldset>
}
I am going to review Music Store again (I think I did view it before).
If it's not broken, fix it until it is.


My Blog
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform