using crudusingapi.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace crudusingapi.Controllers
{

public class MobileController : Controller
{
    HttpClient client = new HttpClient();
    // GET: MobileController
    public async Task<ActionResult> Index()
    {
        List<Mobile> mobile = new List<Mobile>();
        HttpClient client = new HttpClient();
        HttpResponseMessage Response = await client.GetAsync("https://localhost:44342/api/Mobiles");
        if (Response.IsSuccessStatusCode)
        {
            var result = Response.Content.ReadAsStringAsync().Result;
            mobile = JsonConvert.DeserializeObject<List<Mobile>>(result);
            return View(mobile);
        }

        return View();
    }

    // GET: MobileController/Details/5
    public async Task<ActionResult> Details(int id)
    {
        Mobile mobile = new Mobile();
        HttpClient client = new HttpClient();
        HttpResponseMessage Response = await client.GetAsync("https://localhost:44342/api/Mobiles/" + id);
        if (Response.IsSuccessStatusCode)
        {
            var result = Response.Content.ReadAsStringAsync().Result;
            mobile = JsonConvert.DeserializeObject<Mobile>(result);
            return View(mobile);
        }
        return View();

    }

    // GET: MobileController/Create
    public async Task<ActionResult> Create()
    {
        List<Maker> mobile = new List<Maker>();
        HttpClient client = new HttpClient();
        HttpResponseMessage Response = await client.GetAsync("https://localhost:44342/api/Makers");
        if (Response.IsSuccessStatusCode)
        {
            var result = Response.Content.ReadAsStringAsync().Result;
            mobile = JsonConvert.DeserializeObject<List<Maker>>(result);
            ViewBag.Makerid = new SelectList(mobile, "Makerid", "Name");
            return View();
        }

        return View();
    }

    // POST: MobileController/Create
    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Create([Bind] Mobile mobile)
    {

        HttpClient client = new HttpClient();
        StringContent content = new StringContent(JsonConvert.SerializeObject(mobile), Encoding.UTF8, "application/json");
        HttpResponseMessage Response = await client.PostAsync("https://localhost:44342/api/Mobiles", content);
        if (Response.IsSuccessStatusCode)
        {
            return RedirectToAction(nameof(Index));
        }
        return View();
    }

    // GET: MobileController/Edit/5
    public async Task<ActionResult> Edit(int id)
    {
        Mobile mobile = new Mobile();
        HttpClient client = new HttpClient();
        HttpResponseMessage Response = await client.GetAsync("https://localhost:44342/api/Mobiles/" + id);
        if (Response.IsSuccessStatusCode)
        {
            var result = Response.Content.ReadAsStringAsync().Result;
            mobile = JsonConvert.DeserializeObject<Mobile>(result);

        }

        List<Maker> mobile1 = new List<Maker>();
        HttpClient client1 = new HttpClient();
        HttpResponseMessage Response1 = await client1.GetAsync("https://localhost:44342/api/Makers");
        if (Response.IsSuccessStatusCode)
        {
            var result1 = Response1.Content.ReadAsStringAsync().Result;
            mobile1 = JsonConvert.DeserializeObject<List<Maker>>(result1);
            ViewBag.Makerid = new SelectList(mobile1, "Makerid", "Name");
        }
        return View(mobile);
    }

    // POST: MobileController/Edit/5
    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Edit(int id, [Bind] Mobile mobile)
    {
        HttpClient client = new HttpClient();
        StringContent content = new StringContent(JsonConvert.SerializeObject(mobile), Encoding.UTF8, "application/json");
        HttpResponseMessage Response = await client.PutAsync("https://localhost:44342/api/Mobiles/" + id, content);
        if (Response.IsSuccessStatusCode)
        {
            return RedirectToAction(nameof(Index));
        }
        return View();
    }

    // GET: MobileController/Delete/5
    public async Task<ActionResult> Delete(int id)
    {
        string endpoint = "https://localhost:44342/api/Mobiles/" + id;
        HttpClient client = new HttpClient();
        HttpResponseMessage Response = await client.DeleteAsync(endpoint);
        if (Response.IsSuccessStatusCode)
        {
            return RedirectToAction(nameof(Index));
        }

        //return Redirect("Index");
        //return View();
        return RedirectToAction(nameof(Index));
    }

    // POST: MobileController/Delete/5
    //[HttpPost]
    //[ValidateAntiForgeryToken]
    //public ActionResult Delete(int id, IFormCollection collection)
    //{
    //    try
    //    {
    //        return RedirectToAction(nameof(Index));
    //    }
    //    catch
    //    {
    //        return View();
    //    }
    //}
}

}

