Razor Method Chaining/Fluent does not work with line breaks
Lets just say I have some fluent syntax in razor like this:
@Model.ListOfStuff.Where(x=>x.StuffProp == "Some Stuff").FirstOrDefault().SomeOtherProp
But lets say that was actually really long because it is a helper that builds a complex grid. If its all on one line it w开发者_运维百科orks fine, but when its on more than one line it blows apart. How can I fix this?
@Model.ListOfStuff.Where(x=>x.StuffProp == "Some Stuff")
.FirstOrDefault().SomeOtherProp
Having such syntax in a view is just wrong and I can't stress more on it. It's not the responsibility of the view to filter and fetch data. Its responsibility is to show data that has been provided by the controller. So I would strongly recommend you using a view model and perform all those selects and stuff in the controller so that in the view all you have to write is:
@Model.SomeStuff
As you can see you no longer should be worrying about any line breaks in a view.
But to answer your question the ugliness could be wrapped in parenthesis:
@(Model.ListOfStuff.Where(x => x.StuffProp == "Some Stuff")
.FirstOrDefault().SomeOtherProp)
精彩评论