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:

 

 

 

 

 

 

 

Static DropdownList in MVC 5 Razor

Static List of Items in a DropdownList using MVC 5 

Create.cshtml

 <div class="form-group">

                @Html.LabelFor(model => model.ProjectStatus, new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                @{
                 List<SelectListItem> lstItems = new List<SelectListItem>();

                lstItems.Add(new SelectListItem
           
                {
                  Text = "Upcoming",
                  Value = "1",
                 //Selected=true
                });

               lstItems.Add(new SelectListItem
               {

               Text = "Ongoing",
               Value = "2"
       

               });
              lstItems.Add(new SelectListItem
                {

                Text = "Ready",
                  Value = "3"
         

                 });


                    }
                    @Html.DropDownList("ProjectStatus", lstItems)
                    @Html.ValidationMessageFor(model => model.
ProjectStatus)
                </div>
            </div>




To add CSS Class 'dropdownList'  : 

@Html.DropDownList("ProjectStatus",listItems,new{@class="dropdownList"}) 

Index.cshtml 

<td>
    @{


  @(item.ProjectStatus.ToString()=="1"?"Upcoming":

(item.ProjectStatus.ToString()=="2"?"Ongoing":"Ready"))                
      }
  </td>

To Get:

 

 

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

}