adsense

Sunday, May 30, 2021

ASP.NET MVC delete multiple records

 When it comes to deleting multiple records in ASP.NET MVC, you have two options,

 1. Connected mode

using (CourseContext db = new CourseContext())
    {
 
        List<Course> courses = db.Courses.Take(5).ToList();

        try
        { 

           db.Courses.RemoveRange(courses);
            db.SaveChanges();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
  

2. Disconnected mode

ListCourse> courses = new List<Course>();
courses.Add(new Course { CourseID = 1 });
courses.Add(new Course{ CourseID = 2 });
 
using (CourseContext db = new CourseContext())
    {
  
        try
        {

            db.Entry(Course).State= System.Data.Entity.EntityState.Deleted;
            db.SaveChanges();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
 
    }


Cheers,

Samitha

Saturday, May 15, 2021

readonly checkbox

 We can use javascript to make a checkbox read only(not disabled) as shown below


<input type="checkbox" checked onkeydown="return false;" onclick="return false;" />

if you want to disable check using keyboard modify above as follows.


<input type="checkbox" checked onclick="return false;" onkeydown="e = e || window.event; if(e.keyCode !== 9) return false;"/>


Cheer

Samitha

Sunday, May 2, 2021

Add icons to links using jquery

If you want to add icons besides your hyperlinks, jQuery can be used to do it as shown below

1. Define Syles

a.pdf { 
    background: url(images/pdf.png) no-repeat left center;
    padding-left: 20px;
    line-height: 16px; /* used to center the image with text */
}
 
a.txt { 
    background: url(images/txt.png) no-repeat left center;
    padding-left: 20px;
    line-height: 16px;
}

a.email {
    background: url(images/email.png) no-repeat left center;
    padding-left: 20px;
    line-height: 16px;
}

2. Add the style using jQuery DOM ready or pageLoad

$(function() {
 
    $("a[href$='.pdf']").addClass("pdf");
 
    $("a[href$='.doc'], a[href$='.txt'], a[href$='.rft']").addClass("txt");
 
    $("a[href^='mailto:']").addClass("email");
 

});

Cheers,

Samitha