Posts

Showing posts from June, 2018

How can we enable data annotation validation on client side in MVC?

 This can be done in two steps. Step 1:  In this step we will add necessary Jquery files. <script src="<%= Url.Content("~/Scripts/jquery-1.5.1.js") %>" type="text/javascript"></script> <script src="<%= Url.Content("~/Scripts/jquery.validate.js") %>" type="text/javascript"></script> <script src="<%= Url.Content("~/Scripts/jquery.validate.unobtrusive.js") %>" type="text/javascript"></script>  Step -2 :   In this step to call the EnableClientValidation method. <% Html.EnableClientValidation(); %> After completing these two steps , you can see the validation message.

Remote attribute in MVC5

As we know we have different types of attributes in MVC5 . 1. Binding Attribute. 2. Handle Error  attribute 3. Remote attribute  4. Hidden Input attribute. In this Tips we will discuss about the Remote attribute. Let me give you an example. Basically When you we register  a user details or when we open a new account in google we check that given email is exits previously or not . If the given email is exists then a error message is appeared like " The given email is exits .Please provide a different one. " . So this email checking is happening without any Form Post request . So this type of request can be handled by " Remote attribute " in MVC5. Lets take an example here . We can apply to an Model property like in our case Email property.       public class User {     public string Name { get; set; }     [Remote("CheckEmail","User",ErrorMessage="Email is already exist")]     public s...

How to allow HTML content as Input in MVC

As we know MVC supports built in functionality to protect the  Cross site scripting and SQL injection as well.  But sometimes as per our requirements we need some HTML content as a Input like in blogging application. So to consider the HTML content as Input  we have different way to handle in MVC5 application . CASE - 1 :      In this case we will allow  to a  post type action method to accept the HTML content like     [HttpPost][ValidateInput(False)] public  ActionResult Create([Bind(Include =  "Id,Title,Description,Image,IsActive,PostedBy,PostedOn" )] Blog blog) {      if  (ModelState.IsValid) {         db.Blogs.Add(blog);         db.SaveChanges();          return  RedirectToAction( "Index" );     }   ...

Unable to retrieve metadata while adding controller scaffolding in MVC5.

Actually I really got the the exact error message while I am trying to add a controller scaffolding in MVC5.    Unable to retrieve metadata for 'CRM.DAL.Blog'. No connection string named     'BlogsEntities' could be found in the application config file.' First of all for this above problem we have lots of reason/solutions but for me this below solutions works for me .   So I checked both entity framework  config file and application config file and from there itself  I got the reason why I was getting the error.  Actually I had  the same content in the both the connection string but have different connection string name. It was resolved my issue .  This was content for App.config file . <add name=" BlogsEntities " connectionString="metadata=res://*/Blogs.csdl|res://*/Blogs.ssdl|res://*/Blogs.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=BIMALN4567\SQLSRV16;initial catalog=Blogs;i...

What is template reference variable in Angular

 As we belongs to Web application developer so most of the times we need the data of an event in our applications. So If we talk about the Angular we basically  send the event data from template and retrieve it in component property.          So we can send the data from template as a event object or by template reference variable. But most of the time we  need very less data from an control event but we send the  complete event object to back end. Lets discuss it by taking a small example @ @ComponentCom({ selector : 'app-Keyup' , template : ` <input (keyup)="onKey($event)"> <p>{{values}}</p> ` }) export class KeyUpComponent { values = '' ; onKey(event: any) { this .values += event.target.value + ' | ' ; } } So in this above example to get the value of the Input field we are sending the event object which is not needed As because If we really work on a big project unnecessarily it wil...

What is the difference between Temp data, View, and View Bag?

Like ASP.net web forms in MVC we have also  some way to keeping the data or passing the data from one request to another request or you can passing the data from one controller to another controller as well.    1.  Temp data: It helps to maintain data when you pass data from one controller to other controller. Its a property  of controllerbase class. Its life ended when the view is loaded completely. Mostly it is used to store validation messages. The life time can be managed by Keep() method. 2.   View data: It helps to maintain data when you move from controller to view. Its a dictionary of objects. It takes a key as string like  ViewData["MyData"] = [Value you want to store] Example: public  ActionResult Index()   {         List<string> Teacher =  new  List<string>();         Teacher .Add( "John" );   ...