Pro ASP.NET MVC 3 Framework (Expert's Voice in .NET)

Category: Programming
Author: Adam Freeman, Steven Sanderson
4.1
All Stack Overflow 17
This Month Stack Overflow 10

Comments

by anonymous   2019-07-21

Knowing Django will put you in a good position to use ASP.NET MVC - you'll already know about the roles of Models and Views etc.

Python is generally a higher level language than C# so you might find that it can be less powerful to use. On the whole I'd say there isn't much between them though.

The best advice I could give would be to read Pro ASP.NET MVC Framework (Steve Sanderson). This book goes beyond simply teaching the reader about the features of the framework but clearly demonstrates them in context and gives clear examples of what could be considered 'best practice' (subjective, but I largely agree with the techniques he advocates).

I'm actually going the inverse at the moment. I've been using ASP.NET MVC for a couple of years but am moving towards Django for some upcoming projects.

by anonymous   2019-07-21

Steve Sanderson describes an example of pagination support in his book Pro ASP.NET MVC 3 which I highly recommend (although it shouldn't be long until the next version is released).

He describes a product controller (listing pages of products) as:

public class ProductController : Controller {
   public int PageSize = 4; //This could be retrieved from the database

   private IProductRepository repository;
   public ProductController(IProductRepository repoParam) {
      repository = repoParam;
   }

   public ViewResult List(int page = 1) {
      ProductsListViewModel viewModel = new ProductsListViewModel {
         Products = repository.Products
            .OrderBy(p => p.ProductID)
            .Skip((page - 1) * PageSize)
            .Take(PageSize),
         PagingInfo = new PagingInfo {
            CurrentPage = page,
            ItemsPerPage = PageSize,
            TotalItems = repository.Products.Count()
         }
      };
   return View(viewModel);
}

A query to the action may then be in the form:

http://localhost:23081/Product/List?page=2

(or whatever routing you require).

This view model would then be:

public class ProductsListViewModel {
   public IEnumerable<Product> Products { get; set; }
   public PagingInfo PagingInfo { get; set; }
}

And the PagingInfo model would be:

public class PagingInfo {
   public int TotalItems { get; set; }
   public int ItemsPerPage { get; set; }
   public int CurrentPage { get; set; }
   public int TotalPages {
      get { return (int)Math.Ceiling((decimal)TotalItems / ItemsPerPage); }
   }
}

You can then use this paging information to display the information in your view as required.

by anonymous   2019-07-21

You can follow some video tutorials on pluralsight. It's not that expensive and one month is quite enough.

There are also plenty of books that may help you during your learning process. I would suggest this one Pro ASP.NET MVC 3 Framework 3rd Edition

Pro ASP.NET MVC 3 Framework 3rd Edition

The official site for asp.net mvc has also some great tutorials

by anonymous   2019-07-21

Check out a great book by Steve Sanderson - Ive used the previous two editions to get me upto speed with MVC.

Pro ASP.NET MVC 3

If your looking to use Entity Framework then I can also recommend;

Programming Entity Framework

by anonymous   2019-07-21

I would suggest you get a copy of either Pro ASP.NET MVC 3 Framework, Third Edition or Pro Asp.Net Mvc 4. Either way, i usually do a blank solution and then add 3 projects like Something.WebUI, Something.Domain, Something.UnitTests (Only if you want to write tests, highly recommended).

Another option is you can head to Pluralsight and watch a video by Scott Allen

by anonymous   2017-08-20

Ideally, in a perfectly TDD environment you would have tests for absolutely everything in your code - but some may consider that dogmatic and you don't see a need to test your code-first-generated-database, so don't do it :) . You're the developer, do what you want.

I understand your feelings on the above, but if you want to develop in a true TDD style, you need to write a test before you code anything, then code to get the 'green light' on the test.

The basic workflow would be as follows (starting on anything you wanted to add to your app past your DBContext)

  1. Realize you need a new feature for your MVC application
  2. Write a test that fails (because there is no actual code at this point)
  3. Write the new feature you were wanting to add to your application
  4. Run your test, if the test passes, move on to 5, if it fails, edit the code and repeat 4
  5. Refactor as needed and re-run the test to ensure that everything passes

You will have a lot of tests, but you will have a very sound application as long as you are diligent about maintaining those tests.

Keep a few things in mind when using TDD with MVC (or likely any development architecture):

  • Run your tests frequently
  • Make sure your tests all pass after they've passed once in the past (sounds obvious, but some people are crazy)
  • Make sure you can run your tests easily and quickly, preferably one or two clicks and the tests are run. This will allow you to more easily run them frequently - this is what the tests are here for.
  • Stick to it, it is easy to get lazy and just say, "yeah, I know this part will work, no need to test it," but when you are looking at that code weeks/months/years from now and you are not as closely connected to it, you may carelessly change something and run your project's tests (since it was developed using a TDD process) and notice everything passes. But, since there wasn't a test for the part you changed, you have no evidence that the code is all working as planned. It sounds simple, but think of it as a forgotten 'broken window' that can lead to other problems.

As far as writing tests specifically for MVC actions, I highly recommend this tutorial from MSDN and this book: Pro ASP.NET MVC 3 Framework (which is an overall great resource for MVC3, including TDD for MVC3) by Freeman and Sanderson.

by anonymous   2017-08-20

You can use the partial validation technique to modify the validation results. This example will discard any errors for the Email field.

public class DontValidateEmailAttribute : ActionFilterAttribute {

  public override void OnActionExecuting(ActionExecutingContext filterContext) {
    var modelState = filterContext.Controller.ViewData.ModelState; 
    var incomingValues = filterContext.Controller.ValueProvider;

    var key = modelState.Keys.Single(x => incomingValues.Equals("Email"));    
    modelState[key].Errors.Clear();

  }
}

and apply this attribute to your Edit Controller.

I learnt this technique from Steve Sanderson's Pro ASP NET MVC 3. He uses the technique to validate a model that has required fields but the data entry is a multistep wizard. If the value has not been returned in the form post, he removes the errors for that property.

by anonymous   2017-08-20

I think making this field nullable and also making it Required is the best shot for now. As the book states, . Otherwise, you can write your own attribute, make the validate method return false when passed in value = default(T)