Tag: c#

  • Entity Framework’s Code-First with an ObjectContext

    If the following blog interests you, it’s probably because you’re using Entity Framework with a Code-First approach and whatever it is you are trying to do *must* use an ObjectContext instead of the standard DbContext. I’m sure, like me, you thought this shouldn’t be too hard. Then you started hitting roadblocks like, Metadata is required in your connection string. What’s metadata? This is only needed in Database-First so you can tell the framework where your edmx and other definition files are, so why do I need this with Code-First, I don’t have these files?

    This was definitely my first reaction as well. So after much trial-and-error and research, I have the solution!

    (more…)

  • Pass Model or Form Data with MVC when redirecting

    Have you ever wanted to pass form data or perhaps even a full model from one action to another through a RedirectToAction? By adding a new library package from NuGet, this can be accomplished with a few small changes to your controllers.

    The first thing that needs to be done is to install the package through the NuGet package manager. Inside of Visual Studio, with the your MVC project selected select Tools -> Library Package Manager -> Add Library Package Reference… On the left hand side, select the Online button. Then, in the search field, type MvcContrib and install the base package and you can explore how to truncate string with C#.

    (more…)

  • Compile Views with your MVC project

    Most ASP.NET developers will use Visual Studio to build their projects. The program has evolved quite a bit over the past few years. Including excellent features like Intellisense inside of ASP.NET MVC view files as well as some error detection in these, by default, not compiled elements. However, when ViewBag variables are used or other run-time specific elements, Visual Studio is unable to determine potential errors in these view files. Have no fear though; there is a nice and simple solution to help solve this problem!

    (more…)

  • MVC: Accessing the RouteData inside of your C# code

    You want to perform some dynamic processing in your code and you need to determine either the name of the current controller or the current action or both. Enter the object ControllerContext.RouteData.

    (more…)

  • C# HtmlExtension to display SVGs

    You have SVGs that you wish to display on your website but you want to be able to re-use them without copying and pasting the code or creating a shared view.

    By creating an HtmlExtension class with a function called Svg you can create a centralized place to store and re-use your SVGs.

    (more…)