public ActionResult Index(string id)//主页 //参数string searchString 访问方式为index?searchString=xxxx 。参数string id 访问方式为index/x
{
string searchString = id;
//return View(db.Books.ToList()); //返回一个对象集合
var s = from m in db.Books select m; //查询所有数据
if (!string.IsNullOrEmpty(searchString)) //判断传来的数据是否位空
{
s = s.Where(x => x.BookName.Contains(searchString)); //模糊查询数据
}
return View(s);
}
public ActionResult Edit(int? id) //只能接受整型数据;其他默认null
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);//传递过去400 //返回400页面
}
Book book = db.Books.Find(id); //在books表中查找指定id的对象 赋值给Book对象
if (book == null)
{
return HttpNotFound(); //未找到调用HttpNotFound()方法,传递 NotFound = 404,返回404 页面
}
return View(book); //返回这个对象
}
        [HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "BookID,BookName,Author,Price,dt")] Book book) //编辑
{
if (ModelState.IsValid)
{
db.Entry(book).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(book);
}
[HttpPost, ActionName("Delete")] //重命名方法名 只接受post请求
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id) //删除
{
Book book = db.Books.Find(id); //根据指定ID查找
db.Books.Remove(book); //移除对象
db.SaveChanges(); //保存修改
return RedirectToAction("Index"); //返回主页
}
   public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
return View();// 参数可以返回model对象
}
//返回ViewResult视图结果,将视图呈现给网页
public ActionResult A1()
{
return View();// 参数可以返回model对象
}
//返回PartialViewResult部分视图结果,主要用于返回部分视图内容
public ActionResult A2()
{
return PartialView("ViewUserControl");//在View/Shared目录下创建ViewUserControl.cshtml部分视图
}
//返回ContentResult用户定义的内容类型
public ActionResult A3()
{
return Content("指定文本", "text/html"); // 可以指定文本类型
}
//返回JsonResult序列化的Json对象
public ActionResult A4()
{
//Dictionary<string, object> dic = new Dictionary<string, object>();
//dic.Add("id", 100);
//dic.Add("name", "hello");
List < string> list= new List<string>();
list.Add("xxxx");
list.Add("YYYY");
list.Add("ZZZZ");
//["xxxx","YYYY","ZZZZ"]
return Json(list, JsonRequestBehavior.AllowGet);//若要使用GET请求设置参数为AllowGet
//{"id":100,"name":"hello"}
}
//返回JavaScriptResult可在客户端执行的脚本
public ActionResult A5()
{
string str = string.Format("alter('{0}');", "弹出窗口");
return JavaScript(str);
}
//返回FileResult要写入响应中的二进制输出,一般可以用作要简单下载的功能
public ActionResult A6()
{
string fileName = "~/Content/test.zip"; // 文件名
string downFileName = "文件显示名称.zip"; // 要在下载框显示的文件名
return File(fileName, "application/octet-stream", downFileName);
}
// 返回Null或者Void数据类型的EmptyResult
public ActionResult A7()
{
return null;
} //重定向方法:Redirect / RedirectToAction / RedirectToRoute
//Redirect:直接转到指定的url地址
public ActionResult Redirect()
{
// 直接返回指定的url地址
return Redirect("http://www.baidu.com");
}
// RedirectToAction:直接使用 Action Name 进行跳转,也可以加上ControllerName;也可以带参数
public ActionResult RedirectResult()
{
return RedirectToAction("Index", "Home", new { id = "", name = "liu" });
}
//RedirectToRoute:指定路由进行跳转 //Default为global.asax.cs中定义的路由名称
public ActionResult RedirectRouteResult()
{
return RedirectToRoute("Default", new { controller = "Home", action = "Index" });
}
}

MVC控制器返回值的更多相关文章

  1. mvc 各种返回值

    一个例子胜过千言万语,直接上代码 SpringMVC的Controller控制器返回值详解 SpringMVC Controller 返回值几种类型 Spring MVC 更灵活的控制 json 返回 ...

  2. ASP.NET Core Mvc中空返回值的处理方式

    原文地址:https://www.strathweb.com/2018/10/convert-null-valued-results-to-404-in-asp-net-core-mvc/ 作者: F ...

  3. MVC方法返回值数据

    ModelAndView的作用以及用法 使用ModelAndView类用来存储处理完后的结果数据,以及显示该数据的视图.从名字上看ModelAndView中的Model代表模型,View代表视图,这个 ...

  4. Spring MVC controller返回值类型

    SpringMVC controller返回值类型: 1 String return "user":将请求转发到user.jsp(forword) return "red ...

  5. ZendFramework-2.4 源代码 - 关于MVC - View层 - 控制器返回值

    <?php class ReturnController extends AbstractActionController { public function returnAction() { ...

  6. Spring的MVC控制器返回ModelMap时,会跳转到什么页面?

    控制器中的方法如下: @RequestMapping("/person/personDisplay") public ModelMap defaultHandler() { Sys ...

  7. spring mvc ajax返回值乱码

    加入如下配置: <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHan ...

  8. MVC控制器返回一个list 视图接收

    控制器 public ActionResult InfoFrame() { List<Users> list = new List<Users>(); if (Session[ ...

  9. MVC控制器返回重定向操作

    注意:在使用Ajax请求后台时是不能在后台重定向的! 解决方案: if (userInfoService.CheckUser(username, psd, out msg)) { , msg = &q ...

随机推荐

  1. 51Nod——T 1109 01组成的N的倍数

    https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1109 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 ...

  2. source 命令的用法,是在当前bash环境下执行脚本文件

    原文: http://www.cnblogs.com/softwaretesting/archive/2012/02/13/2349550.html source命令用法: source FileNa ...

  3. 搭建strom 的开发环境 - local mode

    Setting Up a Development Environment This page outlines what you need to do to get a Storm developme ...

  4. 【面试】【Spring常见问题总结】【07】

    [常见面试问题总结文件夹>>>] 61.Spring IoC容器的依赖有两层含义: Bean依赖容器:也就是说Bean要依赖于容器,这里的依赖是指容器负责创建Bean并管理Bean的 ...

  5. 抓包分析TCP的三次握手和四次握手

    问题描写叙述: 在上一篇<怎样对Android设备进行抓包>中提到了,server的开发者须要我bug重现然后提供抓包给他们分析.所以抓好包自己也试着分析了一下.发现里面全是一些TCP协议 ...

  6. oracle Plsql 运行update或者delete时卡死问题解决的方法

    oracle Plsql 运行update或者delete时 遇到过Plsql卡死问题或者导致代码运行sql的时候就卡死. 在开发中遇到此问题的时候,本来把sql复制出来,在plsql中运行,Sql本 ...

  7. 二分查找(c &amp; c++)

    typedef int ElemType; C版本号 [递归版本号] int binSearch2(ElemType List[] ,int x,int head,int tail){ //递归版本号 ...

  8. 保存Activity的状态

    一般来说, 调用onPause()和onStop()方法后的activity实例仍然存在于内存中, activity的全部信息和状态数据不会消失, 当activity又一次回到前台之后, 全部的改变都 ...

  9. ytu2572——猜灯谜

    题目描写叙述 A 村的元宵节灯会上有一迷题: 请猜谜 * 请猜谜 = 请边赏灯边猜 小明想,一定是每一个汉字代表一个数字,不同的汉字代表不同的数字. 请你帮小明把全部的可能的数都找出来吧. 输入 没有 ...

  10. RK摄像头移植【转】

    本文转载自:http://wiki.t-firefly.com/index.php/Firefly-RK3288/Camera 板载资源 Firefly-RK3288 开发板带有一个 MIPI 摄像头 ...