0
Answer

How to get the selected row values in asp.net mvc apllicatio

Anji Chelloju

Anji Chelloju

11y
1.8k
1
Hello every one ,


I am new to mvc ......here iam using a grid view in mvc2 application ........while selecting the value in gridview the page is automatically refershing.........putted in update panel,panel it is also not working...............so plz help me

here is my code


<script runat="server">

    protected void Page_Load(object sender, EventArgs e)
    {

        //binding the roledet data values to gridview
        GridView1.DataSource = ViewData["roledet"];
        GridView1.DataBind();
    }





    protected void GridView1_SelectedIndexChanged1(object sender, EventArgs e)
    {
        ViewData["roleno"] = GridView1.SelectedRow.Cells[2].ToString();///
    }
</script>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">

</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <div> 

    <form runat="server">
<div>
    <div class="contant_right_top">
               
             <h2>Delete Role</h2>
             
             <div class="divider"></div>
              <h3>Select a row to delete…..</h3>
                    <asp:GridView ID="GridView1" CssClass ="table_font_top" runat="server" 
                            BackColor="#D8DEE5" BorderColor="#010173" 
                             EnableSortingAndPagingCallbacks="false" CellSpacing="2" 
                         HorizontalAlign="Center" PageSize="5" Height="182px" Width="756px" 
                 onselectedindexchanged="GridView1_SelectedIndexChanged1" 
                 AutoGenerateSelectButton="True">
                  
                         <AlternatingRowStyle BackColor="#F1F1F1"  BorderColor="#000099"/>
                     </asp:GridView>
                 <button class="unlk_btn">Delete</button>
                  
            </div>
    
    </div>
 
</div>
 


    



 </form>   


</asp:Content>










in model:

 public bool deleterole(int roleno)
        {

            DataClasses1DataContext dc = new DataClasses1DataContext();
            roledet rd = new roledet();
            var delrole = from role in dc.roledets where role.roleno == roleno select role;
            foreach (var v in delrole)
            {
                rd.roleno = v.roleno;
                dc.roledets.DeleteOnSubmit(rd);
                dc.SubmitChanges();
               
            }
            return true;
        }








in controller:

 [HttpPost]
        public ActionResult Delete(RoleClass r)
        {
            if (ModelState.IsValid)
            {

                int roleno = Convert.ToInt32(ViewData["roleno"]);
                bool b = r.deleterole(roleno);
                if (b == true)
                {
                    Response.Write("<script>alert('Role deleted Successfully')</script>");
                }
                else
                {
                    Response.Write("<script>alert('Role delete failed')</script>");
                }
            }
            return View(r);
        }