DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone >

Enhancing Your Model In MVC 5

Tim Murphy user avatar by
Tim Murphy
·
May. 11, 14 · · Interview
Like (0)
Save
Tweet
3.46K Views

Join the DZone community and get the full member experience.

Join For Free

Originally posted on: http://geekswithblogs.net/tmurphy/archive/2014/05/08/enhancing-your-model-in-mvc-5.aspx

logo-asp_net-mvc-285

Simple views in ASP.NET MVC 5 are simple, but once you start getting a lot of conditional content formatting in the view things can get very messy.  Once you get to the point where your Razor code has way to many if/else blocks and they start repeating you have to come up with a way to abstract and simplify your code.  I looked at functions and partial views as possible solutions.  Both of these have their place but an alternative that you may want to entertain is implementing your model as more than just a DTO (data transfer object).

The advantage that that this approach gives you is that you can more directly take advantage of the data in the model.  If you create a function in Razor you need to either have page variables or pass in a number of parameters to accomplish the same ends.  You can use the return value of your methods to set attributes in your HTML or simply to compress your code.

This does break the MVC pattern strictly speaking, but as I always suggest you should use standards as guidelines and not absolutes.  Follow the pattern as long as it makes sense and when it gets in the way do what needs to be done to achieve your goals.  This is one alternative for a specific problem type.  Use it for what it is.

ASP.NET MVC Advantage (cryptography) Transfer (computing) Data (computing) Pass (software) Blocks Data transfer object ASP.NET guidelines

Published at DZone with permission of Tim Murphy, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How to Submit a Post to DZone
  • Ultra-Fast Microservices: When Microstream Meets Wildfly
  • How to Generate Fake Test Data
  • Composable Architecture

Comments

Partner Resources

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo