Showing posts with label ENTITY FRAMEWORK 6 USING MVC 5. Show all posts
Showing posts with label ENTITY FRAMEWORK 6 USING MVC 5. Show all posts

Thursday, April 3, 2014

Uses Of Data Annotation Attributes To Control The Behavior of The Model Classes

Uses of Data Annotation Attributes 

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

namespace DhakaUniversity.Models
{
    public class Student
    {
        public int ID { get; set; }
         [Required]
         [StringLength(50)]
         [Display(Name = "Last Name")]
        public string LastName { get; set; }
         [Required]
         [StringLength(50, MinimumLength = 1, ErrorMessage = "First name cannot be longer than 50  characters.")]
         [Column("FirstName")]
         [Display(Name = "First Name")]
        public string FirstMidName { get; set; }
         [DataType(DataType.Date)]
         [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = True )]
         [Display(Name = "Enrollment Date")]
        public DateTime EnrollmentDate { get; set; }
         [Display(Name = "Full Name")]
        public string FullName
        {
            get
            {
                return LastName + ", " + FirstMidName;
            }
        }
        //A course can have any number of students enrolled in it, so the Enrollments navigation property is a collection:
        public virtual ICollection<Enrollment> Enrollments { get; set; }
    }
}


Thursday, March 20, 2014

How To Remove Comma and Decimal point from decimal Type data in asp.net mvc5

To Remove comma and decimal point of decimal type property

For Example:
We want to get Value  54321    Instead of  54321.00 

Just Use The Attribute in The model class:

[DisplayFormat(DataFormatString = "{0}")]

e.g.
 public class Mn{

        [DisplayFormat(DataFormatString = "{0}")]

         public decimal NoOfInstallments{ get; set; }

}


And Also Use(carefully):

 TextBoxFor() Instead of  EditFor()

e.g.

 @Html.TextBoxFor(model => model.NoOfInstallments)

For Example:
To Get Value  54321    Instead of  54321.00

Wednesday, March 19, 2014

How To Create a Dynamic DropdownList in ASP.NET MVC5 Step by Step

Dynamic DropdownList in ASP.NET MVC5 Step By Step

Create Two DB Tables e.g.

1.Designstions
Colunns: DesignationID(PK,FK,numeric(6,0),not null)
                Designation (Varchar(50),null)


2.Persons
Columns: PersonID(PK,numeric(6,0),not null)
                 DesignationID(FK,numeric(6,0),not null)


Create The Models:

Designation.cs

{

   public decimal DesignationID { get; set; }
    public string Designation { get; set; }

}

Person.cs

{

 
    public decimal PersonID{ get; set; }
     [DisplayName("Designation")]
   public decimal DesignationID{ get; set; }
 
   public virtual  Designation designation { get; set; }

 

PersonController.cs

{

  public class PersonController: Controller

{

     public ActionResult Index()
        {
            var  mamun= db.Persons.Include(t => t.designation );
            return View(mamun.ToList());
        }


 public ActionResult Create()
        {

          ViewBag.DesignstionID= new SelectList(db.Designstions, "DesignstionID", "Designstion");
            return View();
        }

  public ActionResult Edit(decimal id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Person person = db.Persons.Find(id);
            if (tblsalesperson == null)
            {
                return HttpNotFound();
            }
         ViewBag.DesignstionID= new SelectList(db.Designstions, "DesignstionID", "Designstion",person.DesignstionID);
                   return View(person);
        }

}

}

Create.cshtml & Edit.cshtml:

<div class="form-group">
                @Html.Label("Designation", new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.DropDownList("
DesignstionID")
                    @Html.ValidationMessageFor(model => model.
DesignstionID)
                </div>
            </div>

To Add CSS class  'dropdownList' ,Use:

@Html.DropDownList("DesignstionID", (IEnumerable<SelectListItem>)ViewBag.DesignstionID, new { @class = "dropdownList" })  Instead Of that.


Index: 

 @foreach (var item in Model) 

{

    <td>
                    @Html.DisplayFor(modelItem => item.
Designation.Designation)      
  </td

To Get a Fanstatic One:

 

 

 

 

 

 

 

Wednesday, March 5, 2014

How To Customize The Style of All The DropDownLists in ASP.NET MVC 5 Razor

To Customize The Style of All The DropDownLists  in ASP.NET MVC 5 Razor

 Just Add The Class To bootstrap.min.css

.col-md-10 select {
    -webkit-border-radius: 4px;
    -moz-border-radius: 4px;
    border-radius: 4px;
    border: 1px solid #848484;
    outline: 0;
    height: 25px;
    padding-left: 3px;
    margin-left: 5px

}

How To Add CSS Class For EditFor To Customize All The TextBoxes in ASP.NET MVC 5 Razor

To Add CSS Class For  EditFor To Customize All The TextBoxes in ASP.NET MVC 5 Razor

 Just Add The Class To bootstrap.min.css

.col-md-10 input{
    -webkit-border-radius: 4px;
    -moz-border-radius: 4px;
    border-radius: 4px;
    border: 1px solid #848484;
    outline: 0;
    height: 25px;
    padding-left: 3px;
    margin-left: 5px

}