MVC 3当量至< asp的:转发器GT;功能? [英] mvc 3 equivalent to <asp:repeater> function?

查看:100
本文介绍了MVC 3当量至< asp的:转发器GT;功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Asp.Net一个网站,我想移植到MVC 3,我已经只有前MVC 2的工作。我碰到下面的ASP功能跌跌撞撞

I have a website in Asp.Net that I am trying to port to MVC 3 and I have only worked with MVC 2 before. I stumbled across the following asp function

<div class="popup-holder">
<ul class="popups">
    <asp:Repeater runat="server" ID="ourTeamRepeater" OnItemDataBound="ourTeamRepeater_ItemDataBound">
        <ItemTemplate>
            <asp:Panel ID="pnlTeamMember" runat="server">
                <li id="TeamMember" runat="server" class="memberImage">
                    <asp:Image runat="server" ID="memberImg" />
                </li>
                <div class="popup">
                    <div class="img-holder">
                        <asp:Image runat="server" ID="memberImgBig" />
                    </div>
                    <div class="popup-text-t">
                        <div class="close">
                            close
                        </div>
                    </div>
                    <div class="popup-text">
                    </div>
                    <div class="popup-text-b">
                    </div>
                    <div class="holder">
                        <asp:Literal ID="memberDescription" runat="server" />
                    </div>
                </div>
            </asp:Panel>
        </ItemTemplate>
    </asp:Repeater>
</ul>

看起来也许这工作方式类似于一个for循环,但我不是很积极如何将其转化为MVC 3架构。

it looks like maybe this works similarly to a for loop, but I'm not quite positive how to convert it to MVC 3 architecture.

推荐答案

移植现有的WebForms应用ASP.NET MVC是线不仅盲目地去翻译一些在线观看的WebForms code,你有。应该考虑到目标平台的语义。例如转换这个 ASP:直放站成一个丑陋的的foreach 循环,而不是考虑到的东西像视图模型,显示模板不会是很不错的。

Porting an existing WebForms application to ASP.NET MVC is not only about blindly translating line by line some WebForms view code that you have. You should take into account the semantics of the target platform. For example converting this asp:Repeater into an ugly foreach loop instead of taking into account things like view models, display templates would not be very good.

因此​​,在ASP.NET MVC你通过设计视图模型启动:

So in ASP.NET MVC you start by designing view models:

public class MemberViewModel
{
    public int Id { get; set; }
    public string Description { get; set; }   
}

那么你设计出这种填充视图模型的控制器操作:

then you design a controller action which populates this view model:

public ActionResult Index()
{
    IEnumerable<MemberViewModel> model = ...
    return View(model);
}

那么你就写在你调用一个显示模板一个强类型的视图:

then you write a strongly typed view in which you invoke a display template:

@model IEnumerable<MemberViewModel>
@Html.DisplayForModel()

和则定义将被渲染的收集(〜/查看/共享/ DisplayTemplates / MemberViewModel.cshtml )的每个元素的显示模板:

and then you define a display template which will be rendered for each element of the collection (~/Views/Shared/DisplayTemplates/MemberViewModel.cshtml):

@model MemberViewModel

<li id="TeamMember" class="memberImage">
    <img src="Url.Action("ThumbnailImage", new { id = Model.Id })" alt=""/>
</li>

<div class="popup">
    <div class="img-holder">
        <img src="Url.Action("FullImage", new { id = Model.Id })" alt=""/>
    </div>

    <div class="popup-text-t">
        <div class="close">
            close
        </div>
    </div>

    <div class="popup-text"></div>
    <div class="popup-text-b"></div>

    <div class="holder">
        @Html.DisplayFor(x => x.Description)
    </div>
</div>

现在你会发现另外两个 ThumbnailImage FullImage 控制器操作,在使我们能够获取的图像成员给出的会员ID。例如:

Now you will notice the two additional ThumbnailImage and FullImage controller actions that will allows us to fetch the images of the members given the member id. For example:

public ActionResult ThumbnailImage(int id)
{
    byte[] thumbnail = ...
    return File(thumbnail, "image/jpeg");
}

现在这更像是ASP.NET MVC。正如你可以看到它比传统的WebForms一个完全不同的模式。

Now that's more like ASP.NET MVC. As you can see it's a totally different pattern than classic WebForms.

这篇关于MVC 3当量至&lt; asp的:转发器GT;功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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