Sunday, February 9, 2014

ASP.NET MVC 5

# Getting Started...
#Adding a Controller...
#Adding a View...
#Adding a Model...
#Creating a Connection String and  Working with SQL Server 2008 and uploading an Image using MVC 5
Table:

Find  the <connectionStrings>  element:






<connectionStrings>
    <add name="BLRSDBEntities"
     connectionString="Server=192.168.10.15\SS8;Database=BLRSDB;User Id=sa;Password=1234; providerName="System.Data.SqlClient" />

    <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-BLRS-20140119112437.mdf;Initial Catalog=aspnet-BLRS-20140119112437;Integrated Security=True"
      providerName="System.Data.SqlClient" />
  </connectionStrings>

create a class at model:
 public class tblCompanye
    {
        [Required, Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
        [DisplayName("Company ID")]
         public decimal CompID {get;set;}
        [DisplayName("Company Code")]
         public string CompCode    {get;set;}
        [DisplayName("Company Name")]
         public string CompName    {get;set;}
        [DisplayName("Company Address")]
         public string CompAddr {get;set;}
        [DisplayName("Phone No")]
         public string PhoneNo        {get;set;}
        [DisplayName("Mobile No")]
         public string MobileNo    {get;set;}
        [DisplayName("Fax")]
         public string Fax    {get;set;}
        [DisplayName("Email")]
         public string Email    {get;set;}
        [DisplayName("WEB URL")]
         public string WebURL        {get;set;}
        [DisplayName("Company Logo")]
         public byte[] CompLogo    {get;set;}
        [DisplayName("Reference ID")]
         public decimal RefID        {get;set;}
        [DisplayName("Inserted By")]
         public string InsertedBy  {get;set;}
        [DisplayName("Inserted On")]
         public DateTime? InsertedOn  {get;set;}
        [DisplayName("Updated By")]
         public string UpdatedBy   {get;set;}
        [DisplayName("Updated On")]
         public DateTime?  UpdatedOn  {get;set;}
        [DisplayName("Is Active")]
         public bool IsActive { get; set; }
      
    }
}

 public class BLRSDBEntities :DbContext{
 public System.Data.Entity.DbSet<BLRS.Models.tblCompanye> tblCompanyes { get; set; }
}

The name of the connection string must match the name of the DbContext class.

