Posts

Showing posts from March, 2020

How To Use Bind Attribute In Asp.Net MVC

Image
In this article we will understand how to use Bind Attribute in Asp.Net MVC By Sagar Jaybhay . Also we will Understand Including And Excluding Properties In Model Binding. Bind Attribute In Asp.Net MVC In a previous article or aove we understand how to create IncludeList and Exclude List but there is another way to do the same functionality by using Bind class. Bind class has several overloaded methods by using this ou can Include and Exclude several properties from the strongly typed model. Below is the code for that. [HttpPost] // Bind Function public ActionResult Edit([Bind(Include = "EmpID,EmpGender,EmpSalary, EmpCity, EmpEmail, DepartmentID")]Employee employee) var emp = new BusinessLogic.Business().GetEmployee(employee.EmpID.ToString()); employee.EmpName = emp.EmpName; // UpdateModel(emp, new string[] "EmpSalary", "EmpGender", "EmpCity", "EmpEmail", "DepartmentID" ); if (ModelState.IsValid) new BusinessLogic.B

Uninted Updates In Asp.Net MVC

Image
In this article we will understand if we don't want to update certain fields means Uninted Updates how we acheive that in Asp.Net MVC By Sagar Jaybhay. Uninted Updates In the previous article, we know how to Edit the details but here as you see all fields are editable and you will able to change that all. but we don’t want to change that ID and Name field and want to mark this as read-only to do this we change the Html helper method from EditFor to DisplayFor. By doing this we have below UI when we click on the Save button. To overcome this we need to use HiddenField for code for this is below @Html.ValidationSummary(true, "", new @class = "text-danger" ) <div class="form-group"> @Html.LabelFor(model => model.EmpID, htmlAttributes: new @class = "control-label col-md-2" ) <div class="col-md-10"> @Html.DisplayFor(model => model.EmpID, new htmlAttributes = new @class = "form-control" ) @Html.HiddenFor(

How to Perform Edit Operation In Asp.Net MVC

Image
In this article we will understand How to Perform Edit Operation in Asp.Net MVC By Sagar Jaybhay. How to Perform Edit Operation In Asp.Net MVC Created Edit method in EmployeeController which is shown below and strongly typed EditView for this. [HttpGet] public ActionResult Edit(string EmpID) var Employye = new BusinessLogic.Business().GetEmployee(EmpID); return View(Employye); Edit Model In Asp.net MVC Now after creating a view and click on edit link you will find below the window Edit Employee In Asp.Net MVC But if you see above image gender will display in textbox but we want dropdown list so we need to modify this as per our requirement. We modify our view, but in this, you should rename DropDownList Name to EmpGender as our model property is EmpGender so it will bind value directly. @model WebApplication1.Models.Employee @ ViewBag.Title = "Edit"; <h2>Edit</h2> @using (Html.BeginForm()) @Html.AntiForgeryToken() <div class="form-horizontal"

Model Binding UpdateModel TryupdateModel

Image
In this article we will understand model binding updateModel tryupdateModel -mvc ASP.NET MVC By Sagar Jaybhay . But for this you need to read previous article where we start CRUD Operation in MVC . Model Binding [HttpPost] public ActionResult Create(string EmpName, string EmpSalary, string gender, string EmpEmail, string EmpCity, int departmentid ) var employee = new Employee() EmpName= EmpName, EmpSalary=Convert.ToDouble(EmpSalary), EmpCity= EmpCity, EmpEmail= EmpEmail, EmpGender=gender, DepartmentID=departmentid ; new BusinessLogic.Business().CreateEmployee(employee); return RedirectToAction("DisplayCompleteEmployee"); In this above method, we use the method parameter and when we click on Create button this method is called. Also, we can use FormCollection in this which required the key to access data and we get this key from formcollectionobject.allkeys get all keys. But how the form textboxes are bind correct data to parameters in that method is by using

CRUD Operations In MVC Sagar Jaybhay

