Techaray

Shailendra Saxena's Technical Blog

Dependency Injection with AutoFac 2

clock March 13, 2016 18:29 by author ShailendraSaxena

3. UserController

publicclassUserController : Controller

    {

 

        IUserRepository _userrepos;

 

        public UserController( IUserRepository _repository)

        {

            _userrepos = _repository;

        }

 

        // GET: User

        publicActionResult Index()

        {

            var data = _userrepos.GetUserList();

            return View(data);

        }

 

        // GET: User/Details/5

        publicActionResult Details(int id)

        {

            var data = _userrepos.GetUser(id);

            return View(data);

        }

 

        // GET: User/Create

        publicActionResult Create()

        {

            return View();

        }

 

        // POST: User/Create

        [HttpPost]

        publicActionResult Create(User user)

        {

            try

            {

                _userrepos.Add(user);

                _userrepos.SaveChanges();

                return RedirectToAction("Index");

            }

            catch

            {

 

            }

            return View();

        }

 

        // GET: User/Edit/5

        publicActionResult Edit(int id)

        {

            return View();

        }

 

        // POST: User/Edit/5

        [HttpPost]

        //public ActionResult Edit(int id, FormCollection collection)

        publicActionResult Edit(int id, User usr)

        {

            try

            {

                usr.UserId = id;

                // TODO: Add update logic here

                _userrepos.Update(usr);

                _userrepos.SaveChanges();

                return RedirectToAction("Index");

            }

            catch

            {

                return View();

            }

        }

 

        // GET: User/Delete/5

        publicActionResult Delete(int id)

        {

            return View();

        }

 

        // POST: User/Delete/5

        [HttpPost]

        publicActionResult Delete(int id, FormCollection collection)

        {

            try

            {

                // TODO: Add delete logic here

                return RedirectToAction("Index");

            }

            catch

            {

                return View();

            }

        }

    }

4. Autofac install (autofac + autofacMVC)

5. Global.asax.

  protectedvoid Application_Start()

        {

            AreaRegistration.RegisterAllAreas();

            GlobalConfiguration.Configure(WebApiConfig.Register);

            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);

            RouteConfig.RegisterRoutes(RouteTable.Routes);

            BundleConfig.RegisterBundles(BundleTable.Bundles);

 

            var builder = newContainerBuilder();

            builder.RegisterControllers(typeof(MvcApplication).Assembly);

            builder.RegisterType(typeof(UserRepository)).AsImplementedInterfaces();

            //builder.RegisterType(typeof(CustomerRepository)).AsImplementedInterfaces();

            var container = builder.Build();

            DependencyResolver.SetResolver(newAutofacDependencyResolver(container));

 

 

 

        }



Dependency Injection with AutoFac

clock March 13, 2016 18:27 by author ShailendraSaxena

 

1.       1. Interface

publicinterfaceIUserRepository

    {

        void Add(User user);

        void Update(User user);

        void Delete(int Id);

        User GetUser(int Id);

        IEnumerable<User> GetUserList();

        void SaveChanges();

    }

2. Repository

