0

I'm new in learning asp.net MVC. I am writing because I am stubborn to a problem. Indeed, I have an application that should allow me to create an XML file that will be added to a database. At this point, I created my Model, and my view that allows me to create my XML tags.

I saw on this site that could add lines in my table via Javascript. What I have done just as you can see in the code.

I can not recover what is the value of each line that I can insert. Passing my view a list I created myself. I can recover both inputs I inserted in my controller.

My question is, there's another way to create a dynamic lines via javascript, then all the entries that the user has entered the recover and fill in my list? Then I know myself how I can play with my list. But I just want to recover all the different lines that my user has inserted. I am new in ASP.NET MVC. Any help , please

This is my code.

Model

public class XMLFile
{ 
    public string TypeDoc { get; set; }
    public string Type { get; set; }
    public string Contenu { get; set; }
    public string DocName { get; set; }
 }

This is my controller :

public class XMLFileController : Controller
{
    List<XMLFile> file = new List<XMLFile>();
    [HttpGet]
    public ActionResult Save()
    {

            file.AddRange( new XMLFile[] {
                new XMLFile (){Type = "Titre", Contenu = "Chef de Service"},
                new XMLFile (){Type = "Item", Contenu="Docteur Joel"}  

                });
        return View(file);
    }

    [HttpPost]
    public ActionResult Save(List<XMLFile> formCollection)
    {
        try
        {
            if (formCollection == null)
            {

                return Content("la liste est nulle");
            }
            else
            {
                return RedirectToAction("Create", "Layout");
            }

         }


        catch
        {
            return View();
        }

    }
}

My view with a script for adding a new Row :

@using (Html.BeginForm("Save", "XMLFile", FormMethod.Post,new { @class = "form-horizontal", @role = "form", @id = "FormCreateXML" }))
     {
        <table class="table table-bordered" id="XMLFileTable"> 

                <thead>
                    <tr>
                        <th>Type</th>
                        <th>Contenu</th>
                        <th></th>
                        <th></th>
                    </tr>
                </thead>
                <tbody>
                    @for (int i = 0; i<Model.Count; i++)
                    {
                    <tr> 
                        <td>@Html.TextBoxFor(model=>model[i].Type, new {@class="form-control help-inline", @placeholder="type" })</td>
                        <td> @Html.TextBoxFor(model=>model[i].Contenu, new {@class="form-control help-inline", @placeholder="contenu" })</td>
                        <td> <input type="button" class="BtnPlus" value="+" /> </td>  
                        <td> <input type="button" class="BtnMinus" value="-" /> </td> 

                    </tr>
                     }
                </tbody>
                <tfoot>
                    <tr>
                        <td> <button type="submit" class="btn btn-success" >Save</button> </td> 
                    </tr>
                </tfoot>

        </table>
     }
</body> 

<script type="text/javascript">
    $(document).ready(function () {
        function addRow() {
            var html = '<tr>' +
                            '<td><input type="text" class="form-control" placeholder="type"></td>' +
                            '<td> <input type="text" class="form-control" placeholder="contenu"></td>' +
                            '<td> <input type="button" class="BtnPlus" value="+" /> </td>' +
                            '<td> <input type="button" class="BtnMinus" value="-" /></td>' +
                       '</tr>'
            $(html).appendTo($("#XMLFileTable"))
        };
        function deleteRow() {
            var par = $(this).parent().parent();
            par.remove();
        };
        $("#XMLFileTable").on("click", ".BtnPlus", addRow);
        $("#XMLFileTable").on("click", ".BtnMinus", deleteRow);


    });

</script>
Steve
  • 213,761
  • 22
  • 232
  • 286
  • Your not even giving your dynamically created controls a name, and the names must be correct with indexers to post back corectly. [This answer](http://stackoverflow.com/questions/24026374/adding-another-pet-to-a-model-form/24027152#24027152) may help you understand, but you also need to use an `Index` property if you want to be able to delete items from the collection –  Dec 01 '14 at 08:49
  • Thank you for your help. I think it was me who did not quite explained my problem. By browsing the answer that you gave me. I noticed that I could actually recover the value of the first line of my table. But the other values, ie the value of dynamically created lines Javascript is not recognized. I do not know from where the problem. Apparently when my controller returns an object, it takes into account only the first entry. Would it be possible to create a loop just at the beginning of the table? – Joël Mulaj'i Dec 01 '14 at 14:35
  • Of course it does not recognize the dynamically created elements. You have not given them a `name` attribute as I explained. –  Dec 01 '14 at 20:46

0 Answers0