ASP.NET MVC3 - dynamically add an element to an array on an object

I am working on a site where the user should be able to fill out a dynamically expanding form.

I would add a line with gray fields, and when the user gives focus to one of these fields, the following javascript will add a line

<tr class="unSelected">
   <input name="id[]">
   <input name="blabla[]">
</tr>

$('.unSelected').focusin(function () {
  if ($(this).hasClass('unSelected')) {
    var row = $(this).clone(true);
    $(this).after(row);
    $(this).removeClass('unSelected');
  }
});

but how to do it with a razor and asp.net, since objects will not be automatically generated?

+5
source share
4 answers

In ASP.NET MVC, if you have a model class, for example:

public class PageModel
{
    public Collection<RowItem> RowItems { get; set; }
}

public class RowItem
{
    public int Id {get;set;}
    public string MoreFields { get; set; }
}

And your javascript adds these lines:

<script type="text/javascript">
    var currentRowIndex = 0;

    $(document).ready(function () {
        SetFocusEventForInputs('unSelected0');
    });

    function SetFocusEventForInputs(className) {
        var inputSelector = '.' + className;

        $(inputSelector).focusin(function () {
            AddNewRowTo(this);
            $(inputSelector).unbind('focusin').removeClass(className);
        });
    }

    function AddNewRowTo(sendingInputField) {
        currentRowIndex++;
        var className = 'unSelected' + currentRowIndex;
        var collectionNamePrefix = 'RowItems[' + currentRowIndex + '].';

        var idField = $('<input/>').attr('name', collectionNamePrefix + 'Id').attr('type', 'text').attr('class', className);
        var moreFields = $('<input/>').attr('name', collectionNamePrefix + 'MoreFields').attr('type', 'text').attr('class', className);

        var cell = $('<td></td>').append(idField).append(moreFields);
        var row = $('<tr></tr>').append(cell);

        $(sendingInputField).parents("tr").after(row);

        SetFocusEventForInputs(className);
    }
</script>
<table>
    <tr>
        <td>
            <input name="RowItems[0].Id" type="text" class="unSelected0" />
            <input name="RowItems[0].MoreFields" type="text" class="unSelected0" />
        </td>
    </tr>
</table>

The standard mediator in MVC should resolve it just fine when it is sent

[HttpPost]
public ActionResult YourPostActionHere(PageModel model)
{
    var count = model.RowItems.Count();
    etc...
}
+3

, jQuery, () ASP.NET MVC.

, , PHP .

0

, , script, PHP Asp.Net, , . Asp.Net mvc.

, , JavaScript , . , .

0

, script? , javascript .

<tr class="unSelected">
   <input name="id[]">
   <input name="blabla[]">
</tr>

<script src="/Scripts/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
    $().ready(function () {
        $('.unSelected').focusin(function () {
            if ($(this).hasClass('unSelected')) {
                var row = $(this).clone(true);
                $(this).after(row);
                $(this).removeClass('unSelected');
            }
        });
    });
</script>
0
source

All Articles