TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Krishna Rana
NA
362
10.5k
Dropdown Required and Display attribute not working
Apr 12 2019 2:29 AM
Can we make dropdown required to select which is binding using ViewBag as below-
// GET: Employee/Create in the action method
public
ActionResult Create()
{
ViewBag.DepartmentId =
new
SelectList(db.Departments,
"Id"
,
"Name"
);
return
View();
}
// GET: Employee/Create in the Create view
@Html.LabelFor(model => model.DepartmentId,
"DepartmentId"
, htmlAttributes:
new
{ @
class
=
"control-label col-md-2"
})
@Html.DropDownList(
"DepartmentId"
,
null
,
"Select Department"
, htmlAttributes:
new
{ @
class
=
"form-control"
})
@Html.ValidationMessageFor(model => model.DepartmentId,
""
,
new
{ @
class
=
"text-danger"
})
// setting metadata
[MetadataType(
typeof
(EmployeeMetaData))]
public
partial
class
Employee
{
}
public
class
EmployeeMetaData
{
[Required]
public
int
EmployeeId {
get
;
set
; }
[Required]
public
string
Name {
get
;
set
; }
[Required]
public
string
Gender {
get
;
set
; }
[Required]
public
string
City {
get
;
set
; }
[Required]
[Display(Name =
"Department"
)]
public
Nullable<
int
> DepartmentId {
get
;
set
; }
i have set attribute Required and Display but both are not working,
pls help me.
Reply
Answers (
5
)
How to add Edit and Delete button for jq tablefor every row
System.Byte[] showing in Gridview Column in ASP.NET C#