Create  a Controller:
  public class CompanyController : Controller
    {
        private BLRSDBEntities db = new BLRSDBEntities();

        // GET: /Company/
        public ActionResult Index()
        {
            return View(db.tblCompanyes.ToList());
        }
        public ActionResult MyImage(int id, int img)
        {
            ViewBag.Pic = img;
            tblCompanye insp = db.tblCompanyes.Find(id);
            return View(insp);
        }
        // GET: /Company/Details/5
        public ActionResult Details(decimal id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            tblCompanye tblcompanye = db.tblCompanyes.Find(id);
            if (tblcompanye == null)
            {
                return HttpNotFound();
            }
            return View(tblcompanye);
        }

        // GET: /Company/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: /Company/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(tblCompanye inspection, HttpPostedFileBase file1)
        {
            if (ModelState.IsValid)
            {
                string filename = "";
                byte[] bytes;
                int BytestoRead;
                int numBytesRead;

                if (file1 != null)
                {
                    filename = Path.GetFileName(file1.FileName);
                    bytes = new byte[file1.ContentLength];
                    BytestoRead = (int)file1.ContentLength;
                    numBytesRead = 0;
                    while (BytestoRead > 0)
                    {
                        int n = file1.InputStream.Read(bytes, numBytesRead, BytestoRead);
                        if (n == 0) break;
                        numBytesRead += n;
                        BytestoRead -= n;
                    }
                    inspection.CompLogo = bytes;
                }
                //inspection.CompID=
                inspection.CompID = Common.GetMaxIDInt("tblCompanyes", "CompID");
                inspection.InsertedBy = "Base";//Session["USERID"].ToString().Trim();
                inspection.InsertedOn = System.DateTime.Now;

                db.tblCompanyes.Add(inspection);
                db.SaveChanges();
                return RedirectToAction("Index");

            }

            return View(inspection);
        }

        //public ActionResult Create([Bind(Include="CompID,CompCode,CompName,CompAddr,PhoneNo,MobileNo,Fax,Email,WebURL,CompLogo,RefID,InsertedBy,InsertedOn,UpdatedBy,UpdatedOn,IsActive")] tblCompanye tblcompanye)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        db.tblCompanyes.Add(tblcompanye);
        //        db.SaveChanges();
        //        return RedirectToAction("Index");
        //    }

        //    return View(tblcompanye);
        //}

        // GET: /Company/Edit/5
        public ActionResult Edit(decimal id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            tblCompanye tblcompanye = db.tblCompanyes.Find(id);
            if (tblcompanye == null)
            {
                return HttpNotFound();
            }
            return View(tblcompanye);
        }

        // POST: /Company/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]

        //public ActionResult Edit([Bind(Include="CompID,CompCode,CompName,CompAddr,PhoneNo,MobileNo,Fax,Email,WebURL,CompLogo,RefID,InsertedBy,InsertedOn,UpdatedBy,UpdatedOn,IsActive")] tblCompanye tblcompanye)
        public ActionResult Edit(tblCompanye inspection, HttpPostedFileBase file1)
        {

            if (ModelState.IsValid)
            {
                string filename = "";
                byte[] bytes;
                int BytestoRead;
                int numBytesRead;

                if (file1 != null)
                {
                    filename = Path.GetFileName(file1.FileName);
                    bytes = new byte[file1.ContentLength];
                    BytestoRead = (int)file1.ContentLength;
                    numBytesRead = 0;
                    while (BytestoRead > 0)
                    {
                        int n = file1.InputStream.Read(bytes, numBytesRead, BytestoRead);
                        if (n == 0) break;
                        numBytesRead += n;
                        BytestoRead -= n;
                    }
                    inspection.CompLogo = bytes;
                }
                inspection.UpdatedBy = "Base";//Session["USERID"].ToString().Trim();
                inspection.UpdatedOn = System.DateTime.Now;
                db.tblCompanyes.Add(inspection);
                db.Entry(inspection).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
          

          


            //if (ModelState.IsValid)
            //{
            //    db.Entry(tblcompanye).State = EntityState.Modified;
            //    db.SaveChanges();
            //    return RedirectToAction("Index");
            //}
            return View(inspection);
        }

        // GET: /Company/Delete/5
        public ActionResult Delete(decimal id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            tblCompanye tblcompanye = db.tblCompanyes.Find(id);
            if (tblcompanye == null)
            {
                return HttpNotFound();
            }
            return View(tblcompanye);
        }

        // POST: /Company/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(decimal id)
        {
            tblCompanye tblcompanye = db.tblCompanyes.Find(id);
            db.tblCompanyes.Remove(tblcompanye);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }


In Views:


CREATE:



@model BLRS.Models.tblCompanye

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Company Create</title>
</head>
<body>
    <form name="datapluspics" method="post" enctype="multipart/form-data">
        @*@using (Html.BeginForm())
            {*@
        @Html.AntiForgeryToken()

        <div class="form-horizontal">
            <h4>tblCompanye</h4>
            <hr />
            @Html.ValidationSummary(true)

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

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

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

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

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

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

            <div class="form-group">
                @Html.LabelFor(model => model.Fax, new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.Fax)
                    @Html.ValidationMessageFor(model => model.Fax)
                </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">
                @Html.LabelFor(model => model.WebURL, new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.WebURL)
                    @Html.ValidationMessageFor(model => model.WebURL)
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.CompLogo, new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    <input type="file" id="file1" name="file1" />
                    @*@Html.EditorFor(model => model.CompLogo)
                    @Html.ValidationMessageFor(model => model.CompLogo)*@
                </div>
            </div>

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

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

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

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

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

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

            <div class="form-group">
                <div class="col-md-offset-2 col-md-10">
                    <input type="submit" value="Create" class="btn btn-default" />
                </div>
            </div>
        </div>
    </form>

        <div>
            @Html.ActionLink("Back to List", "Index")
        </div>
</body>
</html>
DELETE:
@model BLRS.Models.tblCompanye

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Delete</title>
</head>
<body>
    <h3>Are you sure you want to delete this?</h3>
    <div>
        <h4>tblCompanye</h4>
        <hr />
        <dl class="dl-horizontal">
            <dt>
                @Html.DisplayNameFor(model => model.CompCode)
            </dt>
   
            <dd>
                @Html.DisplayFor(model => model.CompCode)
            </dd>
   
            <dt>
                @Html.DisplayNameFor(model => model.CompName)
            </dt>
   
            <dd>
                @Html.DisplayFor(model => model.CompName)
            </dd>
   
            <dt>
                @Html.DisplayNameFor(model => model.CompAddr)
            </dt>
   
            <dd>
                @Html.DisplayFor(model => model.CompAddr)
            </dd>
   

        </dl>
   
        @using (Html.BeginForm()) {
            @Html.AntiForgeryToken()
   
            <div class="form-actions no-color">
                <input type="submit" value="Delete" class="btn btn-default" /> |
                @Html.ActionLink("Back to List", "Index")
            </div>
        }
    </div>
</body>
</html>

Details:
@model BLRS.Models.tblCompanye

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Companye Detail</title>
</head>
<body>
    <div>
        <h4>Companye Detail</h4>
        <hr />
        <dl class="dl-horizontal">
            <dt>
                @Html.DisplayNameFor(model => model.CompCode)
            </dt>
   
            <dd>
                @Html.DisplayFor(model => model.CompCode)
            </dd>
   
            <dt>
                @Html.DisplayNameFor(model => model.CompName)
            </dt>
   
            <dd>
                @Html.DisplayFor(model => model.CompName)
            </dd>
   
            <dt>
                @Html.DisplayNameFor(model => model.CompAddr)
            </dt>
   
            <dd>
                @Html.DisplayFor(model => model.CompAddr)
            </dd>
   
            <dt>
                @Html.DisplayNameFor(model => model.PhoneNo)
            </dt>
   
            <dd>
                @Html.DisplayFor(model => model.PhoneNo)
            </dd>
   
            <dt>
                @Html.DisplayNameFor(model => model.MobileNo)
            </dt>
   
            <dd>
                @Html.DisplayFor(model => model.MobileNo)
            </dd>
   
            <dt>
                @Html.DisplayNameFor(model => model.Fax)
            </dt>
   
            <dd>
                @Html.DisplayFor(model => model.Fax)
            </dd>
   
            <dt>
                @Html.DisplayNameFor(model => model.Email)
            </dt>
   
            <dd>
                @Html.DisplayFor(model => model.Email)
            </dd>
   
            <dt>
                @Html.DisplayNameFor(model => model.WebURL)
            </dt>
   
            <dd>
                @Html.DisplayFor(model => model.WebURL)
            </dd>
   
            <dt>
              @Html.DisplayNameFor(model => model.CompLogo)
            </dt>
   
            <dd>
                @*@Html.DisplayFor(model => model.CompLogo)*@
             
                @if (Model.CompLogo != null)
                {
                    <img alt="tag" src="@Url.Content("/Company/MyImage?id=" + Model.CompID + "&img=1")" />
                }
            </dd>
   
            <dt>
                @Html.DisplayNameFor(model => model.RefID)
            </dt>
   
            <dd>
                @Html.DisplayFor(model => model.RefID)
            </dd>
   
            <dt>
                @Html.DisplayNameFor(model => model.InsertedBy)
            </dt>
   
            <dd>
                @Html.DisplayFor(model => model.InsertedBy)
            </dd>
   
            <dt>
                @Html.DisplayNameFor(model => model.InsertedOn)
            </dt>
   
            <dd>
                @Html.DisplayFor(model => model.InsertedOn)
            </dd>
   
            <dt>
                @Html.DisplayNameFor(model => model.UpdatedBy)
            </dt>
   
            <dd>
                @Html.DisplayFor(model => model.UpdatedBy)
            </dd>
   
            <dt>
                @Html.DisplayNameFor(model => model.UpdatedOn)
            </dt>
   
            <dd>
                @Html.DisplayFor(model => model.UpdatedOn)
            </dd>
   
            <dt>
                @Html.DisplayNameFor(model => model.IsActive)
            </dt>
   
            <dd>
                @Html.DisplayFor(model => model.IsActive)
            </dd>
   
        </dl>
    </div>
    <p>
        @Html.ActionLink("Edit", "Edit", new { id = Model.CompID }) |
        @Html.ActionLink("Back to List", "Index")
    </p>
</body>
</html>
EDIT:
@model BLRS.Models.tblCompanye

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Edit</title>
</head>
<body>
    @*@using (Html.BeginForm())
    {*@
    <form name="datapluspics" method="post" enctype="multipart/form-data">
        @Html.AntiForgeryToken()

        <div class="form-horizontal">
            <h4>Edit Companye</h4>
            <hr />
            @Html.ValidationSummary(true)
            @Html.HiddenFor(model => model.CompID)
            @Html.HiddenFor(model => model.InsertedOn)
            @Html.HiddenFor(model => model.InsertedBy)

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

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

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

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

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

            <div class="form-group">
                @Html.LabelFor(model => model.Fax, new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.Fax)
                    @Html.ValidationMessageFor(model => model.Fax)
                </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">
                @Html.LabelFor(model => model.WebURL, new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.WebURL)
                    @Html.ValidationMessageFor(model => model.WebURL)
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.CompLogo, new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    <input type="file" id="file1" name="file1" />
                    @if (Model.CompLogo != null)
                        {
                            <img alt="tag" src="@Url.Content("/Company/MyImage?id=" + Model.CompID + "&img=1")" />
                        }
                    @*@Html.EditorFor(model => model.CompLogo)
                    @Html.ValidationMessageFor(model => model.CompLogo)*@
                </div>
            </div>

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

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

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

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

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

            <div class="form-group">
                <div class="col-md-offset-2 col-md-10">
                    <input type="submit" value="Save" class="btn btn-default" />
                </div>
            </div>
        </div>
    </form>

        <div>
            @Html.ActionLink("Back to List", "Index")
        </div>
</body>
</html>

Index:

@model IEnumerable<BLRS.Models.tblCompanye>

@{
    ViewBag.Title = "Index";
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <br />
    <p>
        @Html.ActionLink("Create New", "Create")
    </p>
    <table class="table">
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.CompCode)
            </th>
            <th>            
                Logo
            </th>
            <th>
                @Html.DisplayNameFor(model => model.CompName)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.CompAddr)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.PhoneNo)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.MobileNo)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Fax)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Email)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.WebURL)
            </th>
            @*<th>
                @Html.DisplayNameFor(model => model.CompLogo)
                Logo
            </th>*@
            @*<th>
                @Html.DisplayNameFor(model => model.RefID)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.InsertedBy)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.InsertedOn)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.UpdatedBy)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.UpdatedOn)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.IsActive)
            </th>*@
            <th></th>
        </tr>
   
    @foreach (var item in Model) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.CompCode)
            </td>
            <td>
                @*@Html.DisplayFor(modelItem => item.CompLogo)*@
                @if (item.CompLogo != null)
                {
                    <img alt="tag" src="@Url.Content("/Company/MyImage?id=" + item.CompID + "&img=1")" />
                }
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.CompName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.CompAddr)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.PhoneNo)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.MobileNo)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Fax)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Email)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.WebURL)
            </td>
            @*<td>
             
                @if (item.CompLogo != null)
                {
                    <img alt="tag" src="@Url.Content("/CImage/MyImage?id=" + item.CompID + "&img=1")" /> 
                }
            </td>*@
            @*<td>
                @Html.DisplayFor(modelItem => item.RefID)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.InsertedBy)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.InsertedOn)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.UpdatedBy)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.UpdatedOn)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.IsActive)
            </td>*@
            <td>
                @Html.ActionLink("Edit", "Edit", new { id=item.CompID }) |
                @Html.ActionLink("Details", "Details", new { id=item.CompID }) |
                @Html.ActionLink("Delete", "Delete", new { id=item.CompID })
            </td>
        </tr>
    }
   
    </table>
</body>
</html>
MyImage:(For Image Upload)
@model BLRS.Models.tblCompanye

@{
    if (ViewBag.Pic == 1)
    {
        if (Model.CompLogo != null)
        {
            WebImage wi = new WebImage(Model.CompLogo);
            wi.Resize(150, 150, true, true);
            wi.Write();
        }
    }
}

Conventions

The amount of code you had to write in order for the Entity Framework to be able to create a complete database for you is minimal because of the use of conventions, or assumptions that the Entity Framework makes. Some of them have already been noted or were used without your being aware of them:
  • The pluralized forms of entity class names are used as table names.
  • Entity property names are used for column names.
  • Entity properties that are named ID or classnameID are recognized as primary key properties. 
  • A property is interpreted as a foreign key property if it's named <navigation property name><primary key property name> (for example, StudentID for the Student navigation property since the Student entity's primary key is ID). Foreign key properties can also be named the same simply <primary key property name> (for example, EnrollmentID since the Enrollment entity's primary key is EnrollmentID).


 




No comments:

Post a Comment