0

I am having a value ["aaa","bbb","ccc"] in column. I have to put each value in separate textbox without double quotes.

I have tried (in view):

 <td>@Html.TextBoxFor(m => (m.editPeriodicTask.args[0]), new { @id = "args0", @class = "form-control-list" })</td>
  <td>@Html.TextBoxFor(m => (m.editPeriodicTask.args[1]), new { @id = "args1", @class = "form-control-list" })</td>
 <td>@Html.TextBoxFor(m => (m.editPeriodicTask.args[2]), new { @id = "args2", @class = "form-control-list" })</td>

and also

 <td>@Html.TextBoxFor(m => (m.editPeriodicTask.args.split(',')[0]), new { @id = "args0", @class = "form-control-list" })</td>
 <td>@Html.TextBoxFor(m => (m.editPeriodicTask.args.split(',')[1]), new { @id = "args1", @class = "form-control-list" })</td>
 <td>@Html.TextBoxFor(m => (m.editPeriodicTask.args.split(',')[2]), new { @id = "args2", @class = "form-control-list" })</td>

but it does not gives the exact solution what i need. How to do this.?

4
  • 1
    Use a view model with a property string[] and convert your value to an array using the string.Split() method and then use a for loop to generate a textbox for each value in the array (side note: you usage of @id = "args" is generating duplicate id attributes which is invalid html) Commented Feb 16, 2016 at 6:57
  • Isn't the value already a string array? Commented Feb 16, 2016 at 7:06
  • 1
    @EthanFischer, Maybe/maybe not. OP has stated the value is in a 'column' which suggests its not Commented Feb 16, 2016 at 7:12
  • Not sure why you have accepted the answer you did. It would never bind to your model when your submit the form Commented Feb 17, 2016 at 1:03

3 Answers 3

3

You also do this, like the below code

    var value = ["aaa","bbb","ccc"];
    var data1 = value.Replace("[","").Replace("]","");
    var item = data1.Split(',');
    for(var i = 0; i < item.length; i++)
    {
        var data = item[i].Replace("\"","");
        <td>@Html.TextBoxFor(m => m.editPeriodicTask.args, new { @id = "args" + i, @class = "form-control-list", @Value =  data})</td>
    }

hope this helps

Sign up to request clarification or add additional context in comments.

3 Comments

Actually the 'args' only having the ["aaa","bbb","ccc"]. By doing this too also not giving the result which i am expect
I am facing an error in replace statement. could you please check this @anand
0

try this

@{ 
     foreach (var value in Model.editPeriodicTask.args.ToList().Split(','))
     {
         <td> @Html.TextBox("name", value, new {@id = "args", @class = "form-control-list"}) </td>
     }
}

4 Comments

'@id = "args" will generate the same id for each textbox right? Should she use @class = "args"?'
Yes I know it will generate same ids but she has done the same in her question so I just used that.
@ Akshay, i have got the same output as mine. The first textbox having ["aaa" 2nd textbox having "bbb" and the third having "ccc"]. I have to remove the [ and "" in the textbox result
@sara, you can replace "[" before setting the value
0

Try this( get a counter used in differentiating the ids before you loop to create the textboxex )

 @{int count = 0;}

 @foreach (var value in Model.editPeriodicTask.args.ToList().Split(','))
 {
    <td> @Html.TextBox("name", value, new {@id = "args"+count, @class = "form-control-list"}) </td>

count++
 }

1 Comment

Okay but how to use the different id name in controller

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.