强类型ASP.NET Controller.View()方法的模型类型 [英] Strongly typing ASP.NET Controller.View() method for model type

查看:89
本文介绍了强类型ASP.NET Controller.View()方法的模型类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有强类型的视图()方法返回一个ActionResult。因此,假设我有



 类编辑:&的ViewPage LT; FROB> 

在我FrobController,我会做这样的事回归视图(编辑,someFrob); 。有没有检查怎么回事,所以我总是手动同步视图和控制器的使用它。这是最理想的,我不知道有任何内置的修复



我加入这个方法我的控制器基类:

 公众的ActionResult的ViewPage< V,M>(v查看,M型)
其中V:的ViewPage< M>
,其中M:{类
返回查看(typeof运算(V).name和模型);
}




请注意:我拿一个视图$原因这是从来没有使用b $对象b是因为,
AFAIK,有没有办法让C#的类型
推理,否则工作。如果我
删除视图参数,我需要
明确指定V,这也
指指定中号明确太...
叹了口气。




所以,现在,我可以做到这一点:

 回归的ViewPage(新Views.Frob.Edit(),myFrob); 



我指定确切的视图(没有问题,如果它被重命名),以及myFrob被typechecked到是合适的型号。丑陋的一面是,我新了一个编辑。或者,我可以写:

 返回的ViewPage((Views.Frob.Edit)空,myFrob); 



一个缺点是,该模型必须完全匹配。所以用的ViewPage>,我不能在列表通过。我想这可能工作:

 公众的ActionResult的ViewPage< V,M,T>(v查看,T型)
,其中五:&的ViewPage LT; M>
,其中M:类
,其中T:M {
返回查看(typeof运算(V).name和模型);
}



不过,C#的类型推理无法弄明白。另一个潜在的问题是,视图类型的名称可能不正确的名称,因为我认为它可以通过属性来覆盖。 。但是,这是一个简单的办法,如果我碰上它



问题:




  1. 我怎样才能让这个语法清洁?

  2. 缺点我在这里失踪?



编辑什么:对于控制器寂寂View,它只是稍微做。它从视图中获得的唯一的事情就是类型,它抓住了名。所以,这相当于传入字符串名称。而强类型的模型,它必须匹配,或者它会失败。因此它并不真正知道太多的观点 - 它只是一招,让编译器发现错误


解决方案

我看到的第一个问题是,你现在已经使你的控制器意识到视图。这是你永远不应该跨过行。


There's no strongly typed View() method to return an ActionResult. So, suppose I have

class Edit : ViewPage<Frob>

In my FrobController, I will do something like "return View("Edit", someFrob);". There's no checking going on here, so I have to always manually synchronize the view and controller's use of it. This is suboptimal, and I'm not aware of any built-in fixes.

I added this method to my controller base class:

public ActionResult ViewPage<V, M>(V view, M model)
    where V : ViewPage<M>
    where M : class {
    return View(typeof(V).Name, model);
}

Note: The reason I take a view object that's never used is because, AFAIK, there's no way to get C#'s type inference to work otherwise. If I removed the view parameter, I'd need to specify V explicitly, which also means specifying M explicitly too... sigh.

So now, I can do this:

  return ViewPage(new Views.Frob.Edit(), myFrob);

I'm specifying the exact view (no problem if it gets renamed), and myFrob is typechecked to be the right model type. The ugly side is that I new up a Edit. Alternatively, I could write:

  return ViewPage((Views.Frob.Edit)null, myFrob);

One downside is that the the model must be an exact match. So with a ViewPage>, I cannot pass in a List. I thought this might work:

    public ActionResult ViewPage<V, M, T>(V view, T model)
        where V : ViewPage<M>
        where M : class 
        where T : M {
        return View(typeof(V).Name, model);
    }

But C#'s type inference can't figure it out. The other potential problem is that the name of the view type might not be the right name, as I think it can be overridden by attributes. But that's an easy fix if I run into it.

Questions:

  1. How can I make this syntax cleaner?
  2. What downsides am I missing here?

Edit: With respect to the Controller knowing about the View, it only slightly does. The only thing it gets from the view is the Type, for which it grabs the name. So that's equivalent to passing in the string name. And the strongly typed model, which must match or it'll fail. So it doesn't really know too much about the View -- its just a trick to get the compiler to catch errors.

解决方案

The first problem I see is that you've now made your Controller aware of the View. That's a line you should never cross.

这篇关于强类型ASP.NET Controller.View()方法的模型类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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