在MVC视图VS VS的HtmlHelper动作条件逻辑 [英] conditional logic in mvc view vs htmlhelper vs action

查看:117
本文介绍了在MVC视图VS VS的HtmlHelper动作条件逻辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要一些条件逻辑来决定视图的中间渲染的几大块HTML的大图。我有我的模型,它可以有它决定了HTML要输出几个不同值的属性。

I have a large view that needs some conditional logic to decide which of several html chunks to render in the middle of the view. I have a property on my model which can have several different values which determines the html to be output.

我通常把条件逻辑在一个HTML帮手,但考虑到每个输出是HTML的一个公平块,我不知道,在C#文件逃脱这些将是巨大的。我也可以把逻辑的行动,并呈现不同的意见,但鉴于多数的观点是一样的,这似乎并不算很好。所以我离开了我多,如果在我看来报表(或部分?),这似乎也难看(而且显然是无法验证的)。

I would normally put conditional logic in an html helper, but given that each output is a fair chunk of html, I am not sure that escaping these in a c# file would be great. I could also put the logic in the action and render different views but given that the majority of the view is the same, this does not seem great either. So I am left with multiple if statements in my view (or partial?) which also seems ugly (and is obviously untestable).

什么是这样做的最佳方式?

What is the best way of doing this?

(我的情况下,使用MVC3有新的东西和时髦的我可以使用!)

(I am using MVC3 in case there is something new and funky I can use!)

推荐答案

我通常把单独的视觉块在自己的谐音。然后,我的观点有条件地调用每个部分与Html.Partial。这样可以使您主视图从腹胀。

I usually put separate visual chunks in their own partials. Then my view conditionally calls each partial with Html.Partial. This keeps you main view from bloating.

在一般情况下,我尽量避免含有比单个元素更Html.Helpers。

In general, I try to avoid Html.Helpers that contain more than a single element.

是这样的:

@if(Model.HasA) 
{
    @Html.Partial("widgetdetails-hasa")
}

@if(Model.HasB)
{ 
    @Html.Partial("widgetdetails-hasb")
}
// etc

这篇关于在MVC视图VS VS的HtmlHelper动作条件逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