Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding textbox dynamically and save values to the database in asp.net mvc

Tags:

asp.net

I would like to add text box dynamically by clicking add button , can delete text box and finally can save the list values from the text boxes . My model class

public class CustModel
{
    public List<Cust> CustList { get; set; }
}

public class Cust
{
    public string Name { get; set; }
}
My controller class


public class HomeController : Controller
    {
        private DB _entities;

        public HomeController()
        {
            _entities = new DbEntities();
        }

        public ActionResult Index()
        {
                return View(_customers);

        }
        [HttpPost]
        public ActionResult Index(CustModel model)
        {
                // save to the database
                return View();

        }
   } 

I want to know .cshtml code . Or any other suggestion to submit list item to the database .

like image 523
wahid_maruf Avatar asked Apr 17 '14 05:04

wahid_maruf


1 Answers

Here's how I would do it:

In the CustModel, I will change the property to IEnumerable. I will use EditorTemplate for Cust and this will save extra looping.

public class CustModel
{
    public IEnumerable<Cust> CustList { get; set; }
}

My Index.cshtml view is vary simple, I have declared strongly typed model, then in the form I have @Html.EditorFor for Custlist, a button to add new cust, a button to submit and JQuery script to add new cust. Notice that in the jquery I am creating array of controls so that model binder can pick them properly.

Index.cshtml

@model MvcApplication2.Models.CustModel

@{
    ViewBag.Title = "Home Page";
}


@using (Html.BeginForm()) {
    <fieldset>
        <legend></legend>
        <div id="divcust">
            @Html.EditorFor(m=>m.CustList)
        </div>
        <input id="btnAdd"  type="button" value="Add Cust" onclick="AddCust();" />
        <br />
        <br />
        <input type="submit" value="Submit" />
    </fieldset>
}
<script>
    function AddCust() {
        var m = $('#divcust input:last-child').attr('name');
        var index = 0;
        if (m != null && m.length > 0) {
            index = m.split('CustList[')[1].replace('].Name', '');
            index++;
        }

        var html = '<label for=\"CustList_' + index + '__Name\">Name</label>' +
            '<input id=\"CustList_' + index + '__Name\" class=\"text-box single-line\"' +
            ' type=\"text\" value=\"\" name=\"CustList[' + index + '].Name\">';
        $('#divcust').append(html);


    };
</script>

I have added a EditorTemplates folder in my view/home folder and added a view for Cust:

Cust.cshtml

@model MvcApplication2.Models.Cust

@Html.LabelFor(m=>m.Name)
@Html.EditorFor(m=>m.Name)

Everything works fine now, I can add new Custs and post them to save.

enter image description here

If I want to add delete function, I have to be careful to keep integrity of my control array.

like image 162
afzalulh Avatar answered Nov 15 '22 07:11

afzalulh