Image
In series of this post you will able to understand CRUD Operations In MVC by Sagar Jaybhay. CRUD Operations In MVC In this, we have a scenario we need to display the top 25 employees and perform Edit, Display and Delete Operation on Employee. Display Employee List To achieve above this we need to create a method in Business class to get the top 25 employees for that method is below. public IEnumerable<Employee> GetEmployees() List<Employee> employees = new List<Employee>(); string Query = "select top 25 * from Employee"; var data = this.dataAccess.GetTable(Query); if(data!=null&&data.Rows.Count>0) foreach(DataRow dataRow in data.Rows) var emp = new Employee() EmpID = Convert.ToInt32(dataRow["EmpID"]), EmpCity = dataRow["EmpCity"].ToString(), EmpEmail = dataRow["EmpEmail"].ToString(), EmpGender = dataRow["EmpGender"].ToString(), EmpName = dataRow["EmpName"].ToString(), EmpSalary = Co

Working With Multiple Tables In MVC

Image
In this article we will understand Working With Multiple Tables In MVC(Asp.Net) By Sagar Jaybhay . Working with Multiple Tables in MVC Now we are creating an Employee table with the Department ID field added in the Employee table and another table is created which is Department below having syntax for both tables. create table Employee ( EmpID INT, EmpName VARCHAR(50), EmpSalary DECIMAL(8,2), EmpGender VARCHAR(50), EmpCity VARCHAR(50), EmpEmail VARCHAR(50), DepartmentID INT ); create table Department ( DepartmentID INT, DepartmentName VARCHAR(50) ); For both table insert data script is attached to here insert into Department (DepartmentID, DepartmentName) values (1, 'Product Management'); insert into Department (DepartmentID, DepartmentName) values (2, 'Engineering'); insert into Department (DepartmentID, DepartmentName) values (3, 'Business Development'); insert into Department (DepartmentID, DepartmentName) values (4, 'Support'); insert into

In Depth Cookies In JavaScript Sagar Jaybhay

Image
In this article we will understand In Depth about Cookies in JavaScript . By reading this article you will able to create Cookies, Delete Cookie By Sagar Jaybhay . If you see web application works on HTTP protocol and the HTTP protocol is a stateless protocol. The meaning of this is when we request some data, web page after filling some information on the webpage the webserver does not remember anything about that request after processing that request but we want our web application to remember users choice. This means if I fill my first name on the page after the subsequent request the page should know what information I filled in that textbox but the page doesn’t remember that so why this happens because web pages use the HTTP protocol to serve the webpage and HTTP is stateless. Meaning that after processing the request of client web server doesn’t remember about the client settings to remember this we have several options but one of the easiest and common way is use cookies. functio

How to generate HyperLink In MVC?

Image
In this article we will learn How to generate HyperLink In MVC(Asp.Net MVC) by Sagar Jaybhay . For this, we need to use Html.ActionLink helper function is used to generate an action link. To understand this better we get first 100 employee list where contain only EmployeeID hyperlink after clicking on this link you will able to access all the details about that EmmployeeID. For this, we create one action method in our EmployeeController which return a list of ID view and If you click on that you will be redirected to Index view which takes one parameter EmpID and displays that employee Detail. https://www.youtube.com/watch?v=390dPStnFt4 First, we write the business logic method to get top 100 EmpIds. public List<int> GetEmpIDs() List<int> ids = new List<int>(); string Query = "select top 100 EmpID from Employee"; var data=this.dataAccess.GetTable(Query); if(data!=null&&data.Rows.Count>0) foreach (DataRow id in data.Rows) ids.Add(Convert.

How to generate HyperLink in Asp.Net MVC by Sagar Jaybhay

Image

Strongly Typed Model In Asp.Net MVC

