Click here to Skip to main content
15,902,635 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a table of one column of an attribute List<string> strengthC
C#
<table>
<thead>
 <tr>
  <th>
     @Html.LabelFor(model => model.strengthC, htmlAttributes: new { @class = "control-label col-md-3" })
  </th>
 </tr>
</thead>
<tbody>
 <tr>
  <td>
     @Html.EditorFor(model => model.strengthC[0], new { htmlAttributes = new { @class = "form-control", @style = "background-color: Azure; opacity:0.9" } })
@Html.ValidationMessageFor(model => model.strengthC, "", new { @class = "text-danger" })
  </td>
 </tr>
</tbody>
</table>

I want to add new rows to the table with a counter 'i' that is the index of strength[i] using Javascript.

What I have tried:

C#
<script type="text/javascript">
var i = 0;
function newRow() {
   i += 1;
$('#mainTable tr:last').after('<tr><td>@Html.EditorFor(model => model.strengthC[i], new { htmlAttributes = new { @class = "form-control", @style = "background-color: Azure; opacity:0.9" } })
@Html.ValidationMessageFor(model => model.strengthC, "", new { @class = "text-danger" }</td></tr>');
    }
</script>

The problem here is that model.strengthC[i] doesn't accept 'i' as a parameter: "name 'i' does not exist in the current context"
Posted
Updated 16-Mar-18 4:26am
Comments
F-ES Sitecore 16-Mar-18 10:08am    
You can't do that because your razor code runs on the server to generate html, which is then send to the browser to execute. Your model.strength[i] line is being executed on the server before the resulting js is sent to the browser to be executed, so "i" as a javascript concept doesn't yet exist, it is looking for a variable called "i" in your server-code.

You're going to have to create the html to add a new row normally. If you look at the source of your page you'll see how you need to construct the elements, it'll probably be an input with a name like "strength[0]", "strength[1]" etc, so you just need to create a new input with a name that is "strength[n]" where "n" is the next number in the sequence.

If you google "mvc create dynamic editor via javascript" you'll probably find sample code.
TempoClick 16-Mar-18 11:20am    
But how to pass n to the javascript function and then use it in strengthC[]?

1 solution

First, why are you worried about the index of the item you're editing? Seems weird given the nature of EditFor.

Don't do it on the client side. Do it this way:

HTML
@foreach(var item in Model)
{
    @* if you really need the index, you can do this in a foreach loop *@
    int index = Model.IndexOf(item);
    <tr>
        <td>
            @Html.EditFor(m=>m.strengthC[index],...)
            ...
        </td>
   </tr>
}
 
Share this answer
 
Comments
TempoClick 16-Mar-18 11:18am    
I am inserting a new row with a button click. then I need to pass all the items inserted (strengthC[0], strengthC[1].....) to controller.
#realJSOP 16-Mar-18 13:42pm    
You should still do that on the server side. MVC supports it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900