开发者

Fetching attribute values from FormCollection in Asp.net MVC

开发者 https://www.devze.com 2023-02-06 21:08 出处:网络
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.

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.开发者_JAVA技巧

Thanks for the help in advance..


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.


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.


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)
    {
0

精彩评论

暂无评论...
验证码 换一张
取 消