如何使用MVC 3剃须刀填单子? [英] how to fill list using Mvc 3 razor?

查看:116
本文介绍了如何使用MVC 3剃须刀填单子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Asp.net MVC3新。我不能做到这一点:我怎样才能使用的测试方法显示列表中,但我不能写@ product.Test()。
我新,你能帮帮我吗?结果
查看:

 
@model的IList @ {
    布局=〜/查看/共享/ _Layout.cshtml
    ViewBag.Title =产品;
}

产品



    @foreach(以型号VAR产品)      
  • @ product.Test()

控制器:

 
命名空间MvcApplicationScottGu.Controllers
{
    公共类ProductController的:控制器
    {
        //
        // GET:/产品/        公众的ActionResult测试()
        {
            。VAR产品=新产品型号()GetProduct();
            返回视图(产品,产品);
        }    }
}


解决方案

如果你打MVC,MVC不会赢,但你会失去

问题是,你正试图从调用视图的操作。这意味着你真的没有得到MVC。这意味着你要使用MVC视图,如果它是一个Web窗体。

在MVC视图只是模板馅全格式良好的数据,用户可以很容易地消化。

VS MVC Web窗体 - 两个圣诞节请求的故事

让我们用一个比喻季节性。

一个MVC请求的故事:

在MVC,一个视图是喜欢你的圣诞火鸡。火鸡被塞满了数据,并提供给用户。

控制器就像厨师。家人问他的火鸡和厨师,他并提供它的圣诞大餐。煮火鸡,库克必须首先从商店得到它。那里的厨师买火鸡的存储是模式

整个过程排序是有道理的。

的ASP.NET Web窗体请求的故事:

在ASP.NET Web窗体,情况就不同了。

再次家庭希望土耳其为他们的圣诞大餐。而不是使用一个厨师,他们做了非常难以相信:

而不是问厨师,家里问火鸡。

火鸡说:OK(或者更确切地说,狼吞虎咽,护目镜,狼吞虎咽)。然后,它会杀死自己,拽着自己,自己做饭,如果它没有在微波炉爆炸,燔祭供应家庭(用户)。

而不是有很多可爱的煮熟的数据被塞满,它塞满了所谓的ViewState的倒胃口和消化的感伤,这是有点像宇航员的圣诞大餐。

晚餐是太恶心了欢快的节日聚会溶入混乱,有人放火烧圣诞老人是谁仍然停留在烟囱里(他吃了太多的肉馅饼)。

鲁道夫,与此同时,naffed关回拉普兰这样他就可以及时为交配季节,这是有点像他的圣诞恢复。

大家圣诞快乐(或者至少,那些你们谁把圣诞节)。

i am a new in Asp.net Mvc3. i can not do that: how can i show list using Test method but i can not write @product.Test(). i am new,can you help me?
VIEW:


@model IList<MvcApplicationScottGu.Models.ProductModel>

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
    ViewBag.Title = "Product";
}

<h2>Product</h2>
<ul>
@foreach (var product in Model)

      <li>@product.Test()</li>

</ul>

CONTROLLER:


namespace MvcApplicationScottGu.Controllers
{
    public class ProductController : Controller
    {
        //
        // GET: /Product/

        public ActionResult Test()
        {
            var products = new ProductModel().GetProduct();
            return View("Product",products);
        }

    }
}

解决方案

If you fight MVC, MVC won't win, but you will lose

The problem is that you are trying to call an action from a View. Which means you seriously aren't getting MVC. It means you are trying to use an MVC view as if it was a Web Form.

Views in MVC are just templates for stuffing full of nicely formatted data that the user can easily digest.

MVC vs Web Forms - Tale of Two Christmas Requests

Lets use a seasonal analogy.

Tale of an MVC Request:

In MVC, a view is like your Christmas turkey. The turkey gets stuffed with data and is served to the user.

The Controller is like the cook. The family ask him for a turkey and he cooks it and serves it for Christmas dinner. To cook the turkey, the cook must first get it from the store. The store where the cook buys the turkey is the Model.

The whole process sort of makes sense.

Tale of an ASP.NET Web Forms Request:

In ASP.NET Web Forms, the situation is different.

Again, the family wants Turkey for their Christmas dinner. Instead of using a cook, they do something highly improbably:

Instead of asking the cook, the family asks the turkey.

The turkey says "OK" (or rather, "Gobble, goggle, gobble"). It then kills itself, plucks itself, cooks itself and, if it hasn't exploded in the microwave, a burnt offering is served to the family (user).

Instead of being stuffed with lots of lovely well cooked data, it is stuffed with an unappetising and indigestible goo called ViewState, which is sort of like an astronaut's Christmas dinner.

The dinner is so disgusting that the merry festive gathering dissolves into mayhem and someone sets fire to Father Christmas who is still stuck up the chimney (he ate too many mince pies).

Rudolph, meanwhile, has naffed off back to Lapland so that he can recover in time for the rutting season, which is sort of like his Christmas.

Merry Christmas everyone (or at least, those of you who are into Christmas).

这篇关于如何使用MVC 3剃须刀填单子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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