2

I've created a dynamic form for key-value pair input, some values will contain commas:

    using(Html.BeginForm("Index", "Home", FormMethod.Post, new { id = "parameterForm" }))
    {
    <div id="inputBoxesDIV">
         for(int i = 0; i < Model.GetParameters().Count; i++)
           { 
                Html.TextBoxFor(m => m.GetParameters().ElementAt(i).Name, new { name = "name" + i, size = 20 })
                Html.TextBoxFor(m => m.GetParameters().ElementAt(i).Value, new { name = "Value" + i, size = 60 })
        }
    </div>
    }

I've tried to use FormCollection to get my pairs like this:

    [HttpPost]
    public ActionResult Index(FormCollection formCollection)
    {
        foreach (var key in formCollection.AllKeys)
        {
            var value = formCollection[key];
        }

        foreach (var key in formCollection.Keys)
        {
            var value = formCollection[key.ToString()];
        }
    //etc...

But the FormCollection use comma separated strings so it's to no good.

Is there some way I still can use FormCollection or do you have any idea how I can solve it?

5
  • 1
    Why dont you use some model as parameter instead of FormCollection Oct 11, 2013 at 9:29
  • This Razor view won't work. You cannot use such complex Lambda expression in a strongly typed helper such as TextBoxFor: m => m.GetParameters().ElementAt(i).Name. It will throw an exception. Also you cannot override the name property as you are trying to. The helper will always use the lambda expression to calculate the view. Oct 11, 2013 at 9:29
  • How can i achieve that?
    – Mangs
    Oct 11, 2013 at 9:30
  • @DarinDimitrov, do we have any reference to know what kind of lambda expression can be supported in razor view? So we could avoid mistakes in future. You always rock :) Oct 11, 2013 at 9:53
  • @JohanMagnusson, I am not aware of a reference but you could use indexer access and property access expressions. That's all. Oct 14, 2013 at 9:07

2 Answers 2

0

Why don't you use model binding:

public class KeyValue
{
    public string Name { get; set; }
    public string Value { get; set; }
}
public class Test
{
    public IEnumerable<KeyValue> KV { get; set; }
}

And in view:

@using(Html.BeginForm())
{
    for (var kv = 0; kv < Model.KV.Count(); ++kv)
    {
        @Html.TextBox("KV[" + kv + "].Name", Model.KV.ElementAt(kv).Name);
        @:<br />
        @Html.TextBox("KV[" + kv + "].Value", Model.KV.ElementAt(kv).Value);
        @:<br />
    }

    @:<input type="submit" />
}

And in cotroller get your Model:

    [HttpPost]
    public ActionResult Index(Test model)
    {
        ...
    }
0

I think you should be able to generate a view based on dynamically generated model, For that you shouldn't give chance to make changes to name part of keyvalue pair right, so remove textbox for that like,

using(Html.BeginForm("Index", "Home", FormMethod.Post, new { id = "parameterForm" }))
    {
    <div id="inputBoxesDIV">
         for(int i = 0; i < Model.GetParameters().Count; i++)
           { 
<label>Model.GetParameters().ElementAt(i).Name</label>

                Html.TextBoxFor(m => m.GetParameters().ElementAt(i).Value, new { name =Model.GetParameters().ElementAt(i).Name , size = 60 })
        }
    </div>
    }

so user will make changes to value textboxes, so after he submits your able to read all values using key names,

[HttpPost]
    public ActionResult Index(FormCollection formCollection)
    {
        foreach (var key in formCollection.AllKeys)
        {
            var value = formCollection[key];
        }

        foreach (var key in formCollection.Keys)
        {
            var value = formCollection[key.ToString()];
        }
}

If you want to give the users the ability to modify both name and value in key value pair, Then you should try like this,

     using(Html.BeginForm("Index", "Home", FormMethod.Post, new { id = "parameterForm" }))
        {
        <div id="inputBoxesDIV">
             for(int i = 0; i < Model.GetParameters().Count; i++)
               { 

<input type="text" name="@String.Format("name{0}",i)" value="@Model.GetParameters().ElementAt(i).Name" size="20"/>

                   <input type="text" name="@String.Format("value{0}",i)" value="@Model.GetParameters().ElementAt(i).Value" size="60"/>
            }
        </div>
        }

and in your post action,

[HttpPost]
    public ActionResult Index(FormCollection formCollection)
    {


        for(int i=0;i<formCollection.AllKeys.Length;i++)
        {
            var value = formCollection["value"+i];
            var name=formCollection["name"+i];
        }
    }

Hope this helps.

3
  • The first part is not an option, the user has to type both name and value =/ And I don't see how the second would help, I still get comma separated strings, and the user should be allowed to use commas in value.
    – Mangs
    Oct 11, 2013 at 10:35
  • Actually both "var value = formCollection["value"+i];" and "var name=formCollection["name"+i];" returns null (It loops four times returning null every time)
    – Mangs
    Oct 11, 2013 at 10:39
  • Instead of Html helper for textbox try raw html syntax, like <input type="text" name="@String.Format("name{0}",i)"/> like that.. Oct 11, 2013 at 11:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.