• mvc控制器


    控制器

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using System.Net.Http;
    using Newtonsoft.Json;
    using WebApplication1.Models;
    using Webdiyer.WebControls.Mvc;
    namespace WebApplication1.Controllers
    {
    public class showController : Controller
    {

    // GET: show
    /// <summary>
    /// 查询 显示 分页
    /// </summary>
    /// <param name="name"></param>
    /// <param name="type"></param>
    /// <param name="pageIndex"></param>
    /// <returns></returns>
    public ActionResult Index(string name, int type = 0, int pageIndex=1)
    {
    //获取全部数据
    Uri uri = new Uri("http://localhost:1036/"); //api的地址
    HttpClient client = new HttpClient();
    client.BaseAddress = uri;//报头
    client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
    HttpResponseMessage message = client.GetAsync("actionApi/showApi/show?name=" + name + "&type=" + type).Result;

    List<Allcs> tt = new List<Allcs>();//显示在集合里
    if (message.IsSuccessStatusCode)
    {
    string pp = message.Content.ReadAsStringAsync().Result;
    tt = JsonConvert.DeserializeObject<List<Allcs>>(pp);//反序列化
    }
    client.Dispose();//释放

    //下拉
    Uri uris = new Uri("http://localhost:1036/");
    HttpClient clients = new HttpClient();
    clients.BaseAddress = uris;
    clients.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
    HttpResponseMessage mess = clients.GetAsync("actionApi/showApi/xia").Result;
    List<stutype> tts = new List<stutype>();

    if (mess.IsSuccessStatusCode)
    {
    string pp = mess.Content.ReadAsStringAsync().Result;

    tts= JsonConvert.DeserializeObject<List<stutype>>(pp);

    SelectList list = new SelectList(tts, "Sids", "Sname");
    ViewBag.xia = list;
    }
    clients.Dispose();//释放


    return View(tt.ToList().ToPagedList(pageIndex,2)); //显示 反页
    }
    /// <summary>
    /// 添加
    /// </summary>
    /// <returns></returns>
    public ActionResult add()
    {

    //下拉
    Uri uris = new Uri("http://localhost:1036/");
    HttpClient clients = new HttpClient();
    clients.BaseAddress = uris;
    clients.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
    HttpResponseMessage mess = clients.GetAsync("actionApi/showApi/xia").Result;
    List<stutype> tts = new List<stutype>();

    if (mess.IsSuccessStatusCode)
    {
    string pp = mess.Content.ReadAsStringAsync().Result;

    tts = JsonConvert.DeserializeObject<List<stutype>>(pp);

    SelectList list = new SelectList(tts, "Sids", "Sname");
    ViewBag.xia = list;
    }
    clients.Dispose();//释放
    return View();
    }
    /// <summary>
    /// 添加
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    [HttpPost]
    public ActionResult add(stuM model)
    {

    //添加图片  img是一个新建的文件夹
    var file = Request.Files["tu"];
    var names = "/img/" + file.FileName;
    file.SaveAs(Server.MapPath(names));


    //添加mvc里的类
    Allcs m = new Allcs();
    m.men = model.men;
    m.name = model.name;
    m.number = model.number;
    m.shijian = model.shijian;
    m.Sids = model.Sids;
      m.tu = names;


    //传值
    Uri uri = new Uri("http://localhost:1036/");
    HttpClient client = new HttpClient();
    client.BaseAddress = uri;
    client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
    HttpContent cont = new StringContent(JsonConvert.SerializeObject(m));//把类序列化
    cont.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
    var pp = "";
    HttpResponseMessage mesa = client.PostAsync("actionApi/showApi/add", cont).Result;//执行到api
    if (mesa.IsSuccessStatusCode)//判断
    {
    pp = mesa.Content.ReadAsStringAsync().Result;
    }
    if (Convert.ToInt32(pp) > 0)
    {
    return Content("<script>alert('添加成功');location.href = '/show/Index';</script>");
    }
    else
    {
    return Content("<script>alert('添加失败')</script>");
    }


    }
    /// <summary>
    /// 登录
    /// </summary>
    /// <returns></returns>
    public ActionResult login()
    {

    return View();
    }
    /// <summary>
    /// 登录
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    [HttpPost]
    public ActionResult login(loginM model)
    {
    Login m = new Login();
    m.name = model.name;
    m.pwd = model.pwd;
    Session["name"] = m.name;

    Uri uri = new Uri("http://localhost:1036/");
    HttpClient client = new HttpClient();
    client.BaseAddress = uri;
    client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
    HttpContent cont = new StringContent(JsonConvert.SerializeObject(m));
    cont.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
    var pp = "";
    HttpResponseMessage mesa = client.PostAsync("actionApi/showApi/login", cont).Result;
    if (mesa.IsSuccessStatusCode)
    {
    pp = mesa.Content.ReadAsStringAsync().Result;
    }
    if (Convert.ToInt32(pp) > 0)
    {
    return Content("<script>alert('登录成功');location.href = '/show/Index';</script>");
    }
    else
    {
    return Content("<script>alert('登录失败')</script>");
    }

    }
    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>

    public ActionResult shan(int id)
    {

    Uri uri = new Uri("http://localhost:1036/");
    HttpClient client = new HttpClient();
    client.BaseAddress = uri;
    client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
    var pp = "";
    HttpResponseMessage mesa = client.DeleteAsync("actionApi/showApi/shan/" + id).Result;
    if (mesa.IsSuccessStatusCode)
    {
    pp = mesa.Content.ReadAsStringAsync().Result;
    }
    if (Convert.ToInt32(pp) > 0)
    {
    return Content("<script>alert('删除成功');location.href = '/show/Index';</script>");
    }
    else
    {
    return Content("<script>alert('删除失败')</script>");
    }


    }

    /// <summary>
    /// 修改
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public ActionResult xiu(int id)
    {
    Uri uri = new Uri("http://localhost:1036/");
    HttpClient client = new HttpClient();
    client.BaseAddress = uri;
    client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
    HttpResponseMessage message = client.GetAsync("actionApi/showApi/dan?id=" + id).Result;
    List<Allcs> tt = new List<Allcs>();
    if (message.IsSuccessStatusCode)
    {
    string pp = message.Content.ReadAsStringAsync().Result;
    tt = JsonConvert.DeserializeObject<List<Allcs>>(pp);
    }
    client.Dispose();//释放

    //下拉
    Uri uris = new Uri("http://localhost:1036/");
    HttpClient clients = new HttpClient();
    clients.BaseAddress = uris;
    clients.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
    HttpResponseMessage mess = clients.GetAsync("actionApi/showApi/xia").Result;
    List<stutype> tts = new List<stutype>();

    if (mess.IsSuccessStatusCode)
    {
    string pp = mess.Content.ReadAsStringAsync().Result;

    tts = JsonConvert.DeserializeObject<List<stutype>>(pp);

    SelectList list = new SelectList(tts, "Sids", "Sname");
    ViewBag.xia = list;
    }
    clients.Dispose();//释放


    return View(tt[0]);//
    }
    /// <summary>
    /// 修改
    /// </summary>
    /// <param name="mdoel"></param>
    /// <returns></returns>
    [HttpPost]
    public ActionResult xiu(stuM mdoel)
    {

    //修改图片  img是一个新建的文件夹
    var file = Request.Files["tu"];
    var names = "/img/" + file.FileName;
    file.SaveAs(Server.MapPath(names));

     //修改传值

    Allcs m = new Allcs();
    m.men = mdoel.men;
    m.name = mdoel.name;
    m.number = mdoel.number;
    m.shijian =mdoel.shijian;
    m.Sids = mdoel.Sids;
    m.id = mdoel.id;

    //地址

    Uri uri = new Uri("http://localhost:1036/");
    HttpClient client = new HttpClient();
    client.BaseAddress = uri;
    client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
    HttpContent cont = new StringContent(JsonConvert.SerializeObject(m));

    cont.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
    var pp = "";
    HttpResponseMessage mesa = client.PutAsync("actionApi/showApi/xiu", cont).Result;
    if (mesa.IsSuccessStatusCode)
    {
    pp = mesa.Content.ReadAsStringAsync().Result;
    }
    if (Convert.ToInt32(pp) > 0)
    {
    return Content("<script>alert('修改成功');location.href = '/show/Index';</script>");
    }
    else
    {
    return Content("<script>alert('修改失败')</script>");
    }


    }
    }
    }

    显示页面的方法

    @using Webdiyer.WebControls.Mvc; //分页
    @model PagedList<WebApplication1.Allcs> 

    类型:

    @Html.DropDownList("typeid",ViewBag.xia as SelectList,"选择")
    收费人:<input id="Text1" type="text" />
    <input id="Button1" type="button" value="查询" onclick="cha()" />
    <input id="Button1" type="button" value="添加" onclick="yang()" />
    <table>
    <tr>
    <td>编号</td>
    <td>门牌号</td>
    <td>类型</td>
    <td>费用</td>
    <td>收费人</td>
    <td>时间</td>
    <td>操作</td>
    </tr>
    <tbody>
    @foreach (var item in Model)
    {
    <tr>
    <td>@item.id</td>
    <td>@item.men</td>
    <td>@item.Sname</td>
    <td>@item.number</td>
    <td>@item.name</td>
    <td>@item.shijian</td>
    <td><a href="#" onclick="shan(@item.id)">删除</a>
    <a href="/show/xiu/@item.id" >修改</a></td>
    </tr>
    }
    </tbody>
    </table>
    @*分页*@
    @Html.Pager(Model, new PagerOptions { PageIndexParameterName = "pageIndex" })    //分页
    </div>
    <script>
    //添加
    function yang()
    {
    location.href = '/show/add';
    }
    //查询
    function cha() {

    var name = $("#Text1").val();
    var type = $("#typeid").val();
    location.href = '/show/Index?name=' + name + "&type=" + type;
    }
    //删除
    function shan(id)
    {
    location.href = '/show/shan?id='+id;
    }
    </script>

    添加页面的方法

    @model WebApplication1.Allcs

    //"add", "show", FormMethod.Post, new { @enctype = "multipart/form-data" } 做添加图片时用

    @using (Html.BeginForm("add", "show", FormMethod.Post, new { @enctype = "multipart/form-data" })) 
    {
    <table>
    <tr>
    <td>门牌号</td>
    <td>@Html.TextBoxFor(s=>s.men)</td>
    </tr>
    <tr>
    <td>类别</td>
    <td>@Html.DropDownListFor(s=>s.Sids,ViewBag.xia as SelectList)</td>
    </tr>
    <tr>
    <td>费用</td>
    <td>@Html.TextBoxFor(s => s.number)</td>
    </tr>
    <tr>
    <td>收费人</td>
    <td>@Html.TextBoxFor(s => s.name)</td>
    </tr>
    <tr>
    <td>时间</td>
    <td>@Html.TextBoxFor(s => s.shijian)</td>
    </tr>

    <tr>
    <td>图片</td>
    <td><input type="file" name="tu" /> </td>
    </tr>

    <tr>
    <td></td>
    <td><input id="Submit1" type="submit" value="添加" /></td>
    </tr>
    </table>

    }

    修改的方法

     @model WebApplication1.Allcs 

     //"add", "show", FormMethod.Post, new { @enctype = "multipart/form-data" } 做修改图片时用

    @using (Html.BeginForm())
    {
    <table>
    <tr>
    <td>门牌号</td>
    <td>@Html.TextBoxFor(s => s.men)</td>
    </tr>
    <tr>
    <td>类别</td>
    <td>@Html.DropDownListFor(s => s.Sids, ViewBag.xia as SelectList)</td>
    </tr>
    <tr>
    <td>费用</td>
    <td>@Html.TextBoxFor(s => s.number)</td>
    </tr>
    <tr>
    <td>收费人</td>
    <td>@Html.TextBoxFor(s => s.name)</td>
    </tr>
    <tr>
    <td>时间</td>
    <td>@Html.TextBoxFor(s => s.shijian)</td>
    </tr>

    <tr>
    <td>图片</td>
    <td><img src="@Model.tu" alt="" style="50px;height:50px" /></td>
    <td><input type="file" name="tu" /> </td>
    </tr>


    <tr>
    <td><input id="Text1" type="hidden" value="@Model.id" /></td>
    <td><input id="Submit1" type="submit" value="添加" /></td>
    </tr>
    </table>

    }

    配置

    config.Routes.MapHttpRoute(
    name: "actionApi",
    routeTemplate: "actionApi/{controller}/{action}/{id}",
    defaults: new { id = RouteParameter.Optional }
    );

  • 相关阅读:
    JS加密对应的c#解码
    Content-Type: application/www-form-urlencoded
    使用abcpdf分页设置的问题
    Windows10远程报错:由于CredSSP加密Oracle修正
    ueditorUE 去掉本地保存成功的提示框!
    js进制转换
    缓存穿透 缓存雪崩 缓存击穿
    DTcms 模版用vs2015或2017 打开编辑时候 粘贴出问题 代码被调整
    通俗简述 依赖倒置•控制反转•依赖注入•面向接口编程 的思想
    tcp/ip
  • 原文地址:https://www.cnblogs.com/net520/p/7875022.html
Copyright © 2020-2023  润新知