mercredi 25 octobre 2017

How to insert and update checkbox values in ASP.NET MVC?

I am performing CRUD operation in MVC
I am stuck with checkbox When I update my checkbox checked v
alue then only get last checked item does not update all checked box item

View

@foreach (var item in ViewBag.chkCheckBox)
{
 <br>var checkBoxId = item.Id;<br>
 var checkedItem = item.Status; <br>
<input type="checkbox" name="block[]" value="@checkBoxId" checked="@checkedItem ">
   <br />
}
<br>

Controller

public ActionResult PageUpdate(Page page,string[] block)
    {            
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbConnection"].ConnectionString);
        con.Open();
        SqlCommand cmd = new SqlCommand("sp_updatePage", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@Id", page.Id);
        cmd.Parameters.AddWithValue("@PageTitle", page.PageTitle);
        cmd.Parameters.AddWithValue("@MenuTitle", page.MenuTitle);
        cmd.Parameters.AddWithValue("@Url", page.Url);
        cmd.Parameters.AddWithValue("@Description", page.Description);            
        int ans = cmd.ExecuteNonQuery();
        con.Close();
        int pId = page.Id;
        int param = updateMaster(pId, block);
        if(param>0)
        {

        }
        return RedirectToAction("Page", "Admin");
    }


public int updateMaster(int Id,string[] block)
    {
        SqlConnection con1 = new SqlConnection(ConfigurationManager.ConnectionStrings["dbConnection"].ConnectionString);
        con1.Open();

        int ansa = 0;

        for (int i = 0; i < block.Count(); i++)
        {
            SqlCommand cmd1 = new SqlCommand("sp_updateTableAll", con1);
            string blockId = block[i];
            cmd1.CommandType = CommandType.StoredProcedure;
            cmd1.Parameters.AddWithValue("@blockId", blockId);
            cmd1.Parameters.AddWithValue("@pageId", Id);
            ansa = cmd1.ExecuteNonQuery();
        }

        return ansa;
    }




Aucun commentaire:

Enregistrer un commentaire