简体   繁体   English

详细动作如何运作

[英]How Does detail action work

I have a controller in which i have some actions. 我有一个控制器,在其中有一些操作。 One of these, is the Detail action. 其中之一就是“细节”动作。 Here: 这里:

public ActionResult Details(int id) {
    Type x = ReadFromSomewhere(id);
    return View(x);
} 

I have another action , that after updating the Type x object , it will return the same object again to the Detail action. 我还有另一个动作,即在更新Type x对象之后,它将再次将同一对象返回给Detail动作。 So: 所以:

[HttpPost]
public ActionResult Update(Type y)
{
    Some works here...

    return View("Details", y);
}

As you can see i don't pass the id to the return View("Details", y); 如您所见,我没有将ID传递给return View("Details", y); but everythigs work. 但是每个人都能工作。 Why ? 为什么呢 I mean, i should return something like this: 我的意思是,我应该返回以下内容:

return View("Details", y.Id);

But the View() method accepts only object after the name of the view. 但是View()方法仅在视图名称之后接受对象。 So, What kind of magic is this? 那么,这是什么魔术呢?

Thank you 谢谢

There is a very simple reason for this. 有一个很简单的原因。 The View() method that you are calling is looking for the view name as the first parameter, and the model object as the second. 您正在调用的View()方法正在将视图名称作为第一个参数,并将模型对象作为第二个参数。

In order to not have a generic overload of that method, the type that they are looking for is object . 为了没有该方法的泛型重载,他们正在寻找的类型是object Since that is the base type of EVERY type in .NET, you are able to supply any type as the parameter. 由于这是.NET中EVERY类型的基本类型,因此您可以提供任何类型的参数。

You can set your method call up like that, however you would get a run time exception when the Razor engine tries to render your page and it has a different model type than it is expecting. 您可以像这样设置方法调用,但是当Razor引擎尝试呈现您的页面并且它的模型类型与预期不同时,您将获得运行时异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM