AspNet MVC4 教育-28:Asp.Net MVC4 Ajax技术部门四舍五入余速Demo
A.创建一个Basic项目类型。
B.于Models创建一个文件夹:
DivModel.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations; namespace MvcAjaxDivTest.Models
{
public class DivModel
{
[Required(ErrorMessage = "请输入一个整数.")]
public int Dividend {set; get;}
[Required(ErrorMessage = "请输入一个整数.")]
public int Divisor {set; get;}
public int? Answer { set; get; }
public int? Remainder { set; get;}
}
}
C.创建HomeController.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcAjaxDivTest.Models; namespace MvcAjaxDivTest.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/ public ActionResult Index()
{
return View();
}
public ActionResult Div(DivModel m)
{
if (m.Divisor == 0)
{
return Json(new { DivisorTip = "除数不能为0", Answer = "", Remainder = "" }, JsonRequestBehavior.AllowGet);
}
return Json(new { DivisorTip= "", Answer = m.Dividend / m.Divisor, Remainder = m.Dividend % m.Divisor }, JsonRequestBehavior.AllowGet);
}
}
}
D.创建对应的Home/Index.cshtml:
@model MvcAjaxDivTest.Models.DivModel @{
ViewBag.Title = "Ajax Div Test";
} <h2>Ajax Div Test</h2>
@using (Ajax.BeginForm("Div", "Home", new AjaxOptions {OnSuccess = "ShowResult"}))
{
@Html.ValidationSummary(true) <fieldset>
<legend>DivModel</legend> <div class="editor-label">
@Html.LabelFor(model => model.Dividend)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Dividend)
@Html.ValidationMessageFor(model => model.Dividend)
</div> <div class="editor-label">
@Html.LabelFor(model => model.Divisor)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Divisor)<span id="DivisorTip"></span>
@Html.ValidationMessageFor(model => model.Divisor)
</div> <div class="editor-label">
@Html.LabelFor(model => model.Answer)
</div>
<div id="Answer" class="editor-field">
@Html.DisplayFor(model => model.Answer)
</div> <div class="editor-label">
@Html.LabelFor(model => model.Remainder)
</div>
<div id="Remainder" class="editor-field">
@Html.DisplayFor(model => model.Remainder)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
@*<script src="/Scripts/jquery-1.7.1-vsdoc.js" type="text/javascript"></script>*@
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
<script language="javascript" type="text/javascript">
function ShowResult(data) {
$("#DivisorTip").html(data.DivisorTip);
$("#Answer").html(data.Answer);
$("#Remainder").html(data.Remainder);
}
</script>
}
E.效果示范:
版权声明:本文博主原创文章,博客,未经同意不得转载。
AspNet MVC4 教育-28:Asp.Net MVC4 Ajax技术部门四舍五入余速Demo的更多相关文章
- AspNet MVC4 教学-22:Asp.Net MVC4 Partial View 技术高速应用Demo
A.创建Basic类型的MVC项目. B.Model文件夹下,创建文件: LoginModel.cs: using System; using System.Collections.Generic; ...
- AspNet MVC4 教学-23:Asp.Net MVC4 Display And Editor 模板技术高速应用Demo
A.创建Basic类型的项目. B.在Model文件夹下,创建3个文件: Role.cs: using System; using System.Collections.Generic; using ...
- AspNet MVC4 教学-27:Asp.Net MVC4 自己定义helper及function的高速Demo
A.创建Basic类型项目. B.创建App_Code目录,在里面创建2个cshtml文件: MyHelper.cshtml: @helper MyTruncate(string input, int ...
- ASP.NET中AJAX的异步加载(Demo演示)
此次的Demo是一个页面,页面上有两行字,然后后面用AJAX,使用一个下拉框去替换第一行文字 第一个是被替换的网页 <!DOCTYPE html> <html> <hea ...
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(28)-系统小结
原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(28)-系统小结 我们从第一节搭建框架开始直到二十七节,权限管理已经告一段落,相信很多有跟上来的园友,已经 ...
- 如何构建ASP.NET MVC4&JQuery&AJax&JSon示例
背景: 博客中将构建一个小示例,用于演示在ASP.NET MVC4项目中,如何使用JQuery Ajax. 步骤: 1,添加控制器(HomeController)和动作方法(Index),并为Inde ...
- Asp.net MVC4 Step By Step(4)-使用Ajax
Ajax技术就是利用Javascript和XML技术实现这样的效果, 可以向Web服务器发送异步请求, 返回更新部分页面的数据, 而不需要全部更新整个页面. Ajax请求两种类型的内容, 一种是服务端 ...
- SignalR + KnockoutJS + ASP.NET MVC4 实现井字游戏
1.1.1 摘要 今天,我们将使用SignalR + KnockoutJS + ASP.NET MVC实现一个实时HTML5的井字棋游戏. 首先,网络游戏平台一定要让用户登陆进来,所以需要一个登陆模块 ...
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(1)-前言与目录(持续更新中...)
转自:http://www.cnblogs.com/ymnets/p/3424309.html 曾几何时我想写一个系列的文章,但是由于工作很忙,一直没有时间更新博客.博客园园龄都1年了,却一直都是空空 ...
随机推荐
- 使用Visual Studio 寻找App性能瓶颈
使用Visual Studio 寻找App性能瓶颈 最后更新日期:2014-05-05 阅读前提: 环境:Windows 8.1 64bit英文版,Visual Studio 2013 专业版Upda ...
- ajax基本概念,方法
ajax Asynchronous javascript and xml异步的 javascript and XMLajax 是一门在不刷新网页的情况下,与服务器进行交互更新部分网页的技术: 传 ...
- HTTP真的很简单(转)
原文:HTTP Made Really Easy因为我本身网络基础就很差,所以看到这篇文章一方面是学习网络知识,另一方面为了锻炼我蹩脚的英语水平,文中如有错误,欢迎浏览指正! 前言 在看这篇文章的时候 ...
- 自定义ComboBox,简简单单实现
private void Button_Click(object sender, RoutedEventArgs e) { Popup1.PlacementTarget = TesTextBox; P ...
- QTP脚本--应用参数化来测试某个输入框
以前一直觉得自己没有写代码的资质,太急于求成,以为一天就能写好几个功能,几千行代码,于是就没耐心了,没心情学下去了....但是最近发现其实写代码是一个漫长的过程,都是在修修改改中成长起来的.于是今天试 ...
- shell手册--笨鸟杰作
==================================================================================================== ...
- Windows Phone开发(21):做一个简单的绘图板
原文:Windows Phone开发(21):做一个简单的绘图板 其实我们今天要说的就是一个控件--InkPresenter,这个控件并不是十分强大,没办法和WPF中的InkCanvas相比,估计在实 ...
- HP服务器更换主板前后的注意事项
问题 X86服务器免不了会出现故障,以我们实验室使用的HP服务器为例,已经有几台因为各种故障更换过主板了,更换主板前后有些事情需要我们应该注意或处理, 这里记录如下,其中第二.三点是针对我们所使用的C ...
- poj 3259(bellman最短路径)
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 30169 Accepted: 10914 Descr ...
- lua 远程调试 【zeroBrane 使用mobdebug】(good转)
最近基于业务需求,学习了如何使用zeroBrane这个IDE实现C/S 模式下的 lua远程调试,废话不多,上效果图: ---------------------------------------- ...