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的更多相关文章

  1. AspNet MVC4 教学-22:Asp.Net MVC4 Partial View 技术高速应用Demo

    A.创建Basic类型的MVC项目. B.Model文件夹下,创建文件: LoginModel.cs: using System; using System.Collections.Generic; ...

  2. AspNet MVC4 教学-23:Asp.Net MVC4 Display And Editor 模板技术高速应用Demo

    A.创建Basic类型的项目. B.在Model文件夹下,创建3个文件: Role.cs: using System; using System.Collections.Generic; using ...

  3. AspNet MVC4 教学-27:Asp.Net MVC4 自己定义helper及function的高速Demo

    A.创建Basic类型项目. B.创建App_Code目录,在里面创建2个cshtml文件: MyHelper.cshtml: @helper MyTruncate(string input, int ...

  4. ASP.NET中AJAX的异步加载(Demo演示)

    此次的Demo是一个页面,页面上有两行字,然后后面用AJAX,使用一个下拉框去替换第一行文字 第一个是被替换的网页 <!DOCTYPE html> <html> <hea ...

  5. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(28)-系统小结

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(28)-系统小结 我们从第一节搭建框架开始直到二十七节,权限管理已经告一段落,相信很多有跟上来的园友,已经 ...

  6. 如何构建ASP.NET MVC4&JQuery&AJax&JSon示例

    背景: 博客中将构建一个小示例,用于演示在ASP.NET MVC4项目中,如何使用JQuery Ajax. 步骤: 1,添加控制器(HomeController)和动作方法(Index),并为Inde ...

  7. Asp.net MVC4 Step By Step(4)-使用Ajax

    Ajax技术就是利用Javascript和XML技术实现这样的效果, 可以向Web服务器发送异步请求, 返回更新部分页面的数据, 而不需要全部更新整个页面. Ajax请求两种类型的内容, 一种是服务端 ...

  8. SignalR + KnockoutJS + ASP.NET MVC4 实现井字游戏

    1.1.1 摘要 今天,我们将使用SignalR + KnockoutJS + ASP.NET MVC实现一个实时HTML5的井字棋游戏. 首先,网络游戏平台一定要让用户登陆进来,所以需要一个登陆模块 ...

  9. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(1)-前言与目录(持续更新中...)

    转自:http://www.cnblogs.com/ymnets/p/3424309.html 曾几何时我想写一个系列的文章,但是由于工作很忙,一直没有时间更新博客.博客园园龄都1年了,却一直都是空空 ...

随机推荐

  1. 《Java并发编程实战》第七章 取消与关闭 读书笔记

        Java没有提供不论什么机制来安全地(抢占式方法)终止线程,尽管Thread.stop和suspend等方法提供了这种机制,可是因为存在着一些严重的缺陷,因此应该避免使用. 但它提供了中断In ...

  2. SWT中Display和Shell是个什么东东

    Display:与操作系统沟通的桥梁 我们在前面说过,每个swt程序在最开始都必须创建一个Display对象.Display对象起什么作用呢?它是swt与操作系统沟通的一座桥梁.它负责swt和操作系统 ...

  3. android_重写button样式

    这样的button样式应该源自IOS.假设安卓上实现,则须要使用android上面的layer-list来实现. 事实上layer-list有点像framlayout,作用就是覆盖. 先说一下实现原理 ...

  4. zimbra启用SMTP认证并绑定认证登录和发件人

    1. smtp认证    1.1 修改mynetworks        登录zimbra后台-->全局配置-->MTA-->信任网络-->127.0.0.0/8        ...

  5. LeetCode: Valid Palindrome [125]

    [题目] Given a string, determine if it is a palindrome, considering only alphanumeric characters and i ...

  6. JavaScript采用append添加的元素错误

    1.错误叙述性说明 于IE览器上: Uncaught HierarchyRequestError:Failed to excute 'appendChild' on 'Node':The new ch ...

  7. MongoDB获得短暂的

    大约MongoDB该数据是现在比较少.和大多数英文网站.最上面的经笔者从官方网站翻译.请翻译或误解之处请作证.然后,我们将继续关注MongoDB,和翻译“Developer Zone”和“Admin ...

  8. Windows Phone开发(7):当好总舵主

    原文:Windows Phone开发(7):当好总舵主 吹完了页面有关的话题,今天我们来聊一下页面之间是如何导航的,在更多情况下,我们的应用程序不会只有一个页面的,应该会有N个,就像我们做桌面应 用开 ...

  9. Wind River Linux 6 Security Profile

    2692407267@qq.com,很多其它内容请关注http://user.qzone.qq.com/2692407267 Wind River Linux 6 Security Profile

  10. 使用 Cordova+Visual Studio 创建跨平台移动应用(1)

    1简介 本章节是关于Visual Studio Tools for Apache Cordova的,目前此产品只发布了预览版.Visual Studio for Apache Cordova帮助熟悉V ...