近来在使用ext.net MVC 。由于还不是特别熟悉,所以先把使用中的一点一滴经验记录下来,以后再系统的整理。
这篇博客主要说明点击一个按钮如何调用Controler中的方法。
首先我们看一下view中的代码:
@{
ViewBag.Title = "Login";
Layout = null;
}
<html>
<head>
<title>@ViewBag.Title</title>
</head>
<body>
@Html.X().ResourceManager(ViewBag.ManagerConfig as MvcResourceManagerConfig)
@(
Html.X().Panel()
.Title("Login")
.Width(350)
.BodyPadding(5)
.Frame(true)
.Layout(LayoutType.Form)
.Items(
Html.X().TextField().ID("txtUserName").FieldLabel("User Name").AllowBlank(false),
Html.X().TextField().ID("txtPassword").FieldLabel("Password")
)
.Buttons(
Html.X().Button()
.Text("Login")
.Handler("App.direct.Identify(App.txtUserName.getValue(), App.txtPassword.getValue());")
)
.X(400)
.Y(200)
)
</body>
</html>
然后,我们看一下这段代码生成的页面,
这段代码生成的页面如下图:
这是一个非常简单的登录页面,两个文本框,一个按钮。
问题是当点击login的时候,controler中如何进行验证呢?
我们只要在对应的controler中做两件事情:
1.把controler类标记为: [DirectController]
2.把对应的方法标记为:[DirectMethod]
如下代码所示:
using System.Web.Mvc;
using Ext.Net.MVC;
using Ext.Net;
namespace AtHome.Controllers
{
[DirectController]
public class LoginController : Controller
{
// GET: /Login/
public ActionResult Index()
{
return View();
}
[DirectMethod]
public ActionResult Identify(string name, string password)
{
//TODO: Identify username and password
return this.Redirect("/main/index");
}
}
}
按照以上的去做,在点击login按钮的时候,就会调用controler中的Identify方法了,并且会传递参数。
但是为什么会这样呢?
当把类和对应的方法做了标记以后,ext.net 会在页面生成对应的代理方法,通过查看网页的源码可以看到以下内容
Ext.apply(App.direct, { Identify:function(name,password,config){return Ext.net.DirectMethod.request("Identify",Ext.applyIf(config || {}, {params:{name:name,password:password},url:"/Login/Identify"}));} });