• 0

ASP.NET MVC5 CSHTML (Razor) Issues


Question

I really like the @Html.EditorFor(m => m.Foo) expressions and such.

There are a few issues though,

1. Refactoring the Model does not refactor the CSHTML (I am actively working on the model so its a big issue)

2. Rebuilding does not build the CSHTML files to tell me that it no longer compiles. This becomes a runtime error.

3. IdFor gets the Id of an EditorFor form field. I used that in a "var foo = $('#@Html.IdFor(m => m.Foo)');" but at that point it becomes nasty. Ideas?

Link to comment
https://www.neowin.net/forum/topic/1248318-aspnet-mvc5-cshtml-razor-issues/
Share on other sites

5 answers to this question

Recommended Posts

  • 0

If you're still actively working on the model then I would use Html.EditorForModel(m => m) and let it scaffold on-the-fly until your model has stabilized. Otherwise you have not many other options other than updating your views as your model matures. When done you can write a custom editor for your model (in EditorTemplates) and you won't have to change your views that use EditorForModel()

  • 0

If you want to refactor a field (assuming you're using Visual Studio), you can press CTRL+R, R or F2 and it'll open a window to rename a model, method or property, which will propagate the change to everything that references it.

 

I also recommend you look into ReSharper, which will quickly highlight errors like referencing broken objects when a change occurs. It's a superbly nice addition to VS, even if it can bloat VS at times.

  • 0

1. You need something like ReSharper to do this. Microsoft doesn't think this is a big enough problem.

 

2. Have you enabled the MvcBuildviews? This might increase compile times up to tenfold. http://blogs.msdn.com/b/jimlamb/archive/2010/04/20/turn-on-compile-time-view-checking-for-asp-net-mvc-projects-in-tfs-build-2010.aspx

 

3. What are you trying to do with the field? If you want to just use the value you can do

var foo = @Model.Foo;
  • 0
  On 27/02/2015 at 21:13, night_stalker_z said:

1. You need something like ReSharper to do this. Microsoft doesn't think this is a big enough problem.

 

2. Have you enabled the MvcBuildviews? This might increase compile times up to tenfold. http://blogs.msdn.com/b/jimlamb/archive/2010/04/20/turn-on-compile-time-view-checking-for-asp-net-mvc-projects-in-tfs-build-2010.aspx

 

3. What are you trying to do with the field? If you want to just use the value you can do


var foo = @Model.Foo;
That is a bloody big problem to me! Bad Microsoft, Bad!

MvcBuildviews sounds useful, thanks.

Client Side JavaScript Foo binded to the model - getting EditorFor generated fields (via jQuery $("#ObjectIdHere")) via JS to do some logic.

This topic is now closed to further replies.
  • Posts

    • Sorry, I found your question odd, that's all 😅.
    • "LOL The IoT version is for embedded systems" LOL. The IoT version is for whatever you want, why are you so stuck up? It's just A NAME. Open your mind, buddy 😅. It's not a different version of Windows, only the licensing model and the support dates change, nothing else. "there no real reason to do that" Everyone has their reasons. You love to use Linux. Why? You have YOUR reasons. Other people will have theirs. I use Windows 11 and Linux on the side. Why? I have my reasons. "No security updates? Who cares!" Many people do, just not you. Why not use a supported OS instead of an unsupported one if you can? LOL. I find it odd that a person that loves Linux and choice/freedom so much has such a hard time understanding why people do things different than him.
    • Most people don't know what a Hz is; They buy the iPhone they can afford. This is a non-story, and it's here to get the Apple haters engaging.
    • Ummmm I thought we talking about the desktop for home users? LOL The IoT version is for embedded systems. No regular user is using that version and there no real reason to do that. Why does it seem like half the people here hate using an appropriate OS for the hardware they have? The kind of comments I see here all the time... No security updates? Who cares! I really like Windows 8.1 which was very underrated. Did I run it when it was supported? Well no I used Windows 7 at the time. My hardware doesn't support Windows 11. Windows 10 or Linux than? Nope! They are of course running Windows 11. LOL My hardware does support Windows 11. Never! I am running Windows 10 forever or at least until Windows 12 comes out and then you will have to rip my copy of Windows 11 from my dead, cold hands! I have a 12 year old PC and it's machine #4. Well that would be perfect to run Linux sir! Nah! I will run Windows on that too. Windows 7 of course and yes I absolutely sometimes must browse the Internet with it. Freedom! Did I cover all the scenarios we see here? LOL
  • Recent Achievements

    • Conversation Starter
      DarkShrunken earned a badge
      Conversation Starter
    • One Month Later
      jrromero17 earned a badge
      One Month Later
    • Week One Done
      jrromero17 earned a badge
      Week One Done
    • Conversation Starter
      johnwin1 earned a badge
      Conversation Starter
    • One Month Later
      Marwin earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      250
    2. 2
      snowy owl
      156
    3. 3
      +FloatingFatMan
      140
    4. 4
      ATLien_0
      140
    5. 5
      Xenon
      128
  • Tell a friend

    Love Neowin? Tell a friend!