Thursday, 11 June 2015

ViewData in MVC

View:-


@model IEnumerable<GsMentors.Models.Blog_Master>

@{
    Layout = "~/Views/Shared/_masterLayouth.cshtml";
}
@{

    IEnumerable<GsMentors.Models.Blog_Master> recent = ViewData["recent"] as IEnumerable<GsMentors.Models.Blog_Master>;
    IEnumerable<GsMentors.Models.Category_Master> cat = ViewData["cat"] as IEnumerable<GsMentors.Models.Category_Master>;
}

@*@section sl_image
{
    <img src="../../image1/header.jpg" />
}*@

<div class="container">
    <div class="row" style="float:left">




        @foreach (var item in Model)
        {
            <div class="col-md-1 date">
                <p><strong>@Html.DisplayFor(x => item.Date)</strong></p>
            </div>
            <div class="col-md-6">
                <h3>
                    <a href="/Blogs/Index/@item.ID" style="text-decoration:none;">
                        @Html.DisplayFor(x => item.Title)
                    </a>
                </h3>
                <p>
                    <a href="#"><span class="glyphicon glyphicon-user"></span>@Html.DisplayFor(x => item.User_Master.U_FirstName)@Html.DisplayFor(x => item.User_Master.U_LastName)</a>
                    <a href="#"><span class="glyphicon glyphicon-lock"></span>@Html.DisplayFor(x => item.Category_Master.Name)</a>&nbsp;
                    <a href="#"><span class="glyphicon glyphicon-tag"></span>@Html.DisplayFor(x => item.Tags)</a>&nbsp;
                    <a href="#"><span class="glyphicon glyphicon-cloud"></span>Leave a comment</a>
                </p>
                <p>
                    @Html.DisplayFor(x => item.Descr)
                </p>

            </div>
            <br />
        }

    </div>

    <div class="col-md-3 side" style="float: right;   margin-top: -191px;margin-right: 123px;">
        <h4><a href="#">Like our Facebook Page<a></h4>
        <hr>

        <h4>Recent Posts</h4>
        <hr>
        <ul>
            @foreach (GsMentors.Models.Blog_Master i in recent)
            {
                <li><a href="/Blogs/index/@i.ID">@i.Title</a></li>
            }
        </ul>
        <h4>Archives</h4>
        <hr>
        <ul>
            <li><a href="#">August 2011</a></li>
            <li><a href="#">July 2011</a></li>
        </ul>
        <h4>E-mail Subscription</h4>
        <p>Enter your email address to subscribe to this blog and receive notifications of new posts by email.</p>
        <p>Join 1 other follower</p>
        <input type="text" class="form-control" placeholder="Enter Your email address">
        <br>
        <a href="#" class="btn btn-primary">Sign me up!</a>
        <h4>Categories</h4>
        <ul>
            @foreach (GsMentors.Models.Category_Master i in cat)
            {
                <li><a href="/Blogs/Category/@i.ID">@i.Name</a></li>
            }
        </ul>
    </div>


</div>


Controller:-


    public ActionResult Index()
        {
            var data = db.Blog_Master.ToList();


            ViewData["recent"] = (from s in db.Blog_Master orderby s.Date select s).Take(5);
            ViewData["cat"] = (from s in db.Category_Master orderby s.Name select s);
            return View(data);
        }
            


Model:-


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

namespace GsMentors.Models
{
    public class Query
    {

        [Required(ErrorMessage = "Please Enter Valid Username")]
        [Display(Name = "Name")]
        [MinLength(2)]
        public string Name { get; set; }
        [Required]
        [DataType(DataType.EmailAddress)]
        [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 is not valid")]
        public string Email { get; set; }
        [Required]
        [DataType(DataType.PhoneNumber)]
        public int Mobile { get; set; }
        [Required]
       
        public int Website { get; set; }
        [Required]
        public string quary { get; set; }
    }
}

No comments:

Post a Comment