MVC 4模型和模型之间的区别是什么

时间:2015-05-02 05:11:03

标签: asp.net-mvc-4 model-view-controller

我差不多两年没用C#进行编码了,所以我的知识丢失了。 我正在一个项目中工作,我发现我不能使用模型在视图中做一些数据绑定的事情。在Mvc 4+中,是否将@model [type]更改为WebPage.Model?我只熟悉Mvc3-。如果给出样本项目,我将不胜感激。

1 个答案:

答案 0 :(得分:0)

@model用于在视图页面中“导入”模型,而@Model表示导入的模型,并且是您检索其属性的位置。

请参阅以下示例:

@model MeuExemploMVC.Models.CarrinhoComprasViewModel

@{
    ViewBag.Title = "Purchase Cart";
}

<h2>@Model.Mensagem
</h2>
<fieldset>
    <legend>Purchase Cart</legend>

    <table>
        <caption>Products</caption>
        <thead>
        <tr>
            <th>Product</th>
            <th>Price</th>
        </tr>
        </thead>
        <tbody>
        @foreach (var product in Model.Products) {
            <tr>
                <td>@product.Name</td>
                <td>@product.Price</td>
            </tr>
        }
        </tbody>
        <tfoot>
            <tr>
                <td><strong>Total</strong></td>
                <td>@Model.TotalPrice</td>
            </tr>
        </tfoot>
    </table>

</fieldset>

在这里,您可以看到@model只是将Model对象导入页面,而@Model则从检索到的模型中获得实际值。

Here是CodeProject的一个教程,它说明了这种差异。请注意,那里提供了示例代码。

相关问题