从 Asp.net MVC 中的 FormCollection 获取属性值

发布于 2024-10-14 17:33:50 字数 312 浏览 2 评论 0原文

我的应用程序使用 Helper 类将自定义属性写入输入控件。而且我们动态加载 UserControl,因此我们需要使用 FormCollection 来获取发布的值。 有没有一种方法可以从 FormCollection 对象访问属性值。

示例:

<input type="text" name="textBox1" value="harsha" customAttr1 = "MyValue" />

我的问题是如何从上面(例如从控制器内部)访问 customAttr1 的值。

感谢您提前的帮助..

My application writes custom attributes to input controls using Helper classes. And also we are loading the UserControl dynamically so we need to use FormCollection to get the posted values.
Is there a way that we can access attribute values from FormCollection object.

Example:

<input type="text" name="textBox1" value="harsha" customAttr1 = "MyValue" />

My question is how can i access the value of customAttr1 from the above eg from inside the controller.

Thanks for the help in advance..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

终弃我 2024-10-21 17:33:50

你的助手是如何构建的?如果它扩展了HtmlHelper,则可以访问ViewContext.HttpContext.Request.Form,它是一个NameValueCollection;模型绑定器使用 FormCollection 将值发送回操作方法。它没有在其他地方公开曝光。

HTH。

How is your helper structured? If it's extending HtmlHelper, you can access ViewContext.HttpContext.Request.Form, which is a NameValueCollection; FormCollection is used by the model binder to post values back to an action method. Its not publicly exposed anywhere else.

HTH.

戈亓 2024-10-21 17:33:50

简单的答案是不用担心,formCollection只包含基本的Key和Value信息。

一旦你进入控制器,重新水化这些信息可能会更容易吗?使用某种机制来识别您传入的内容。

另一种方法是,如果您有一个映射到基本类型的控件列表,那么您可以循环遍历每个控件。

MVC 有点神奇,它可以将属性映射回模型,甚至映射到列表。

如果您有一个包含控件列表的模型:

public class Control
{
    String Value {get; set;}
    String Attribute1 {get; set;}
}

public class ControlViewModel
{
    IList<Control> Controls {get; set;}
}

那么在您的视图中:

for(var i = 0; i<controls.Count;i++)
{
   // Obviously this isnt complete right i needs to increment from 0; would be build using your htmlhelpers.
    <input id="Controls[i]_Value" name="Controls[i].Value" type="text" value="hello" />
    <input id="Controls[i]_Attribute1" name="Controls[i].Attribute1" type="hidden" value="Attribute" />
}

在您的 httppost 操作中,您可以收集 ControlViewModel 并且应该填充 Controls 列表。

我还没有测试过这个,可能有很多错误,但这应该足以开始;那里有讨论这个问题的帖子,如果我在发布后发现任何帖子,我会添加它们。

Simple answer is no am afraid, the formCollection only contains the basic Key and Value information.

It might be easier to rehydrate that information once you are in the controller? using some sort of mechanic to identify what you passed in.

An alternative is if you have a list of controls that map to a base type then you could loop through each control.

MVC is a bit magic and can map properties back to a model, even to a list.

If you have a model which has a list of Controls:

public class Control
{
    String Value {get; set;}
    String Attribute1 {get; set;}
}

public class ControlViewModel
{
    IList<Control> Controls {get; set;}
}

then in your view:

for(var i = 0; i<controls.Count;i++)
{
   // Obviously this isnt complete right i needs to increment from 0; would be build using your htmlhelpers.
    <input id="Controls[i]_Value" name="Controls[i].Value" type="text" value="hello" />
    <input id="Controls[i]_Attribute1" name="Controls[i].Attribute1" type="hidden" value="Attribute" />
}

in your httppost action you can then collect the ControlViewModel and the Controls list should populate.

I havent tested this, there are probably plenty of errors, but this should be enough to get started; posts out there that discuss this, if I find any after posting I'll add them.

夏日浅笑〃 2024-10-21 17:33:50

正如 Luke 已经告诉过的.. Form Collection 是字典对象,只保存名称、值对.. 为了将该东西放入控制器中,您需要通过 ajax 传递该自定义属性。

var form = $("#formid").serialize(),
    custom = $("input:text").attr("customAttr1").val();
$.ajax({ 
    type: "POST", 
    url: "/controller/ProcessData", 
    data:{collection :form,customAttr: custom },
    dataType: "html", 
    traditional: true
});

在控制器中,您需要具有以下语法:

public ActionResult ProcessData(FormCollection collection ,string customAttr)
{

如果您需要传递多个自定义值,您需要从 ajax 请求发布字符串数组并制作控制器签名,如下所示:

public ActionResult ProcessData(FormCollection collection ,string[] customArray)
    {

As Luke already told.. Form Collection is dictionary object and only holds name,value pair.. in order to get that thing into controller you need to pass that custom attribute through ajax.

var form = $("#formid").serialize(),
    custom = $("input:text").attr("customAttr1").val();
$.ajax({ 
    type: "POST", 
    url: "/controller/ProcessData", 
    data:{collection :form,customAttr: custom },
    dataType: "html", 
    traditional: true
});

in the controller you need to have following syntax:

public ActionResult ProcessData(FormCollection collection ,string customAttr)
{

in case you need to pass multiple custom values you need to post string array from ajax request and make controller signature like:

public ActionResult ProcessData(FormCollection collection ,string[] customArray)
    {
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文