如何定义在ASP.NET MVC的表单字段preFIX [英] How to define form field prefix in ASP.NET MVC

查看:89
本文介绍了如何定义在ASP.NET MVC的表单字段preFIX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图呈现一个复合的ASP.NET MVC ProductCatalog 视图。这就要求我渲染多个每页产品的意见。每个产品的看法是一个独立的形式。我需要的表单字段具有基于ID的preFIX,所以我没有重复的ID所呈现的文档英寸有没有一种方法来定义preFIX被应用到了由HTML扩展产生的,或者我需要手动建立该方案的所有表单域?

I am attempting to render a composite ProductCatalog view in ASP.NET MVC. This requires that I render multiple Product views per page. Each product view is a separate form. I need the form fields to have a prefix based on the id, so that I don't have duplicate IDs in the rendered document. Is there a way to define a prefix to be applied to all the form fields that are generated by the Html Extensions, or do I need to build this out manually?

推荐答案

是的,你可以定义基础上执行的操作您的视图内控件的preFIX,请考虑以下code,应该是地方在你的GET操作方法:

Yes, you can define a prefix for the controls inside your view based on the executing action, consider the following code that should be place in your GET action method:

ViewData.TemplateInfo.HtmlFieldPrefix = "DESIRED_PREFIX";

这将需要preFIX添加到您的视图控件,但为了对付他们回来时,你发布你的页面,你就需要重新定义preFIX在POST操作的签名以下内容:

this will add the required prefix to your View controls, but in order to deal with them back when you post your page, you will need to redefine the prefix in the signature of your POST action as the following:

public ActionResult Create([Bind(Prefix = "DESIRED_PREFIX")] YOUR_ENTITY model)

让我知道,如果它的工作,谢谢。

Let me know if it worked, thanks.

这篇关于如何定义在ASP.NET MVC的表单字段preFIX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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