How do I get over my fears of <% %> in my ASP.Net MVC markup?

后端 未结 12 1864
轻奢々
轻奢々 2021-02-04 07:40

So I totally buy into the basic tenents of ASP.NET, testability, SoC, HTML control...it\'s awesome. However being new to it I have a huge hang up with the markup. I know it co

相关标签:
12条回答
  • 2021-02-04 08:10

    I would look at some of the other View engines available, I personally really like Spark view engine but there are many others out there.

    Scott Hanselman did a great post looking at spark and Nhaml with some snippets of markup that you can look at to see if it fits your asthetics :)

    http://www.hanselman.com/blog/TheWeeklySourceCode30SparkAndNHamlCrazyASPNETMVCViewEngines.aspx

    0 讨论(0)
  • 2021-02-04 08:11

    If you press ctrl+z after it formats it just undoes the formatting and not the typing.

    I find this helpful to keep the first bracket on the same line as the if statement

    0 讨论(0)
  • 2021-02-04 08:14

    Move some (not all, only in cases it makes sense) display logic (like your dates) into your model. For instance, in your example you could add a Task.DisplayDate string property that handles the .ToShortDateString() call. Then if it ever changes, you change it all in one place and it shortens up the code in the view.

    HTML helpers are nice, sometimes. I don't really like having my HTML in concatenated strings or a StringBuilder in a class somewhere, but if you're doing something a lot that works with a lot of different sets of data they're not so bad.

    I would also change your C# code formatting preferences to have brackets on the same line. That cuts down the clutter. Unfortunately you can't have separate settings for this between the code editor and the HTML editor, so you'll have to get used to it in your code too. It's not the worst thing in the world.

    What others have said helps too: use partial views and strong-typing.

    0 讨论(0)
  • 2021-02-04 08:16

    Well now you could try out Razor (in ASP.NET MVC 3)

    I just hope that the "@" won't scare you too :P

    0 讨论(0)
  • 2021-02-04 08:18

    I'd suggest looking at the Velocity templating engine that MonoRails used. ASP.NET MVC models itself very closely on the MonoRails format, so using the Velocity templates is fairly easy.

    Click here to learn more.

    0 讨论(0)
  • 2021-02-04 08:19

    1) Helpers reduce HTML noise
    2) Partial Views to break up larger pages/reuse view code
    3) Try a different rendering engine, such as Sparkline
    4) REFACTOR if your view includes too many if statements.

    0 讨论(0)
提交回复
热议问题