gpt4 book ai didi

asp.net - 在mvc4 asp.net中的Razor View 中声明模型

转载 作者:行者123 更新时间:2023-12-04 13:51:16 25 4
gpt4 key购买 nike

我是asp.net mvc4的新手,有些东西我不太了解。

如果我已经将对象传递给 Controller ​​中的View,为什么必须在 View 顶部使用@model声明Model。

举个例子:

Controller :

public ActionResult countryDetails(int id)
{

Country country = db.Country.Find(id);
return View(country);

}

看法:
@model MvcApplication2.Models.Country
@{
ViewBag.Title = "countryDetails";
}
...

Controller 返回一个以对象作为参数的View,因此模型应该是已知的。如果很明显,我很抱歉,但是我不知道为什么这不是一个“双重”声明。

谢谢您的帮助!

最佳答案

顶部的声明将为您做两件事:

  • 当您输入@Model或使用任何Html帮助程序扩展名时,它将使才智知道您正在使用哪种类型。
  • 还将在运行时检查传入的模型是否可以转换为 View 所需的类型。

  • 它不一定是“双重声明”,因为它类似于在方法上为参数指定类型。像这样
    Person Someone = new Person();
    RenderView(Someone);
    ...
    void RenderView(Person model) { }

    关于asp.net - 在mvc4 asp.net中的Razor View 中声明模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17949351/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com