Deleting table row in table grid using ASP.NET MVC

Discussion in 'ASP .Net' started by wxl, Jul 2, 2009.

  1. wxl

    wxl Guest

    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.
     
    wxl, Jul 2, 2009
    #1
    1. Advertising

  2. wxl

    jacerhea Guest

    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.
     
    jacerhea, Jul 4, 2009
    #2
    1. Advertising

  3. wxl

    wxl Guest

    "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
     
    wxl, Jul 6, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lauchlan M
    Replies:
    1
    Views:
    431
    Lauchlan M
    May 21, 2004
  2. Bazza Formez
    Replies:
    4
    Views:
    421
    Vanni Tomezzoli
    Dec 9, 2004
  3. rmn190
    Replies:
    2
    Views:
    2,467
    Arne Vajhøj
    Jan 10, 2008
  4. Paulo
    Replies:
    3
    Views:
    508
    Peter Bromberg [C# MVP]
    Dec 4, 2008
  5. Crazy Cat
    Replies:
    1
    Views:
    722
    schepp
    Sep 3, 2009
Loading...

Share This Page