Skip to main content

Posts

Showing posts from 2011

Left outer join in linq

Let's have a scenario in which there are two tables Teachers and Teacher_Courses .Teachers are assigned courses having teacherid in Teacher_Courses table as the foreign key. So to pick up all the Teacher's list with no of courses assigned regardless of having been assigned a course or not in linq you will be using left outer join as follows. using (TeacherContext context = new TeacherContext()) { var Teachers = context.Teachers; var TeacherCourse = context.TeacherCourses; var q = from c in Teachers join o in TeacherCourse on c.int_TeacherId equals o.int_TeacherId into j from Course in j.DefaultIfEmpty().GroupBy(m=>m.int_TeacherId) select new { Teachers = c.vcr_TeacherName, Courses = Course.Count() == 0 ? "(no Courses Assigned)" : Course.Where(m => m.int_TeacherId == c.i

Dotnetnuke Inter-module communication (IMC) simplified on version 06.00.02 with c#

Few days ago I developed a module in which I used IMC which really interested me so I decided to write about it. I will try to explain in this post everything that is necessary to make IMC work in the modules. What is Inter Module Communication? As the name implies if you want to communicate or in other words send data from one module to another IMC is one way of doing it. I will be using module A and module B as the names in my post. An Observation: One thing I observed while playing around with it that if module A is on page 1 and module B is on page 2 then the data doesn't get passed. If both the modules are on the same page then only the data get passed. Example: The basic exercise that I will be performing is to take input from the textbox in Module A and display it in label in Module B. 1) You will be using IModuleCommunicator and IModuleListener interfaces to make this communication works. You will be implementing IModuleCommunicator in the class in the modul

Jquery form plugin with asp.net mvc flavour

I am a great fan of jquery form plugin which is one of my favorites. It's really simple to integrate Ajax in your web pages with jquery form with very thorough documentation found at the site. Basically it really speed up the things. I had extended some of the examples found in the documentation and had created a small implementation of login in Asp.net mvc. So here how it goes. Model: public class User { public string UserName { get; set; } public string Password { get; set; } } Controller: public ActionResult JqueryFormDemo() { return View(); } public string Login(User User) { if(User.UserName=="mazhar" && User.Password=="mazhar") return "You are successfully login" ; else return "Login Failed"; } View: @model MvcApplication5.Models.User <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.

Asp.net mvc razor render partial view using ajax helper

This is the extension to my blog in which I demonstrated rendering of the partial view using jquery Ajax . I want to demonstrate here yet another way by which partial view can be rendered without page refresh. Here is the implementation. Step 1: I will again be using DisplayData class in my demo. Here is it. public class DisplayData { public int ID { get; set; } public DisplayData(int ID) { this.ID = ID; } } Step 2: Create a PartialDemo page @model IEnumerable<MvcApplication5.Models.DisplayData> @{ ViewBag.Title = "PartialDemo"; } @Ajax.ActionLink("Click 1", "PartialDemo", "PartialDemo", new {Data= "1" }, new AjaxOptions { UpdateTargetId = "rsvpmsg" }) @Ajax.ActionLink("Click 2", "PartialDemo", "PartialDemo", new {Data= "2" }, new AjaxOptions { UpdateTargetId = "rsvpmsg" }) <div id="rsvpms

Asp.net mvc razor render partial view using jquery Ajax

I will going to demonstrate how we can render PartialViews using Jquery Ajax. I will be clicking an a href link ,then I will be calling the controller through jquery Ajax which will fill the partialview for a really nice user experience. Step 1: First of all we will be creating an DisplayData class for the use for this example in the model. public class DisplayData { public int ID { get; set; } public DisplayData(int ID) { this.ID = ID; } } Step 2: We will create a Clicks page and write the following code on it. Specially note empty here which will going to empty and then fill partialview with new records. $(document).ready(function () { $('.msg').click(function () { var id = this.id; $.ajax({ url: "/Category/Display", data: { data: id }, success: function (mydata) { $("#link").empty().appe

Google voice actions for android

While playing with my new android phone I came to know of the voice actions which really interested me, so I decided to share it on my blog. Voice actions let you control your phone by voice commands which is really very cool , for example you can search, send text, do browsing all by voice commands. My Review: It is not very exact in some of the cases but it is working. Watch the video for more details

Asp.net mvc 3 built in json model binding

One of the things in mvc3 that recently caught my eyes is built in json model binding support which I have recently used in my project and want to write about it today. JsonValueProviderFactory Class which do all the stuff regarding json model binding is now built-in in mvc 3 which was not in mvc2. To use it in mvc2 you have to register the JsonValueProviderFactory class in global.asax after referencing Microsoft.Web.Mvc from Mvc Futures Library . Here how the thing goes in mvc3 Model Class public class Category { public string CategoryName { get; set; } } The Controller public ActionResult Create() { return View(); } [HttpPost] public ActionResult Create(Category category ) { string Cat = category.CategoryName; var data = new { message = "ok" }; return Json(data);  } The View $(document).ready(function () { $('#save').click(function () { var CategoryName = { CategoryName: $('#CategoryName

My Asp.net mvc,Asp.net, linq and jquery Faqs

Here I want to make the list of every thing about Asp.mvc, linq and jquery I have been involved in or I can get my hands on or find very interesting. The list will keep on growing over time. How to get the current Route Id in the view from Url? <%=Url.RequestContext.RouteData.Values["id"] %>  What is difference between Html.Partial and Html.RenderPartial? Good complete answer here. What is Mvc Futures Library? Please refer here for the answer. http://stackoverflow.com/questions/2734316/asp-net-mvc-futures-refresh-for-mvc2. What is TempData? Answer here What is Difference between ViewData and ViewBag? See here And here Many to Many Relationship in linq to sql ? http://geekswithblogs.net/WinAZ/archive/2010/01/09/simplified-many-to-many-relationships-with-linq-to-sql.aspx http://www.codeproject.com/KB/linq/linq-to-sql-many-to-many.aspx http://www.iaingalloway.com/many-to-many-relationships-in-linq-to-sql-part-2 How to catch error in Jquery's load

Asp.net mvc having multiselect with the listbox

Populate the Listbox with MultiSelect. Controller: public ActionResult Create() { var Degree=DegreeRepository.GetAllDegree(); ViewData["Degree"] = new MultiSelectList(Degree, "DegreeId", "DegreeName"); return View(); } Here I want to mention if you want the single selection for the Listbox you can use SelectList as I mentioned here View <%: Html.ListBoxFor(model => model.DegreeId, ViewData["Degree"] as MultiSelectList) %> Population and then selection of item in the dropdownList.  Controller: public ActionResult Edit(int id) { var Users=UserRepository.GetUser(id); var Degree=DegreeRepository.GetAllDegree(); ViewData["Degree"] = new SectList(Degree,"DegreeId","DegreeName",Users.DegreeId); return View(); } View: <%: Html.DropDownListFor(model => model.DegreeId, ViewData["Degree"]) %>

Asp.net mvc Dropdownlist population and selection

Populate the dropdownList. Controller public ActionResult Create() { var Degree=DegreeRepository.GetAllDegree(); ViewData["Degree"] = new SelectList(Degree, "DegreeId", "DegreeName"); return View(); } View <%: Html.DropDownListFor(model => model.DegreeId, ViewData["Degree"]%> Population and then selection of item in the dropdownList. Controller public ActionResult Edit(int id) { var User=UserRepository.GetUser(id); // Get Single User var Degree=DegreeRepository.GetAllDegree(); // Get All Degrees ViewData["Degree"] = new selectList(Degree,"DegreeId",DegreeName",User.DegreeId); return View(); } View <%: Html.DropDownListFor(model => model.DegreeId, ViewData["Degree"] %>

Asp.net mvc c# changing the dateformat

There are times when you need to change the datetime format of the default date generated by System.DateTime.Now to some other. Here how you can do it string datetFormat = "dd-MMMM-yyyy hh:mm"; string date = Convert.ToDateTime(datetime).ToString(datetFormat); It will generate 11-February-2011 06:13

How to use Asp.net mvc JavaScriptResult practically

I am usually very excited about knowing something that can make my life easy and JavaScriptResult is one of that thing. It is used to execute JavaScript code immediately on the client sent from the server. Here is the small code snippet for its implementation. View:  <%: Ajax.ActionLink("display", "Display", new AjaxOptions()) %> <%: Html.TextBox("name","mazhar") %> <%: Ajax.ActionLink("checks", "Checks", new AjaxOptions()) %> Controller: public ActionResult Display() { var script = "$('#message').append('Display');"; return JavaScript(script); } public ActionResult Checks() { string script = "var textboxvalue=$('#name').val();"; script += "$('#message').append(textboxvalue);"; return JavaScript(script); } Output: 1) Clicking on the first link will display Display in the message div. 2) Cli