This is in addition to my article
CRUD in single View. You can find the details of the class library, which I had used here.
For creating a JSON file, create a simple MVC project.
- Add a Contoller to your project.
- Add an ActionResult method, named as Index in your controller.
- Now, add a view to the Index and do the following steps in your view.
- @model IEnumerable<JsonEntity.Users>
-
-
- @{
- ViewBag.Title = "Index";
- }
-
- <h2>Index</h2>
- <script src="https://code.jquery.com/jquery-3.1.0.min.js" integrity="sha256-cCueBR6CsyA4/9szpPfrX3s49M9vUU5BgtiJj06wt/s=" crossorigin="anonymous"></script>
- <p>
- <input type="button" id="btnCreateJson" value="Create Json" class="btn btn-default"/>
- </p>
Now, add JavaScript.
- $("#btnCreateJson").click(function () {
- $.post("/Users/CreateJson", function (response) { //Users is my controller's Name and CreateJson is my Jsonresult Method
- if (response != null) {
- alert("Json Created");
- location.reload();
- }
- else {
- alert("Error");
- }
- });
- })
Add JsonResult Method to your controller, as given below:
- [HttpPost]
- public ActionResult CreateJson()
- {
- try
- {
- using (JsonContext context = new JsonContext())
- {
- var UsersList = context.ObjUser.ToList();
- var jsondata = new JavaScriptSerializer().Serialize(UsersList);
- string path = Server.MapPath("~/Json/");
- System.IO.File.WriteAllText(path + "User.json", jsondata);
- return Json(UsersList, JsonRequestBehaviour.AllowGet);
- }
- }
- catch(Exception ez)
- {
- return null;
- }
- }
Now, your JSON file has been created.
For retrieving whole data from JSON file, write the code, given below:.
- $('#get-data').click(function () {
- var url="/Json/User.json";
- $.getJSON(url, function (data) {
- console.log(data);
- $('#show-data').empty();
-
- if (data.length) {
- var content="<table class='table'>";
- for(var i=0;i<data.length;i++)
- {
- content+="<tr><td>"+data[i].Name+"</td><td>"+data[i].Address+"</td><td>"+data[i].Phone_Number+"</td></tr>";
- }
- content+="</table>";
- $('#show-data').append(content);
- }
- });
-
- showData.text('Loading the JSON file.');
- })
For searching the users from JSON file, add a TextBox in your Index view.
- <input type="text" id="Search" placeholder="Enter data"/>
Here, I am adding a KeyUp function of JavaScript, as I want to search the data as soon as the alphabet is typed from the keyboard.
- $("#Search").keyup(function (e) {
- clearTimeout($.data(this, 'timer'));
- if (e.keyCode == 13)
- DataFromJson(true);
- else
- $(this).data('timer', setTimeout(DataFromJson, 50));
- })
- function DataFromJson(force)
- {
- var event = $("#Search").val();
- var url = "/Json/User.json";
- $.getJSON(url, function (response) {
-
- if (response.length) {
- $("#tableUser").empty();
- var content = "<tr><th>Name</th><th>Address</th><th>Phone Number</th></tr>";
- for (var i = 0; i < response.length; i++) {
- if ((response[i].Name).indexOf(event) != -1 || (response[i].Address).indexOf(event) != -1 || (response[i].Phone_Number).indexOf(event) != -1)
- content += "<tr id='TableData'><td>" + response[i].Name + "</td><td>" + response[i].Address + "</td><td>" + response[i].Phone_Number + "</td></tr>";
- }
- }
- $("#tableUser").append(content);
- }
- });
- }
That's it. You can create and retrieve the data from JSON file. Searching is done without and page loads with the single alpha matching.