Sunday, 3 April 2016

Show data in webgrid:

Hi dear ,
 This blog I  am writing code  how show data using webgrid and edit.
Controller  page:-
public class WebGridEditController : Controller
    {
        //
        // GET: /WebGridEdit/
        BookRepoNewMvcEntities dt = new BookRepoNewMvcEntities();
        public ActionResult Display()
        {
            var dis = dt.MyBooks.ToList();
            return View(dis);
        }
        public ActionResult Edit(int id = 0)
        {
            MyBook book = dt.MyBooks.Find(id);
            if (book == null)
            {
                return HttpNotFound();
            }
            return View(id);
        }
        [HttpPost]
        public ActionResult Edit(MyBook bookit)
        {
            if (ModelState.IsValid)
            {
                dt.Entry(bookit).State = System.Data.EntityState.Modified;
                dt.SaveChanges();
                return RedirectToAction("Show");
            }
            return View(bookit);
        }

    }
}
View page code:-
@model IEnumerable<TESTMVC.Models.MyBook>
@{
    ViewBag.Title = "Display";
}

<h2>Display</h2>
<html>
    <head>
        <title></title>
        <style type="text/css">
        body
        {
            background-color: #e295fc;
            color: #1A1A1A;
            font-size: .85em;
            font-family: "Segoe UI", Verdana, Helvetica, Sans-Serif;
            margin: 1.5em;
            padding: 0;
        }

        .PGrid
        {
            margin: 2%;
            border-collapse: collapse;
            width: 95%;
        }

        .Header
        {
            font-weight: bold;
            background-color: #60acf6;
            color: #ff6a00;
            text-decoration: none;
        }

        .PGrid th, .PGrid td
        {
            border: 1px solid #0094ff;
            padding: 5px;
        }

        .altRow
        {
            color:aqua;
            background-color:silver;
            /*color: brown;*/
            /*background-color: #808080;*/
        }
    </style>
    </head>
    <body>
        @{
            var grid = new WebGrid(Model, canPage: true, canSort: true, rowsPerPage: 3);
            grid.Pager(WebGridPagerModes.NextPrevious);
            @grid.GetHtml(tableStyle:"PGrid" ,headerStyle:"Header",alternatingRowStyle:"altRow",
                columns: grid.Columns(
                grid.Column("Id","Id", canSort:true),
                grid.Column("Name","Name",canSort:true),
                grid.Column("Author","Author" ,canSort:true),
                grid.Column(header:"Edit", format: @<text><a href="@Url.Action("Edit","WebGridEdit", new{id =item.id})"><img src="~/Images/edit_it.jpg" width="20" height="20" alt="" /></a></text>)
                ));
              
           
        }

    </body>
</html>
Edit page code:-
@model TESTMVC.Models.MyBook

@{
    ViewBag.Title = "Edit";
}

<h2>Edit</h2>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>MyBook</legend>

        @Html.HiddenFor(model => model.Id)

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

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

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

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

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

Insert data in mvc using Jquery

Hi dear
This blog I will writing insert data  code in mvc using jquery
Controller:-
public class JqueryTestController : Controller
    {
        //
        // GET: /JqueryTest/

        public ActionResult Save()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Save(MyAjax insit)
        {
            string message = "";
            if (ModelState.IsValid)
            {
                try
                {
                    using (JqueryNowTestEntities dc = new JqueryNowTestEntities())
                    {
                        dc.MyAjaxes.Add(insit);
                        dc.SaveChanges();
                        message = "Successfully saved";
                    }
                }
                catch (Exception ex)
                {
                    message = "error ! plz try again";
                }
            }
            else
            {
                message = "plz provide correct data";
            }
            if (Request.IsAjaxRequest())
            {
                return new JsonResult { Data = message, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
            }
            else
            {
                return View(insit);
            }
           // return View(insit);
           
        }

    }
}
View page code:-
@model SanPracticeMvc.MyAjax

@{
    ViewBag.Title = "Save";
}

<h2>Save</h2>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>MyAjax</legend>

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

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

        <p>
            <input type="submit" value="Save" id="MySave" />
        </p>
        <div id="content">

        </div>
    </fieldset>
}

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

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
  <script>
      $(document).ready(function(){
          $("#MySave").click(function(){
              $("#content").html("<b>plz wait ...</b>");
              var dataobject ={
                  Name :  $("#Name").val(),
                  City : $("#City").val()
              };
              $.ajax({
                  url: "@Url.Action("Save","JqueryTest")",
                  type:"POST",
                  data:dataobject,
                  dataType:"json",
                  success: function(data){
                      if(data.toString() =="Successfully saved"){
                          $("#Name").val('');
                          $("#City").val('');
                          $("#content").html("<div> + data +</div>");
                      }
                      else{
                          $("#content").html("<div> + data +</div>");
                      }
                  },
                  error: function(){
                      $("#content").html("<div> error ! plz try again</div>");
                  }
              });
          });
      });
  </script>