;WITH cte
AS (SELECT ROW_NUMBER() OVER (PARTITION BY Col1, Col2, Col3
ORDER BY ( SELECT 0)) RN
FROM #MyTable)
DELETE FROM cte
WHERE RN > 1
Thursday, August 13, 2015
how-can-i-remove-duplicate-rows
nth max salary in sql
WITH CTE AS
(
SELECT EmpID,EmpName,EmpSalar,
RN = ROW_NUMBER() OVER (ORDER BY EmpSalary DESC)
FROM dbo.Salary
)
SELECT EmpID,EmpName,EmpSalar
FROM CTE
WHERE RN = @NthRow
Monday, August 10, 2015
interface with same method name in c#
interface IPrinter
{
void print(string a);
}
interface IPrinter1
{
void print(string a);
}
class Program
{
static void Main(string[] args)
{
IPrinter p = new Printer();
p.print("rahul");
Printer p1 = new Printer();
((IPrinter)p1).print("overlaoded");
}
}
class Printer : IPrinter, IPrinter1
{
void IPrinter.print(string a)
{
Console.Write(a + "Printer");
Console.ReadLine();
}
void IPrinter1.print(string a)
{
Console.Write(a+"Printer1");
Console.ReadLine();
}
}
Thursday, July 9, 2015
difference between save and insert in mongodb
If the document contains an _id field, then the save() method performs an upsert querying the collection on the _id field: If a document does not exist with the specified _id value, the save() method performs an insert with the specified fields in the document. |
Tuesday, May 26, 2015
Child Actions in ASP.NET MVC
Child Actions are the action methods which can be invoked within the view. This is used to work with the data in the view, which are not related to the main action method. For example, if you want to create a data driven widget in the view using data that is not related to the main action method, you will need to use the child action method.
In ASP.NET MVC any action can be used as a child action. However, to use an action only as a child action and attribute it with the ChildActionOnly. It will make sure the action is not called by any user request and will only be used in the view. A child action can be created as shown below:
1
2
3
4
5
|
[ChildActionOnly]
public ActionResult CurrentTime()
{
return PartialView(DateTime.Now);
}
|
CurrentTime.cshtml
1
2
3
|
@model DateTime
<p>Current time : @Model.ToShortTimeString() </p>
|
1
|
@Html.Action("CurrentTime");
|
1
2
3
4
5
|
[ChildActionOnly]
public ActionResult CurrentTime(object name)
{
return PartialView(name);
}
|
1
|
@Html.Action("CurrentTime", new { name = "Steve" });
|
- To use data in the view which is not related to the main controller
- To create data driven widgets to be used on different views
Custom Action Filter in ASP.NET MVC 5
ASP.NET MVC 5 provides five different kinds of Filters. They are as follows:
- Authentication [Introduced in MVC5]
- Authorization
- Action
- Result
- Exception
- Authentication filter runs before any other filter or action method
- Authorization filter runs after Authentication filter and before any other filter or action method
- Action filter runs before and after any action method
- Result filter runs before and after execution of any action result
- Exception filter runs only if action methods, filters or action results throw an exception
Action Filter
An action filter consists of codes that run either before or after an action runs. It can be used for tasks like logging, privileged based authorization, authentication, caching etc.
Creating a custom action filter is very easy. It can be created in four simple steps:
- Create a class
- Inherit ActionFilterAttribute class
- Override the OnActionExecuting method to run logic before the action method
- Override the OnActionExecuted method to run logic after the action method
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
using PrivilegeDemo.Services;
using System.Web;
using System.Web.Mvc;
using Microsoft.AspNet.Identity;
using System.Web.Routing;
namespace PrivilegeDemo.Filters
{
public class AuthorizationPrivilegeFilter : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
AuthorizationService _authorizeService = new AuthorizationService();
string userId = HttpContext.Current.User.Identity.GetUserId();
if (userId != null)
{
var result = _authorizeService.CanManageUser(userId);
if (!result)
{
filterContext.Result = new RedirectToRouteResult(
new RouteValueDictionary{{ "controller", "Account" },
{ "action", "Login" }
});
}
}
else
{
filterContext.Result = new RedirectToRouteResult(
new RouteValueDictionary{{ "controller", "Account" },
{ "action", "Login" }
});
}
base.OnActionExecuting(filterContext);
}
}
}
|
- As Global filter
- As Controller
- As Action
1
2
3
4
5
6
7
8
|
public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
filters.Add(new AuthorizationPrivilegeFilter());
}
}
|
1
2
3
|
[AuthorizationPrivilegeFilter]
public class HomeController : Controller
{
|
1
2
3
|
[AuthorizationPrivilegeFilter]
public ActionResult About()
{
|
Custom HTML Helper for MVC Application
The solution to avoid this is to build a custom HTML helper to
encapsulate everything and make the repetitive task less cumbersome.
Let's say we want to avoid doing this many times inside your views:
Let's say we want to avoid doing this many times inside your views:
<img src="@myModel.MyObject.ImageSource", alt ="@myModel.MyObject.Imagename",
title ="@myModel.MyObject.ImageDescription" />
And instead, you want something Razor like that may take care of some
logic or validations to avoid error because in the previous snippet,
the ImageSource
or Imagename
or again the description may be null
:
@Html.Image(@myModel.MyObject.ImageSource,
@myModel.MyObject.Imagename, @myModel.MyObject.ImageDescription)
namespace MyNamespace
{
public static class MyHeleprs
{
public static MvcHtmlString Image(this HtmlHelper htmlHelper,
string source, string alternativeText)
{
//declare the html helper
var builder = new TagBuilder("image");
//hook the properties and add any required logic
builder.MergeAttribute("src", source);
builder.MergeAttribute("alt", alternativeText);
//create the helper with a self closing capability
return MvcHtmlString.Create(builder.ToString(TagRenderMode.SelfClosing));
}
}
}
To make this helper available in your view, add its namespace
as follows:
@namespace MyNamespace
But if you want it available in all your views, you have to add the namespace
not to a specific view but to the collection of namespace
s in views' web.config:
<add namespace="MyNamespace" />
Now, the Image
helper is available everywhere in your views.
Subscribe to:
Posts (Atom)
ASP.NET Core
Certainly! Here are 10 advanced .NET Core interview questions covering various topics: 1. **ASP.NET Core Middleware Pipeline**: Explain the...
-
The error message you encountered ("DeleteService FAILED 1072: The specified service has been marked for deletion") indicates tha...
-
replace html of a div using jquery this is simple . just use .html() method of jquery to set new html for a div . $ ( "#divID...
-
declare @ProductIds nvarchar(50)='18,19' SELECT * FROM products Where (',' + @ProductIds +',' LIKE '%,' ...