Home All Groups Group Topic Archive Search About

Deleting table row in table grid using ASP.NET MVC

Author
2 Jul 2009 2:23 PM
wxl
Hello,

I am looking for source code example or demo on how to delete table row.   I
have a table row with checkbox that renders dynamically based on selected
item from dropdowlist.     How can i delete row for example, i have a Delete
button on top of the table header.  If user click one or more checkbox,
highlighted the row. After hitting the Delete button, the table will updated
and refresh again.
I 'm currently using ASP.NET MVC 1.0 framework.    In ASP.NET WebForm, this
can be achieved using GridView control.  I am just new started to learn
ASP.NET MVC 1.0

I appreciate for any help.

Author
4 Jul 2009 12:48 PM
jacerhea
Where exactly are you stuck?  It sounds straight forward enough.   The
delete button causes a postback, and you'll get the form values from
the table and than render a new table based on what was posted back.
Are all your drivers up to date? click for free checkup

Author
6 Jul 2009 9:50 PM
wxl
"jacerhea" wrote:

> Where exactly are you stuck?  It sounds straight forward enough.   The
> delete button causes a postback, and you'll get the form values from
> the table and than render a new table based on what was posted back.
>

Here is my  code snippet:  I'm trying to delete the row with the  Delete
method i created, and don't know to proceed. I started creating loop to start
if check box is checked..

// Home Controller:
public class HomeController : Controller
{
    ...
     DataTable dt = null;
      DataRow dr = null;


      public ActionResult Index()
      {           

            return View();
       }

public void DeleteRow(int ? id)
        {
            if (Session["Cart"] != null)
            {
                var request = ((DataTable)Session["Cart"]).AsEnumerable();

                foreach (var req in request)
                {

                }

                ViewData["reqItems"] = request;
            }


            dt = (DataTable)Session["Cart"];
            dt.Rows[0].Delete();
            Session["Cart"] = dt;

         }



        public ActionResult IDRequest()
        {
            if (Session["Cart"] == null)
            {
                makeCart();
            }

            //-- converting datatable to IEnumerable
            //IEnumerable<DataRow> data = dt.AsEnumerable();

            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult CreateRequest(string _accessLevel, string
_subjectArea)
        {
            Validate(_accessLevel, _subjectArea);

            if (!ModelState.IsValid)
                return View();

            try
            {
                dt = (DataTable)Session["Cart"];

                dr = dt.NewRow();
                dr["_accessLevel"] = _accessLevel.Trim();
                dr["_subjectArea"] = _subjectArea.Trim();
                dt.Rows.Add(dr);

                Session["Cart"] = dt;
            }
            catch
            {
                return View();
            }

            return View();

        }


private void makeCart()
        {
            dt = new DataTable("Cart");
            dt.Columns.Add(new DataColumn("ID", typeof(System.Int32)));
            dt.Columns["ID"].AutoIncrement = true;
            dt.Columns["ID"].AutoIncrementSeed = 1;

            dt.Columns.Add(new DataColumn("_accessLevel",
typeof(System.String)));
            dt.Columns.Add(new DataColumn("_subjectArea",
typeof(System.String)));

            Session["Cart"] = dt;


        }

        protected void  Validate(string accessLevel, string subjectArea)
        {
            if (String.IsNullOrEmpty(accessLevel))
            {
                ModelState.AddModelError("accessLevel", "Access level is
required.");
            }

            if (String.IsNullOrEmpty(subjectArea))
            {
                ModelState.AddModelError("subjectArea", "Subject area is
required.");
            }
        }

        public ActionResult RequestListCart()
        {
            if (Session["Cart"] != null)
            {
                var request = ((DataTable)Session["Cart"]).AsEnumerable();

                ViewData["reqItems"] = request;
            }

            return View();
        }



}

I'm using Session["cart"] variable  to store data into datatable and needs
to add or delete item row dynamically, for user display of items to be
submitted.
Also, do you know how can i get the cell value of for each row when i submit
the table list ?

Thanks
WXL

Bookmark and Share