MVC5 - 如何为动作参数类型定义反序列化行为?(MVC5 - How do I define deserialization behavior for action parameter type?)
public class LogsController : Controller { public async Task<ActionResult> User(string id, int? page) { return await User(id, (Page)page); } private async Task<ActionResult> User(string id, Page page) { var data = await Repo.GetData(id, page, 10); var model = new UserLogs { User = id, Events = data, Pager = new PagerInput(page, 10, data.TotalCount) }; return View("UserGrid", model); } }
我的Page类提供了有用的功能,例如确保页面永远不会小于1,等等。 但是,如果我的公共操作将Page作为参数,它始终为null,无论表单或查询字符串中的值是什么,即使我已经定义了与int之间的隐式强制转换。
无论如何,我可以告诉MVC如何反序列化页面类型? 这样提供某种方法/ ctor从字符串转换?
我想在实际的页面类型上定义它,因为我将在多个控制器/动作中使用它。
我很高兴不需要双动作定义。
public class LogsController : Controller { public async Task<ActionResult> User(string id, int? page) { return await User(id, (Page)page); } private async Task<ActionResult> User(string id, Page page) { var data = await Repo.GetData(id, page, 10); var model = new UserLogs { User = id, Events = data, Pager = new PagerInput(page, 10, data.TotalCount) }; return View("UserGrid", model); } }
My Page class provides useful functionality such as making sure the page is never less than 1, ect. However, if my public action takes Page as a parameter, it is always null, no matter the value in the form or querystring, even though I have defined implicit casts to and from int.
Is there anyway I can tell the MVC how to deserialize a Page type? Such a providing some sort of method/ctor to convert from string?
I would like to define it on the actual Page type since I will be using it in multiple controllers/actions.
I would be nice to not need the double action definition.
原文:https://stackoverflow.com/questions/44338006
最满意答案
您的PhotoBundle \ Entity \ Photo实体应如下所示。 注意它没有完全充实,只是相关的方法。
class Photo{ protected $id; protected $file_managed; public function setFileManaged($file) { $this->file_managed = $file; return $this; } public function getFileManaged() { return $this->file_managed; } }
有了这个,您应该能够访问entity.fileManaged.url或您在twig模板中的实体中定义的任何内容。
Your PhotoBundle\Entity\Photo entity should look like this. Note it is not fully fleshed out, just the relevant methods.
class Photo{ protected $id; protected $file_managed; public function setFileManaged($file) { $this->file_managed = $file; return $this; } public function getFileManaged() { return $this->file_managed; } }
With this you should be able to access entity.fileManaged.url or whatever you defined in your entity inside your twig templates.
相关问答
更多-
在symfony中打印语句(print statement in symfony)[2023-08-14]
是的你可以: public function fAction(Request $request){ // ... $user = $this->getDoctrine() ->getRepository('AppBundle:User') ->findOneById($id); var_dump($user); exit; // or ... exit(\Doctrine\Common\Util\Debug::d ... -
从渲染中删除“控制器”,如下所示: {{ render(controller('FOSUserBundle:Security:login',{})) }} Remove 'controller' from the render, like this : {{ render(controller('FOSUserBundle:Security:login',{})) }}
-
不确定的完整答案,但symfony和树枝必须使用单独的缓存机制,因为你不必在symfony中使用树枝,但缓存仍然有效。 有关更多信息,请查阅http://symfony.com/doc/current/并深入研究代码。 编辑:扩大我的上述非答案。 Twig缓存实际上是一个编译缓存。 您的模板保存为php文件。 引用F. Potencier“[...] Twig缓存编译后的模板以避免后续请求的解析阶段[...]”( 源代码 ) 虽然Symfony缓存(如前所述)是一个HTTP缓存,并在Symfony Docs ...
-
您可以使用attribute内置函数(从1.2开始)。 http://twig.sensiolabs.org/doc/functions/attribute.html说 attribute可用于访问变量的“动态”属性 在这种情况下,您可以写如下: {{ form_row(attribute(form, girl.id)) }} You can use attribute built-in function (as of 1.2). http://twig.sensiolabs.org/doc/functi ...
-
您的PhotoBundle \ Entity \ Photo实体应如下所示。 注意它没有完全充实,只是相关的方法。 class Photo{ protected $id; protected $file_managed; public function setFileManaged($file) { $this->file_managed = $file; return $this; } public function getFi ...
-
Twig中的数组是一个关联数组。 为了获得相对值,您必须在方形篮子中指定一个键,如下所示: {{ arrayName['key'] }} 在您的情况下,您只需将restaurantname作为数组的键: {{ arrayName['restaurantname'] }} The array in Twig is an associative array. In order to get a relative value, you must specify a key inside square basket ...
-
如果您想要此变体中的渲染内容:
您必须在控制器中生成此数据并返回响应: $em = $this->getDoctrine()->getManager(); $categoryRepository = $em->getRepository('YouBundle:Category'); $categ ...{% for category in categories %} {{ category.name }} {% endfor %}
-
解决方案:事实上,该方法是正确的,我必须使用raw过滤器来获取我的变量未被转义。 我有一个autoescape块集,它可以使这个特定的输出成为可能,这就是为什么它必须是“未转义”的原因。 Symfony 2的Twig包提供了几个块来呈现表单数据,它们使用一个特定的块来进行属性呈现,称为{% block widget_attributes %} 。 我做的是编辑这个块(我有一个单独的模板文件,包含我所有的自定义块),所以我可以添加一层“如果这个值被转义”: {% block widget_attributes ...
-
以下是我创建自定义树枝功能的方法 namespace Acme\DemoBundle\Twig; class AcmeExtension extends \Twig_Extension { public function getFunctions() { return array( 'currencySymbol' => new \Twig_Function_Method($this, 'currencySymbolFunction'), ) ...
-
您可以将for标签与Iterate over Keys和Values一起使用 。 你的功能翻译是: {% for region, list in timezones %}