开发者

Razor Method Chaining/Fluent does not work with line breaks

开发者 https://www.devze.com 2023-03-06 06:48 出处:网络
Lets just say I have some fluent syntax in razor like this: @Model.ListOfStuff.Where(x=>x.StuffProp == \"Some Stuff\").FirstOrDefault().SomeOtherProp

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)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号