publicclassUserRepository : IUserRepository

    {

        readonlyDataContext _context;

        public UserRepository()

        {

            _context = newDataContext();

        }

        publicvoid Add(User user)

        {

            _context.User.Add(user);

            _context.SaveChanges();

        }

        publicvoid Delete(int Id)

        {

            User usr = _context.User.Find(Id);

            _context.User.Remove(usr);

        }

        publicUser GetUser(int Id)

        {

            User usr = _context.User.Find(Id);

            return usr;

        }

        publicIEnumerable<User> GetUserList()

        {

            return _context.User.ToList();

        }

        publicvoid SaveChanges()

        {

            _context.SaveChanges();

        }

        publicvoid Update(User user)

        {

            _context.Entry(user).State = EntityState.Modified;

        }

 



How to Show Enum Dropdown List in MVC4

clock May 14, 2014 05:03 by author ShailendraSaxena

 

 

This example shows you how to create a Enum Dropdown in ASP.Net MVC4.

We need to show Sex as Male or Female in Employee Screen

Step1:

Create a Class which contains the Enumlist under a Folder Enums.

 

namespace MVCApp.Enums

{

    public class MyEnums

    {

 

        public enum Gender

        {

            Male=0,

            Female=1

        }

    }

}

Step2.

Create a Employee Model as below.

using MVCApp.Enums;

 

 

namespace MVCApp.Models

{

    public class Employee

    {

               

        public int ID { get; set; }

        public string Name { get; set; }

 

        public MyEnums.Gender Sex { get; set; }

 

        public string Post { get; set; }

 

        public decimal Salary { get; set; }

 

        public string City { get; set; }

    }

}

 

Step 3.

In Create.CSHTML define as below

<divclass="form-group">

            @Html.LabelFor(model => model.Sex, new { @class = "control-label col-md-2" })

            <divclass="col-md-10">

               @Html.DropDownListFor(model => model.Sex,newSelectList(Enum.GetValues(typeof(MVCApp.Enums.MyEnums.Gender))))

                @Html.ValidationMessageFor(model => model.Sex)

            </div>

        </div>

 



There was an error running the selected code generator: MVC Entity Framework

clock February 6, 2014 08:16 by author ShailendraSaxena

Error : There was an error running the selected code generator

 

Above error comes in case of MVC project. when you have already created a separate DAL with Entity Framework 5.0 and you are adding this DAL in another project which is made in VS2013 with EF 6.0.

 

Resolution : To resolve this error I re-create my DAL Project with EF6.0 and add the reference in my Web Project.

 



ASP.NET Web Forms V/s ASP.net MVC

clock February 4, 2014 05:03 by author ShailendraSaxena



ASP.NEt MVC Releases

clock July 31, 2013 08:27 by author ShailendraSaxena

 

 

Date Version
10/12/2007 ASP.NET MVC CTP
13/03/2009 ASP.NET MVC 1.0
16/12/2009 ASP.NET MVC 2.0 RC
4/2/2010 ASP.NET MVC 2.0 RC 2
10/3/2010 ASP.NET MVC 2
9/11/2010 ASP.NET MVC 3 Beta
10/12/2010 ASP.NET MVC 3 RC 2
13/01/2011 ASP.NET MVC 3  
20/09/2011 ASP.NET MVC 4 Developer Preview
15/02/2012 ASP.NET MVC 4  Beta
31/05/2012 ASP.NET MVC 4 RC
15/08/2012 ASP.NET MVC 4  

 



What is ASP.NET SignalR

clock May 27, 2013 09:48 by author ShailendraSaxena

ASP.NET SignalR is a new library for ASP.NET developers that makes it incredibly simple to add real-time web functionality to your applications. What is "real-time web" functionality? It's the ability to have your server-side code push content to the connected clients as it happens, in real-time.

You may have heard of WebSockets, a new HTML5 API that enables bi-directional communication between the browser and server. SignalR will use WebSockets under the covers when it's available, and gracefully fallback to other techniques and technologies when it isn't, while your application code stays the same.

SignalR also provides a very simple, high-level API for doing server to client RPC (call JavaScript functions in your clients' browsers from server-side .NET code) in your ASP.NET application, as well as adding useful hooks for connection management, e.g. connect/disconnect events, grouping connections, authorization.



Unable to retrieve metadata error While Adding a Controller in ASP.Net MVC

clock April 10, 2013 00:19 by author ShailendraSaxena

Hi

 

I face this issue "Unable to retrieve metadata..." while I was dding a Controller.

Solution for this is as follows.

 

1. In Connection String change provider to "system.data.sqlclient"

2.or Remove the Connection String from the Web.config and Add controller and after adding controller again add the connection String.

 

It seems this is a bug in MVC.

I hope with this you can go ahead.

 

 



Add Controller

clock April 7, 2013 02:34 by author ShailendraSaxena

ASP.NET MVC4[TIP]:

You can now right click and select Add Controller from any folder in your MVC project. This gives you more flexibility to organize your controllers however you want, including keeping your MVC and Web API controllers in separate folders.



ASP.Net MVC 4 New Features

clock March 31, 2013 06:24 by author ShailendraSaxena

 

  • ASP.NET Web API
  • Refreshed and modernized default project templates
  • New mobile project template
  • Many new features to support mobile apps
  • Enhanced support for asynchronous methods


Search

Calendar

<<  July 2017  >>
SuMoTuWeThFrSa
2526272829301
2345678
9101112131415
16171819202122
23242526272829
303112345

Archive

Tags

Categories


Visitors

FlagCounter

Flag Counter

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2017

Sign In