Image
In this article, you will understand What is Strongly Typed Model In Asp.Net MVC ?. How to Create Strongly Typed Model ? How Get data from DataBase for that model in Asp.Net MVC by Sagar Jaybhay . Srtongly Typed Model The flow of MVC is when you hit URL it first go to the Controllers ' action method, the controller will search for if particular request required data or not if yes then it gets from Model and then this model is the pass to View . Model View Controller in MVC For this, we create one table in SQL server, Create table Employee ( EmpID int, EmpName nvarchar(20), EmpSalary float, EmpGender nvarchar(6), EmpCity nvarchar(20), EmpEmail nvarchar(20) ) Our Model is public class Employee public int EmpID get; set; public string EmpName get; set; public double EmpSalary get; set; public string EmpGender get; set; public string EmpCity get; set; public string EmpEmail get; set; Below we create a strongly typed view and Employee class is our Model which we created in t

ViewBag and ViewData In Asp.Net MVC

Image
In this article we will under stand How to Pass Data From Controller to View by Sagar Jaybhay . How to Pass Data From Controller to View using ViewBag and ViewData? ViewBag and ViewData In Asp.Net MVC Both are used to pass data from controller to view. ViewData is a dictionary of objects which store and retrieve the data based on key. ViewData["Today"] = DateTime.Now; See the above code. https://www.youtube.com/watch?v=7mZ89MvGnzU ViewBag uses the dynamic keyword internally and the dynamic keyword is introduced in C# v4. It allows objects to add as properties. ViewBag.list = list; See the above code. In this, both do not provide compile-time error checking. Means if given ABC as a key name in the controller and in view if you misspell Abc then it will not provide compile-time checking. You will get the error at runtime only. It is good practice to pass data from controller to view using a strongly typed model. ViewBag is a sugar coating over ViewData. Internally ViewB

ViewBag and ViewData

Image

View In Asp.Net MVC By Sagar Jaybhay

Image
In this article you will able to understand what is View In Asp.Net MVC . How to Create View In MVC By Sagar Jaybhay Views In Asp.Net MVC The purpose of view in MVC to render the data in the format that the end-user want. If you want to return the view from the action method which you going to create by default MVC adds the same name to view as an action method name See below image. View In Asp.Net MVC Our application, we add a NameList method and by using the MVC dialog box we are going to add View. The name of the method and view name is the same is MVC default convention and this view is added Under the Views -> Home(Controller Name Folder)-> View See below image. Create View In Asp.Net MVC public ActionResult NameList() var list=new List<string>() "Sagar Jaybhay", "Ram", "Raghu", "Ravan" ; ViewBag.list = list; return View(); https://www.youtube.com/watch?v=NQrOa3cWPOw Above is Controller action method and below i

View In Asp.Net MVC By Sagar Jaybhay

Image
In this article you will able to understand what is View In Asp.Net MVC . How to Create View In MVC By Sagar Jaybhay Views In Asp.Net MVC The purpose of view in MVC to render the data in the format that the end-user want. If you want to return the view from the action method which you going to create by default MVC adds the same name to view as an action method name See below image. View In Asp.Net MVC Our application, we add a NameList method and by using the MVC dialog box we are going to add View. The name of the method and view name is the same is MVC default convention and this view is added Under the Views -> Home(Controller Name Folder)-> View See below image. Create View In Asp.Net MVC public ActionResult NameList() var list=new List<string>() "Sagar Jaybhay", "Ram", "Raghu", "Ravan" ; ViewBag.list = list; return View(); https://youtu.be/NQrOa3cWPOw Above is Controller action method and below is the code for

Abstract Classes Object reflection Strict Mode JavaScript

Image
How to implement Abstract Classes & Object reflection & Strict Mode In JavaScript ? In this article we will understand How do I create an Abstract Classes & Object reflection & Strict Mode in JavaScript By Sagar Jaybhay . Abstract Classes Object-oriented programming languages like C# and java support abstract classes. These classes are incomplete and when you trying to create an object of that abstract classes it will throw a compile-time error. The abstract classes are mainly for base classes. var Shape=function(nameofshape) this.shapeName=nameofshape; throw new Error("You can't create object of this abstract class.") ; Shape.prototype.draw=function() return "Drawing the Shape "+this.shapeName; var Circle=function(name) this.shapeName=name; Circle.prototype=Object.create(Shape.prototype); var circle=new Circle("Circle"); document.writeln("<br/>"); document.writeln("is circle instance of Circle "+(circl