今天在學C#時,有sample code提及

[ ScaffoldColumn(false)]
 public int CategoryID { get; set; }

對於[ ScaffoldColumn(false)]的用法不清楚,所以查了一下,順便把結果記錄一下

[ ScaffoldColumn(false)]的意思就是不要產生給網站使用者看,也就是說這個欄位是屬於使用者不需要知道的欄位

搜尋中有一篇文章把它和 HiddenInput 拿來一起比較,讓人更容易理解,網站的原址在 http://www.c-sharpcorner.com/UploadFile/54db21/scaffoldcolumnbool-value-vs-hiddeninputdisplayvalue-boo/

以下是文章內容

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!!

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 { getset; }
        [Required(ErrorMessage = "Please Enter FirstName")]
        [StringLength(10, ErrorMessage = "FirstName morethan 10 charcs")]
        public string FirstName { getset; }       
        [Required(ErrorMessage = "Please Enter LastName")]

 

        [StringLength(10, ErrorMessage = "LastName morethan 10 charcs")]
        public string LastName { getset; }
        [Range(5, 50, ErrorMessage = "Age Should Be Between 5 and 50")]
        public int Age { getset; }
        [HiddenInput(DisplayValue=false)]
        [Required(ErrorMessage = "Please Enter Location")]
        [StringLength(10, ErrorMessage = "Location morethan 10 charcs")]
        public string Location { getset; }
        [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 { getset; }
        [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 { getset; }
        [Display(Name="Person Gender")]
        [Required(ErrorMessage="Gender is Required")]
        [StringLength(8, ErrorMessage="Gender Too Long")]
        public string Gender { getset; }
    }
}

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

Index.cshtml

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

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

        [Required(ErrorMessage = "Please Enter FirstName")]
        [StringLength(10, ErrorMessage = "FirstName morethan 10 charcs")]
        public string FirstName { getset; }
        [Required(ErrorMessage = "Please Enter LastName")]

 

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

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

         [ScaffoldColumn(false)]
        [Required(ErrorMessage = "Please Enter Location")]
        [StringLength(10, ErrorMessage = "Location morethan 10 charcs")]
        public string Location { getset; }       

        [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 { getset; }

        [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 { getset; }
        [Display(Name="Person Gender")]
        [Required(ErrorMessage="Gender is Required")]
        [StringLength(8, ErrorMessage="Gender Too Long")]
        public string Gender { getset; }
    }
}

By Looking at the Output of Index.cshtml now :

Index.cshtml 

By Looking at the Output of Create.cshtml now:

Create.cshtml

arrow
arrow
    文章標籤
    ScaffoldColumn C# HiddenInput
    全站熱搜
    創作者介紹
    創作者 痞客興 的頭像
    痞客興

    痞客興的部落格

    痞客興 發表在 痞客邦 留言(0) 人氣()