Posts

Showing posts from April, 2017

MVC handle multiple submit buttons in ASP.NET MVC.

In MVC we can handle the multiple submit buttons in same post type action. The following code snipt will help you to implement this kind of scenario. Step 1: Add the following two actions in your MVC controller. public ActionResult Test() {   return View(); } [HttpPost] public ActionResult Test(string submitBtn) {   switch (submitBtn)   {     case "Save":     //Perform save functionality.     break;     case "Cancel":     //Manage cancel button click     break;   }   return View(); } Step 2: Now create new view with following html. In this html you can see two submit buttons in one Form. So everytime when we will click on any submit button then the "Test" method will be called. @{     ViewBag.Title = "Test"; } <h2>TestR</h2> @using (Html.BeginForm("Test", "Home", FormMethod.Post)) {     <input type="text" name="FirstName" /> ...

MVC Create custom Display templates

Step 1: Add DisplayTemplates named folder in Views > Shared. Step 2:- Create template that you want to render like below:- Views > Shared > DisplayTemplates  > ToolTip.cshtml <div class="tooltip-block animation"> <span class="right-arrow"></span> <div class="tooltip-pads">     <img alt="" src="@Url.Image("tooltip-icon.jpg")" />     <p>@ViewBag.TooltipText</p> </div> <!-- tooltip-pads --> </div> <!-- tooltip-block --> Step 3:- Now use it in view as bellow:- In .cshtml View :- @Html.Display("", "ToolTip", new { TooltipText = "This is the total number of estimate pledgers for the duration of the Campaign." })

MVC Implementing Dependency Injection.

Please follow the following steps to implement the dependency injection in the MVC. Step1:- Install "Unity.MVC4" Nuget Package. Step2:- Map interfaces with classes as followimg   private static IUnityContainer BuildUnityContainer()        {            var container = new UnityContainer();            // register all your components with the container here            // it is NOT necessary to register your controllers            // e.g. container.RegisterType<ITestService, TestService>();              container.RegisterType<IUser, UserComponent>();            container.Reg...

MVC Submit form using AJAX

In MVC often we need to write some code after submit the form to display notifications. For this solution I have created a js function that capture the submit button's click and post the form asynchronously. For more info check this out. Step 1:  Create "wireUpSubmitForm" function in your js file like below. This wireUpSubmitForm accepts 3 parameters. First parameter is id of form. Second parameter is success callback function that will execute on success of post request. 3rd parameter is error callback function that will execute on any server error while making a post request to the server. In this method I am fetching the request url with id of form like '$(formId).attr('action')'. function  wireUpSubmitForm(formId, successCallback, errorCallBack) {     $(formId).submit(function (event) {         event.preventDefault();         if ($(formId).valid()) {             $.ajax({    ...

MVC custom authorization filter

Step 1:  Create new CustomAuthorizeAttribute.cs class and inherit it from AuthorizeAttribute. Now write the following code in that class.  public class  CustomAuthorizeAttribute  :  AuthorizeAttribute     {           //For handle single role         public string  Role  = string. Empty ;         public override void  OnAuthorization ( AuthorizationContext  actionContext )         {             if  (! SkipAuthorization (actionContext))             {                 var  sessionId  =  actionContext . HttpContext . Request . Headers . Get (" SessionId ");                   if  ( string .IsNullOrEmpty(sessionId))                 ...

MVC - Create email template with cshtml file.

Image
 Step 1:  Install RazorEngine nuget package.  After successfuly install this nuget package you can see the RazorEngine dll in your Refrences: Step 2:  Create new cshtml file under Views > Shared > UserEmailTemplate.chtml file. @model  Project.Web.Models. UserModel @using  TranzgridDataManager.Core.Common; @ {     Layout =  null ; } <table  style ="background-color:skyblue"  cellspacing ="10">     <thead>         <tr>             <th  style =" border-bottom :1px solid white;text-align:left;  padding-left :10px; "  colspan ="3"><h2>  User info </h2></th>         </tr>     </thead>     <tbody>         <tr>             <td  style =" padding-left:10px; "> First ...

MVC Implementing FormsAuthentication.

In this article I am showing you the simplest way to implement FormsAuthentication in your MVC application. In this article I am going to use FormsAuthenticationTicket for encrypt and store logged in user data with FormsAuthenticaiton.  Step 1:   Add "authentication mode=" Forms "" under <system.web> tag in your web.config file. Modify your web.config file according to following screen. <authentication mode="Forms">       <forms name="BillAndPayCookie" defaultUrl="/Account/Index" loginUrl="/Account/Index">     </forms> </authentication> Screen 1. Step 2:  Add new class named LoginModel.cs in your Models folder and paste the following code in this file.  public class LoginModel     {         [Required]         public string Email { get; set; }         [Required]         public string Password { get; set; } ...