search

public async Task<ActionResult> Index(int? a1, int? a2, string uname)
{
List<Student> students = new List<Student>();
HttpClient client = api.initial();
HttpResponseMessage res = await client.GetAsync("api/Student");
if (res.IsSuccessStatusCode)
{
var result = res.Content.ReadAsStringAsync().Result;
students = JsonConvert.DeserializeObject<List<Student>>(result);
}
List<Student> name = students.Where(o => o.Age >= a1 && o.Age <= a2).ToList();
if (a1 == null && a2 == null)
{
return View(students);
}
return View(name);
}

=======================
[04/01, 09:3]r: using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using MakerProject.Models;
namespace MakerProject.Controllers
{
public class SaleTbsController : Controller
{
private ExternalEntities db = new ExternalEntities();
// GET: SaleTbs
public ActionResult Index(string maker, string cname,DateTime? min,DateTime? max)
{
var saleTbs = db.SaleTbs.Include(s => s.CustomerTb).Include(s => s.MobileTb).Where(a => a.MobileTb.MakerTb.Mname.Contains(maker != null ? maker : a.MobileTb.MakerTb.Mname) && a.CustomerTb.Cname.Equals(cname != null ? cname : a.CustomerTb.Cname)
&& a.Date >= (min != null ? min : a.Date) && a.Date <= (max != null ? max : a.Date));
return View(saleTbs.ToList());
}
// GET: SaleTbs/Details/5
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
SaleTb saleTb = db.SaleTbs.Find(id);
if (saleTb == null)
{
return HttpNotFound();
}
return View(saleTb);
}
// GET: SaleTbs/Create
public ActionResult Create()
{
ViewBag.Cid = new SelectList(db.CustomerTbs, "Cid", "Cname");
ViewBag.Moid = new SelectList(db.MobileTbs, "Moid", "Model");
return View();
}
// POST: SaleTbs/Create
// To protect from overposting attacks, enable the specific properties you want to bind to, for
// more details see https://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Sid,Moid,Cid,Date,Bill")] SaleTb saleTb)
{
if (ModelState.IsValid)
{
db.SaleTbs.Add(saleTb);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.Cid = new SelectList(db.CustomerTbs, "Cid", "Cname", saleTb.Cid);
ViewBag.Moid = new SelectList(db.MobileTbs, "Moid", "Model", saleTb.Moid);
return View(saleTb);
}
// GET: SaleTbs/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
SaleTb saleTb = db.SaleTbs.Find(id);
if (saleTb == null)
{
return HttpNotFound();
}
ViewBag.Cid = new SelectList(db.CustomerTbs, "Cid", "Cname", saleTb.Cid);
ViewBag.Moid = new SelectList(db.MobileTbs, "Moid", "Model", saleTb.Moid);
return View(saleTb);
}
// POST: SaleTbs/Edit/5
// To protect from overposting attacks, enable the specific properties you want to bind to, for
// more details see https://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "Sid,Moid,Cid,Date,Bill")] SaleTb saleTb)
{
if (ModelState.IsValid)
{
db.Entry(saleTb).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.Cid = new SelectList(db.CustomerTbs, "Cid", "Cname", saleTb.Cid);
ViewBag.Moid = new SelectList(db.MobileTbs, "Moid", "Model", saleTb.Moid);
return View(saleTb);
}
// GET: SaleTbs/Delete/5
public ActionResult Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
SaleTb saleTb = db.SaleTbs.Find(id);
if (saleTb == null)
{
return HttpNotFound();
}
return View(saleTb);
}
// POST: SaleTbs/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
SaleTb saleTb = db.SaleTbs.Find(id);
db.SaleTbs.Remove(saleTb);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
}
[04/01, 09:39]r: public ActionResult Index(string name,int? min,int? max)
{
HttpClient client = new HttpClient();
var response = client.GetAsync("http://localhost:31823/api/EmpTbs?name="+name+"&min="+min+"&max="+max).Result;
string data = response.Content.ReadAsStringAsync().Result;
List<EmpTb> emp = JsonConvert.DeserializeObject<List<EmpTb>>(data);
return View(emp.ToList());
}
==========

Edit
Pub: 03 Jan 2022 19:44 UTC
Edit: 04 Jan 2022 04:15 UTC
Views: 273