i have following code

@model EMS1.Models.UsersViewModel
@{
                ViewBag.Title = "List of users";
                var grid = new WebGrid(source: Model.users, canPage: true, rowsPerPage: 10);
              grid.Pager(WebGridPagerModes.All);
            }



@{
    ViewBag.Title = "IndexViewModel";
}
<script src="@Url.Content("~/Scripts/jquery-1.6.1.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script> 

<h2>Grid</h2>

    <style type="text/css">

        .table

        {

            margin: 4px;

            border-collapse: collapse;

            width: 300px;

        }

        .header

        {

            background-color: gray;

            font-weight: bold;

            color: #fff;

        }

        .table th, .table td

        {

            border: 1px solid black;

            padding: 5px;

        }

    </style>

<h2>IndexViewModel</h2> 
    // This is For EmpDetail in Grid  
        <fieldset>  
            <legend><b>uSER Details</b></legend>  
            <table border="1" cellpadding="10">  
                <tr>  
                    <th>  
                        @Html.DisplayNameFor(model =>model.id)
                    </th>  
                    <th>  
                        @Html.DisplayNameFor(model => model.UserName)  
                    </th>  
                    <th>  
                        @Html.DisplayNameFor(model => model.Password)  
                    </th>  
                    <th>  
                        @Html.DisplayNameFor(model => model.Email)  
                    </th>  
                    <th>  
                        @Html.DisplayNameFor(model => model.RegDateTime)  
                    </th>  
                    <th>  
                        Action  
                    </th>  
                </tr>  

        @*        @foreach (var item in (IEnumerable<EMS1.Models.UsersViewModel>)ViewBag.Name)  *@
                 @foreach (var item in Model.users)  

                {  
                    <tr>  
                        <td>  
                            @Html.DisplayFor(modelItem => item.id)  
                        </td>  
                        <td>  
                            @Html.DisplayFor(modelItem => item.UserName)  
                        </td>  
                        <td>  
                            @Html.DisplayFor(modelItem => item.Password)  
                        </td>  
                        <td>  
                            @Html.DisplayFor(modelItem => item.Email)  
                        </td>  
                        <td>  
                            @Html.DisplayFor(modelItem => item.RegDateTime)  
                        </td>  
                        <td>  
                    @*        @Html.ActionLink("Edit", "Index", new { id = item.id }) |*@  
                              @Html.ActionLink("Edit", "Edit", new { id = item.id }) |
                            @Html.ActionLink("Delete", "Delete", new { id = item.id })  
                        </td>  
                    </tr>  
                }  

            </table>  
      </fieldset>  

       // This is For EmpDetail in webGrid
<fieldset>
        <legend><b>uSER Details</b></legend> 

     <div id="content">
               @grid.GetHtml(
               tableStyle:"webgrid-table",
                headerStyle:"webgrid-header",
               footerStyle:"webgrid-footer",
                alternatingRowStyle:"webgrid-alternating-row",
               rowStyle:"webgrid-row-style",
             columns:grid.Columns(
                  //here i will add column for serial no

                   grid.Column(columnName:"id",header:"id"),
                    grid.Column(columnName:"UserName", header:"Username"),
                   grid.Column(header:"Email", format:@<text><a href="mailto:@item.Email">@item.Email</a></text>),
                 grid.Column(header:"DElete", format: @<text>@Html.ActionLink("Delete", "Delete", new { id = item.Id }  )</text>),
               grid.Column(header:"Edit", format: @<text>@Html.ActionLink("Edit", "Edit", new { id = item.Id }  )</text>)


                 ))
            </div>

</fieldset>
    // This is for the Emp Entry Screen  

    <div class="form-horizontal">  

@using (Html.BeginForm("Create","Home")) 
{
       @Html.AntiForgeryToken()  
   @Html.ValidationSummary(true)  
    <fieldset>  
            <legend> <b>Entry Screen</b></legend>  

            <div class="form-group">  
                @Html.LabelFor( model => model.UserName)  
                <div class="col-md-10">  
                    @Html.EditorFor(model =>  model.UserName)  
                  @Html.ValidationMessageFor(model => model.UserName)    
                </div>  
            </div>  

            <div class="form-group">  
                @Html.LabelFor(model => model.Password, new { @class = "control-label col-md-2" })  
                <div class="col-md-10">  
                    @Html.EditorFor(model => model.Password)  
                    @Html.ValidationMessageFor(model => model.Password)  
                </div>  
            </div>  

            <div class="form-group">  
                @Html.LabelFor(model => model.Email, new { @class = "control-label col-md-2" })  
                <div class="col-md-10">  
                    @Html.EditorFor(model => model.Email)  
                    @Html.ValidationMessageFor(model => model.Email)  
                </div>  
            </div>  




            <div class="form-group">  
                <p>  
                    <input type="submit" value="Create" name="Create"  
                           style=@((ViewBag.Operation != null && Convert.ToInt32(ViewBag.Operation) > 0) ? "display:none" : "display:block") />  
                    <input type="submit" value="Update" name="Update"  
                           style=@((ViewBag.Operation != null && Convert.ToInt32(ViewBag.Operation) > 0) ? "display:block" : "display:none") />  
                </p>  
            </div>  
            </fieldset>  

}


</div>



<!DOCTYPE html>
<html>
<head>
    <title>@ViewBag.Title</title>
    <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
    <script src="@Url.Content("~/Scripts/jquery-1.4.4.min.js")" type="text/javascript"></script>
</head>

<body>
    <div class="page">

        <div id="header">
            <div id="title">
                <h1>Email Managment System</h1>
            </div>

            <div id="logindisplay">
                @Html.Partial("_LogOnPartial")
            </div>

            <div id="menucontainer">

                <ul id="menu">
                    <li>@Html.ActionLink("Home", "Index", "Home")</li>
                    <li>@Html.ActionLink("About us", "About", "Home")</li>
                    <li>@Html.ActionLink("Show All", "IndexViewModel", "Home")</li>
                    <li>@Html.ActionLink("Search User", "searchuser", "Home")</li>
                    <li>@Html.ActionLink("Mail report", "Report", "Home")</li>
                    <li>@Html.ActionLink("Deledted item", "Deleteditems", "Home")</li>
                </ul>

            </div>
        </div>

        <div id="main">
            @RenderBody()
            <div id="footer">
            @RenderSection("Bottom",false)

            </div>
        </div>
    </div>
</body>
</html>




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

namespace EMS1.Models
{
    public class TBL_USER
    {
        public int id { get; set; }
        [Required(ErrorMessage = "Please Enter Name")]
        [Display(Name = "Name")]

        public string UserName { get; set; }
        public string Password { get; set; }
        public string Email { get; set; }
        public Nullable<System.DateTime> RegDateTime { get; set; }
    }
}

but itdoes not showing error messages

Please Enter Name ..any work around

Hmz, looks okay as far as I can tell. Unfortunately I cannot test your code right now.

A workaround can be to use ModelState.AddModelError()

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.