Tag: mvc

Using Autofac in a ASP.NET MVC 4 application

Posted by – October 18, 2012

ASP.NET MVC is a great framework, very flexible, but sometimes we need to organize a little better our business logic, there’s nothing better than have a business logic completely decoupled from controller, which can be easily reused on several controllers over the project.

To help ASP.NET MVC make use of reusable business logic and other logic components aswell, we can add a DI/IoC container to our project, after few days researching about IoC container alternatives, I choose Autofac to create my first web application that takes advantage of Dependency Injection.
More

Share

Fixing SelectedValue issue on DropbDownListFor with SelectList

Posted by – July 30, 2012

After spent several hours trying to figure out how the SelectedValue works on DropDownListFor, I finally figure out how to do it, you can also check my answer on stackoverflow here.

It’s very simple to get SelectList and SelectedValue working together, even if your property isn’t a simple object like a Int, String or a Double value.

Example:

Assuming our Region object is something like this:

1
2
3
4
public class Region {
     public Guid ID { get; set; }
     public string Name { get; set; }
}

More

Share

Data Persistence on ASP.NET MVC Applications with EntityFramework

Posted by – February 27, 2009

There’s no doubt, ASP.NET is an amazing technology, very productive and easy to use, but it isn’t perfect, his architecture doesn’t help enough when you need add unit tests in your project and you can’t see a clean separation of concerns, some of its urls used to access pages isn’t so intuitive, if you need pass some data over browser address bar it will only read by accessing the old QueryString object.

As completly opposite we have ASP.NET MVC, some of its urls are very intuitive, you don’t need use something like QueryString object to read parameters passed via GET method, you can see a clear separation between model, view and controller layers, all ASP.NET MVC applications can be easily tested using the most famous unit tests frameworks.

In this article I’ll give a small introduction to web development with ASP.NET MVC and data persistence using ADO.NET EntityFramework, a simple contacts application will show how to accomplish this.
More

Share

ASP.NET MVC, good or bad?

Posted by – March 15, 2008

I have been listening some screencasts about ASP.NET MVC, at first moment we can see few features found on the other web frameworks like Grails and RoR, the controllers are very easy to write and all controllers actions can be bind easily on your view code by using helper methods.

The default view engine of ASP.NET MVC is terrible, remembers the way that we wrote some ASP and PHP pages in the past, if you need more control over page rendering you must place some conditional statements there, take a look on the code block below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<form action="/App/InsertCustomer">
  <% if (ViewData.ErrorMessage != null) { %>
    The following error occurred while inserting the customer data:
    <br />
    <%= ViewData.ErrorMessage %>
    <br />
  <% } %>

  First name: <input type="text" name="firstName"
      value="<%= ViewData.FirstName %>" /><br />
  Last name: <input type="text" name="lastName"
      value="<%= ViewData.LastName %>"  /><br />

    <input type="submit" value="Insert" />
</form>

Note the presence of ASP tags <%= somecode %>, this kind of web page code can be very easy to understand on small pages, but if your pages start to be bigger and bigger you will have some problems with code readability, I don’t like this kind of view coding, it can turn into a nightmare.

But, in other hand, you can put all this mix of markup and C#/VB code into an ASP.NET user control, separating small pieces of reusable code into .ascx files, with this approach you can reduce the amount of non markup code into your page and all page rendering logic will be a little more easier to understand.

Share