SIGN UP MEMBER LOGIN:    
ARTICLE

ScaffoldColumn(bool value) vs HiddenInput(DisplayValue = bool value) in MVC

Posted by Vijay Prativadi Articles | ASP.NET MVC with C# November 11, 2011
In this article, we will see what the use of ScaffoldColumn and HiddenInput. We will also compare what the key differences between these two attribute and what scenario we should consider these attributes for usage. So, accordingly prior to my articles on MVC we will just add these attributes and we will see what the best we can produce.
Reader Level:

ScaffoldColumn(bool value) vs HiddenInput(DisplayValue = bool value) in MVC

In this article, we will see what the use of ScaffoldColumn and HiddenInput. We will also compare what are the key differences between these two attribute and what scenario we should consider these attributes for usage. So, accordingly prior to my articles on MVC we will just add these attributes and we will see the best that we can produce.

Let's reduce the Confusion held between these using two attributes!!

tablemvc.gif

So, now we will elaborate on these two attributes in a practical manner:

So, let's consider Student.cs:

The Location Prop is specified with [HiddenInput(DisplayValue=false)]

Student.cs

using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.ComponentModel.DataAnnotations;
using
System.Web.Mvc;


namespace
MVC_Basic_Application.Models
{

    public class
Student

    {

        [Key]

        public int StudentId { get; set; }

        [Required(ErrorMessage = "Please Enter FirstName")]

        [StringLength(10, ErrorMessage = "FirstName morethan 10 charcs")]

        public string FirstName { get; set; }
      
        [Required(ErrorMessage = "Please Enter LastName")]

        [StringLength(10, ErrorMessage = "LastName morethan 10 charcs")]
        public string LastName { get; set; }

        [Range(5, 50, ErrorMessage = "Age Should Be Between 5 and 50")]

        public int Age { get; set; }

        [HiddenInput(DisplayValue=false)]

        [Required(ErrorMessage = "Please Enter Location")]

        [StringLength(10, ErrorMessage = "Location morethan 10 charcs")]

        public string Location { get; set; }

        [Required(ErrorMessage = "Email is required")]

        [RegularExpression(@"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
                           @"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +

                           @".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$",

                           ErrorMessage = "Email appears to be invalid.")]

        [UIHint("_Email")]

        public string Email { get; set; }

        [Required(ErrorMessage = "Confirm Email is required")]

        [RegularExpression(@"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +

                           @"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +

                           @".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$",

                           ErrorMessage = "Email appears to be invalid.")]

        [UIHint("_Email")]

        [Compare("Email",ErrorMessage="Email Should Match")]

        public string ConfirmEmail { get; set; }

        [Display(Name="Person Gender")]

        [Required(ErrorMessage="Gender is Required")]

        [StringLength(8, ErrorMessage="Gender Too Long")]

        public string Gender { get; set; }

    }

}

So, the output looks like this where the values are hidden:

fig1.gif

Index.cshtml

So, when we want to create new values using Create.cshtml, the screen looks like below:

fig2.gif 

Now let's try the other attribute which is ScaffoldColumn()

The Location Prop is specified with [ScaffoldColumn(false)]

Student.cs

using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.ComponentModel.DataAnnotations;
using
System.Web.Mvc;

namespace MVC_Basic_Application.Models
{
    public class
Student
    {
        [Key]
        public int StudentId { get; set; }

        [Required(ErrorMessage = "Please Enter FirstName")]
        [StringLength(10, ErrorMessage = "FirstName morethan 10 charcs")]

        public string FirstName { get; set; }

        [Required(ErrorMessage = "Please Enter LastName")]

        [StringLength(10, ErrorMessage = "LastName morethan 10 charcs")]
        public string LastName { get; set; }
 

        [Range(5, 50, ErrorMessage = "Age Should Be Between 5 and 50")]
        public int Age { get; set; }

         [ScaffoldColumn(false)]
        [Required(ErrorMessage = "Please Enter Location")]

        [StringLength(10, ErrorMessage = "Location morethan 10 charcs")]

        public string Location { get; set; }
       

        [Required(ErrorMessage = "Email is required")]
        [RegularExpression(@"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
                           @"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
                           @".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$",
                           ErrorMessage = "Email appears to be invalid.")]
        [UIHint("_Email")]
        public string Email { get; set; }

        [Required(ErrorMessage = "Confirm Email is required")]
        [RegularExpression(@"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +

                           @"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +

                           @".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$",

                           ErrorMessage = "Email appears to be invalid.")]

        [UIHint("_Email")]

        [Compare("Email",ErrorMessage="Email Should Match")]

        public string ConfirmEmail { get; set; }

        [Display(Name="Person Gender")]

        [Required(ErrorMessage="Gender is Required")]

        [StringLength(8, ErrorMessage="Gender Too Long")]

        public string Gender { get; set; }

    }

}

By Looking at the Output of Index.cshtml now :

Index.cshtml

fig3.gif

By Looking at the Output of Create.cshtml now:

Create.cshtml

fig4.gif

I hope this article is useful to you!!!!!!!!..Thanks for reading this article..I look Forward for Comments and Feedback....Vijay Prativadi.

Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Team Foundation Server Hosting
Become a Sponsor