问题描述
我写在MVC3 .NET一个简单的博客应用程序,作为一个学习的过程。
I'm writing a simple "blog" application in .NET with MVC3, as a learning exercise.
要实现 - 基本的 - 更多的功能,我添加了一个自定义的按钮tiny_mce,当pressed,插入
To implement - rudimentary - "read more" functionality, I've added a custom button to tiny_mce which, when pressed, inserts
<!--#readmore#-->
进入后的内容。
这样做是为了获得内容到这一点,显示在主页上,然后添加一个链接,如果有更多的阅读,正如每一个博客引擎做如初。
The idea is to obtain the content up to that point to show on the homepage and then add a link if there's more to read, as done by every blog engine ever.
这样做是pretty简单,我有问题的其中的应该怎么办呢?现在,我已经在Post模型的功能:
Doing this is pretty straightforward, the question I have is where should I do it? Right now, I have the functionality in the Post model:
public String content_read_more()
{
if (this.content.Contains("<!--#readmore#-->"))
{
int position = this.content.IndexOf("<!--#readmore#-->");
this.has_read_more = true;
return this.content.Substring(0, position);
}
else
{
this.has_read_more = false;
return this.content;
}
}
我避免创建链接到模型中的完整的帖子,因为它似乎没有被什么东西模型应该做的。
I avoided creating the link to the complete post within the model, since it didn't seem to be something the Model should do.
但是,这样做那样,我要检查是否有后视图中的更多的内容:
But, doing it like that, I have to check if the post has more content in the view:
<div class="content">
@Html.Raw(item.content_read_more())
@if (item.hasReadMoreLink())
{
@Html.ActionLink("Leer más", "Details", new { id = item.id })
}
</div>
它引入逻辑到视图中。
which introduces logic into the view.
我应该做的控制器?有没有更好的方式来做到这一点?我在想这样太辛苦?
Should I do it in the controller? Is there a better way to do this? Am I thinking about this way too hard?
感谢您!
推荐答案
这是完全正常的。结果
没有什么错在视图中添加简单的如果
秒。
That's perfectly normal.
There is nothing wrong with adding simple if
s in the view.
您应避免添加的业务逻辑视图的;相反,你应该把它放在控制,并将结果发送到视图(完全按照你现在做的)。
You should avoid adding business logic in the view; instead, you should put it in the control and send the results to the view (exactly as you're doing now).
这篇关于实施&QUOT;更多&QUOT;在.NET MVC3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!