Sunday, 15 February 2015

Delete multiple selected checkbox from webgrid using mvc 4


Delete  multiple selected checkbox using  mvc 4:-
Model:-
public class delcheck
    {
        [Required(ErrorMessage="Enter a valid Id")]
        public int StuId { get; set; }
        [Required(ErrorMessage="Enter a valid name")]
        public string StuName { get; set; }
        [Required(ErrorMessage=" Enter a course name")]
        public string StuCourse { get; set; }
        [Required(ErrorMessage="Enter a valid cityname")]
        public string StuCity { get; set; }
    } public class delcheck
    {
        [Required(ErrorMessage="Enter a valid Id")]
        public int StuId { get; set; }
        [Required(ErrorMessage="Enter a valid name")]
        public string StuName { get; set; }
        [Required(ErrorMessage=" Enter a course name")]
        public string StuCourse { get; set; }
        [Required(ErrorMessage="Enter a valid cityname")]
        public string StuCity { get; set; }
    }
Controller:-
public class mycheckdelController : Controller
    {
        //
        // GET: /mycheckdel/
        deldatamvc2015Entities dt = new deldatamvc2015Entities();
        public ActionResult Index()
        {
            List<Student> mydata = new List<Student>();
            mydata = dt.Students.ToList();
            return View(mydata);
        }
        public ActionResult Delete(string[] ids)
        {
            int[] id = null;
            if (ids != null)
            {
                id = new int[ids.Length];
                int j = 0;
                foreach (string i in ids)
                {
                    int.TryParse(i, out id[j++]);
                }

            }
            if (id != null && id.Length > 0)
            {
                List<Student> allselect = new List<Student>();
                using (deldatamvc2015Entities dc = new deldatamvc2015Entities())
                {
                    allselect = dc.Students.Where(a => id.Contains(a.StuId)).ToList();
                    foreach (var i in allselect)
                    {
                        dc.Students.Remove(i);
                    }
                    dc.SaveChanges();
                }
            }
            return RedirectToAction("Index");
        }
    }
View page:-
@model IEnumerable<mvcmvc2015.Student>
@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>
<html>
    <head>
        <title>WELCOME</title>
    </head>
    <body>
        @using (Html.BeginForm("Delete", "mycheckdel", FormMethod.Post))
        {
            var grid = new WebGrid(Model, canSort: true, canPage: true);
            grid.Pager(WebGridPagerModes.NextPrevious);
            @grid.GetHtml(columns:grid.Columns(
            grid.Column("StuId","StuId", canSort:true),
            grid.Column("StuName","StuName", canSort:true),
            grid.Column("StuCourse","StuCourse" ,canSort:true),
            grid.Column("StuCity","StuCity", canSort:true),
            grid.Column(header:"Select",format:@<text><input type="checkbox" name="ids" value="@item.StuId" /></text>)
                     ));
            <input type="submit" name="Delete It" />
        }
    </body>
</html>
View For delete:-
@model mvcmvc2015.Models.delcheck

@{
    ViewBag.Title = "Delete";
}

<h2>Delete</h2>

<h3>Are you sure you want to delete this?</h3>
<fieldset>
    <legend>delcheck</legend>

    <div class="display-label">
         @Html.DisplayNameFor(model => model.StuId)
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.StuId)
    </div>

    <div class="display-label">
         @Html.DisplayNameFor(model => model.StuName)
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.StuName)
    </div>

    <div class="display-label">
         @Html.DisplayNameFor(model => model.StuCourse)
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.StuCourse)
    </div>

    <div class="display-label">
         @Html.DisplayNameFor(model => model.StuCity)
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.StuCity)
    </div>
</fieldset>
@using (Html.BeginForm()) {
    <p>
        <input type="submit" value="Delete" /> |
        @Html.ActionLink("Back to List", "Index")
    </p>
}

Tuesday, 19 August 2014

login page code in mvc3



Login page code using mvc3:-
(1)First create a model like mylogin.cs
public class mylogin
    {
        [Required]
        [StringLength(10)]
        public string UserName { get; set; }
        [Required]
        public string PassWord { get; set; }
    }

(2)Create a  folder like DataAccess and with in it create a cs file like DAL.cs
public class DAL
    {
        static SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["111"].ConnectionString);
        public static bool UserIsValid(string username, string password)
        {
            bool authenticated = false;
            string query = string.Format("select * from[fetchlogin] where UserName ='{0}' and Password ='{1}'", username, password);
            SqlCommand cmd = new SqlCommand(query,con);
            con.Open();
            SqlDataReader sr = cmd.ExecuteReader();
            authenticated = sr.HasRows;
            con.Close();
            return (authenticated);
        }
(3) Create a  controller   with in it write below code:-
public class myloginController : Controller
    {
        //
        // GET: /mylogin/

        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Index(mylogin logit)
        {
            if (ModelState.IsValid)
            {
                if (DataAccess.DAL.UserIsValid(logit.UserName, logit.PassWord))
                {
                    FormsAuthentication.SetAuthCookie(logit.UserName, true);
                    return RedirectToAction("Index", "sanshow");
                }
               
                {
                    ModelState.AddModelError("", "Invalid username or password");
                    // ModelState.AddModelError("", "Invalid username or password");
                }
            }
            return View();

        }
    }
(4)Eventually create a view for this:-
@model sanjibmvcmvc.Models.mylogin

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>mylogin</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.UserName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.UserName)
            @Html.ValidationMessageFor(model => model.UserName)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.PassWord)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.PassWord)
            @Html.ValidationMessageFor(model => model.PassWord)
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>