正式学习MVC 04
1、ActionResult
ActionResult是一个父类,
子类包括了我们熟知的
ViewResult 返回相应的视图
ContentResult 返回字符串
RedirectResult( return Redirect(url:xxxx)) 重定向
RedirectToRouteResult (return RedirectToAction(actionName,controllerName:xxx)) 根据路由重定向
FileResult 向客户端输出文件
JsonResult 向客户端返回JSON对象
HttpStatusCodeResult 显示不同的状态码
PartialViewResult 返回部分页面
跳转到其他的行为(方法)
返回一个文件
一个图片的上传与查看功能实现
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<img src="/Demo/BrowsePic?name=固定的name" alt="Alternate Text" />
<form action="/Demo/UploadFile" method="post" enctype="multipart/form-data">
<input type="file" name="pic" value="" />
<input type="submit" name="" value="提交" />
</form>
</body>
</html>
public ActionResult UploadFile(HttpPostedFileBase pic)
{
var filename = DateTime.Now.Ticks + pic.FileName;
pic.SaveAs(filename: Request.MapPath(basePath + filename));
var info = filename + "has been saved.";
return Content(filename);
} public ActionResult BrowsePic(string name)
{
return File(basePath + name, contentType: "image/jpg");
}
获取文件大小
file.ContentLength 单位是Byte
2、JSON
默认不允许get请求
public ActionResult Index()
{
// 允许get请求
return Json(new { id = , name = "Jack" }, JsonRequestBehavior.AllowGet);
}
单独写Action进行ajax操作
3、状态码
public ActionResult Index()
{
return new HttpStatusCodeResult(System.Net.HttpStatusCode.NotFound);
}
.InternalServerError 500
4、部分视图
不会添加Layout
public PartialViewResult GetPartial()
{
return PartialView();
}
<h2>这是部分页面的视图</h2>
作用类似于组件,适合作为组件被使用,使用方法:
<h1>index page for demo</h1> <p>引用部分视图</p> @Html.Action("GetPartial")
另外,也可以在shared文件夹下创建分部页:
但是使用方法与上面不同:
<h1>index page for demo</h1> <p>引用部分视图</p> @Html.Action("GetPartial")
@Html.Partial("_PartialPage1")
这种方式如何传参?通过其第二个参数:
主视图:
@using MVCStudy.Models <h1>index page for demo</h1> <p>引用部分视图</p> @Html.Action("GetPartial")
@Html.Partial("_PartialPage1", new Animal{ Name = "pig" ,Sex = "male"})
分部视图:
@model MVCStudy.Models.Animal
<h2>你好,我是在shared目录下创建的分部页</h2> <p>接下来是我所接收的参数</p> <p>@Model.Name</p>
<p>@Model.Sex</p>
正式学习MVC 04的更多相关文章
- 正式学习MVC 01
1.新建项目 点击创建新项目,选择ASP.NET web应用程序,对项目进行命名后点击创建. 截图如下: 取消勾选HTTPS配置 可选择空 + mvc 或直接选定MVC 2.目录结构分析 1) App ...
- 正式学习MVC 05
1.剃须刀模板razor的使用 1)混编 循环语法 @model List<MVCStudy.Models.Student> @{ ViewBag.Title = "List&q ...
- 正式学习MVC 02
1.cookie 继续讲解MVC的内置对象cookie 相对不安全 1)保存cookie public ActionResult Index() { // 设置cookie以及过期时间 Respons ...
- 正式学习MVC 06
1.Model常用属性讲解 using System; using System.ComponentModel.DataAnnotations; namespace MVCStudy2.Models ...
- 正式学习MVC 03
1.View -> Controller的数据通信 1) 通过url查询字符串 public ActionResult Index(string user) { return Content(u ...
- 白话学习MVC(十)View的呈现二
本节将接着<白话学习MVC(九)View的呈现一>来继续对ViewResult的详细执行过程进行分析! 9.ViewResult ViewResult将视图页的内容响应给客户端! 由于Vi ...
- 学习MVC之租房网站(二)-框架搭建及准备工作
在上一篇<学习MVC之租房网站(一)-项目概况>中,确定了UI+Service的“双层”架构,并据此建立了项目 接下来要编写Common类库.配置AdminWeb和FrontWeb 一.编 ...
- Redis:学习笔记-04
Redis:学习笔记-04 该部分内容,参考了 bilibili 上讲解 Redis 中,观看数最多的课程 Redis最新超详细版教程通俗易懂,来自 UP主 遇见狂神说 10. Redis主从复制 1 ...
- 白话学习MVC(八)Action的执行二
一.概述 上篇博文<白话学习MVC(七)Action的执行一>介绍了ASP.NET MVC中Action的执行的简要流程,并且对TempData的运行机制进行了详细的分析,本篇来分析上一篇 ...
随机推荐
- Java--类以及对象
什么是类 就是将一类事物的相同的本质特性抽象出来,类具有属性和方法,属性就是特征(具有什么),方法就是行为(能做什么). 类是一种引用的数据类型,类创建的对象的过程叫做实例化 什么是对象 对象就是类中 ...
- CodeForces 91B Queue (线段树,区间最值)
http://codeforces.com/problemset/problem/91/B B. Queue time limit per test: 2 seconds memory limit p ...
- String Distance and Transform Process
http://acm.hdu.edu.cn/showproblem.php?pid=1516 Problem Description String Distance is a non-negative ...
- Linux使用/proc/stat计算CPU使用率
在Linux下,CPU利用率分为用户态,系统态和空闲态,分别表示CPU处于用户态执行的时间,系统内核执行的时间,和空闲系统进程执行的时间,三者之和就是CPU的总时间,当没有用户进程.系统进程等需要执行 ...
- Create Access Point on Archlinux
Create Access Point on Archlinux */--> Create Access Point on Archlinux 1 Solution Download creat ...
- [LC] 438. Find All Anagrams in a String
Given a string s and a non-empty string p, find all the start indices of p's anagrams in s. Strings ...
- CAS 5.3.x 相关信息
CAS 5.3.x 相关信息 单点登录系统 学习网站: https://www.apereo.org/projects/cas 官方网站 https://github.com/apereo/cas-o ...
- 吴裕雄--天生自然python学习笔记:Matplotlib 基本绘图
使用 Matplotlib 组件绘图时,经常要与 Numpy 组件搭配使用 . 使用 Matplotlib 绘图首先要导入 Matplotlib 组件 , 由于大部分绘图功能是在 matplotlib ...
- python与正则不得不说的故事
今日所得 正则表达式 re模块 正则表达式:字符 元字符 匹配内容 . 匹配除换行符以外的任意字符 \w 匹配字母或数字或下划线 \s 匹配任意的空白符 \d 匹配数字 \n 匹配一个换行符 \t 匹 ...
- JavaScript设计模式一:工厂模式和构造器模式
转自:http://segmentfault.com/a/1190000002525792 什么是模式 前阵子准备期末考试,挺累也挺忙的,实在闲不得空来更新文章,今天和大家说说javascript中的 ...