MVC3 之asp.net 与vb.net 互转练习
vb.net mvc3相关教程http://www.asp.net/mvc/overview/older-versions/getting-started-with-aspnet-mvc3/vb/adding-a-view
asp.net mvc3太多了,不列举了。
vb.net 相关基础
http://www.lob.cn/jq/kfjq/1933.shtml
Dim namedCust = New Customer With {.Name = "Terry Adams"}
匿名类型没有可用的名称。因此,匿名类型的实例化不能包含类名称。
Dim anonymousCust = New With {.Name = "Hugo Garcia"}
开始正文 asp.net 原文是http://www.cnblogs.com/willick/p/3418517.html
使用 MVC Unobtrusive Ajax
在 MVC 中使用 Unobtrusive Ajax ,首先要将其“开启”,需要做两个动作。一个是配置根目录下的 Web.config 文件,在 configuration/appSettings 节点下的 UnobtrusiveJavaScriptEnabled 值设为 true,如下所示:
... <configuration> <appSettings> ... <add key="UnobtrusiveJavaScriptEnabled" value="true" /> </appSettings> </configuration> ...
UnobtrusiveJavaScriptEnabled 的值在程序创建的时候默认为true,在开发的时候有时候只需要检查一下。第二个动作就是在需要使用 MVC Unobtrusive Ajax 的 View 中引入jquery库和jquery.unobtrusive-ajax.min.js文件,一般更为常见的是在 /Views/Shared/_Layout.cshtml 中引入,如下:
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width" /> <title>@ViewBag.Title</title> <script src="~/Scripts/jquery-1.8.2.min.js"></script> <script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script> </head> <body> @RenderBody() </body> </html>
现在我们来做一个使用 Unobtrusive Ajax 的例子,从服务器获取一个简单的用户列表。为此我们需要准备一个Model,如下:
namespace MvcApplication1.Models { public class Person { public string ID { get; set; } public string Name { get; set; } public Role Role { get; set; } } public enum Role { Admin, User, Guest } }
我一般习惯先写后台方法,再写UI。创建一个名为 People 的 controller, 在该 controller 中写好要用的 action,代码如下:
public class PeopleController : Controller { private Person[] personData = { new Person {ID = "ZhangSan", Name = "张三", Role = Role.Admin}, new Person {ID = "LiSi", Name = "李四", Role = Role.User}, new Person {ID = "WangWu", Name = "王五", Role = Role.User}, new Person {ID = "MaLiu", Name = "马六", Role = Role.Guest} }; public ActionResult Index() { return View(); } public PartialViewResult GetPeopleData(string selectedRole = "All") { IEnumerable<Person> data = personData; if (selectedRole != "All") { Role selected = (Role)Enum.Parse(typeof(Role), selectedRole); data = personData.Where(p => p.Role == selected); } return PartialView(data); } public ActionResult GetPeople(string selectedRole = "All") { return View((object)selectedRole); } }
这里添加了 GetPeopleData action方法,根据 selectedRole 获取用户数据并传递给 PartialView 方法。
接着为 GetPeopleData action 创建一个partial view:/Views/People/GetPeopleData.cshtml ,代码如下:
@using MvcApplication1.Models @model IEnumerable<Person> @foreach (Person p in Model) { <tr> <td>@p.ID</td> <td>@p.Name</td> <td>@p.Role</td> </tr> }
再创建我们的主视图 /Views/People/GetPeople.cshtml,代码如下:
@using MvcApplication1.Models @model string @{ ViewBag.Title = "GetPeople"; AjaxOptions ajaxOpts = new AjaxOptions { UpdateTargetId = "tableBody" }; } <h2>Get People</h2> <table> <thead><tr><th>First</th><th>Last</th><th>Role</th></tr></thead> <tbody id="tableBody"> @Html.Action("GetPeopleData", new { selectedRole = Model }) </tbody> </table> @using (Ajax.BeginForm("GetPeopleData", ajaxOpts)) { <div> @Html.DropDownList("selectedRole", new SelectList( new[] { "All" }.Concat(Enum.GetNames(typeof(Role))))) <button type="submit">Submit</button> </div> }
vb.net格式的 model
Public Class Person Private _iD As String Public Property ID() As String Get Return _iD End Get Set(ByVal value As String) _iD = value End Set End Property Private _name As String Public Property Name() As String Get Return _name End Get Set(ByVal value As String) _name = value End Set End Property Private _role As Role Public Property Role() As String Get Return _role End Get Set(ByVal value As String) _role = value End Set End Property End Class Public Enum Role Admin User Guest End Enum
PeopleController 代码 这里要注意一点 with 用法,还有默认值可选项 ,还有拉姆达表达式转换
Namespace MvcApplication1 Public Class PeopleController Inherits System.Web.Mvc.Controller Dim personData() As Person = { New Person With { .ID = "ZhangSan", .Name = "张三", .Role = Role.Admin }, New Person With { .ID = "LiSi", .Name = "李四", .Role = Role.User }, New Person With { .ID = "WangWu", .Name = "王五", .Role = Role.User }, New Person With { .ID = "MaLiu", .Name = "马六", .Role = Role.Guest } } ' ' GET: /People Function Index() As ActionResult Return View() End Function Public Function GetPeopleData(Optional ByVal selectedRole As String = "All") As PartialViewResult Dim data As IEnumerable(Of Person) = personData If (selectedRole <> "All") Then ’Dim selected As Role = CType(selectedRole, Role)
Dim selected As Role = CType([Enum].Parse(GetType(Role), selectedRole), Role)'更改后的
' Role selected = (Role)Enum.Parse(typeof(Role), selectedRole);
data = From p In personData Where p.Role = selected End If Return PartialView(data) End Function Public Function GetPeople(Optional ByVal selectedRole As String = "All") As ActionResult Return View(CType(selectedRole, Object)) End Function End Class End Namespace
视图需要注意的地方是:html标签的写法,不能像asp.net中那样随便写了,必须在前面加一个@符号 ,否则出现错误。
GetPeopleData 视图
@Imports MvcApplication1.Models @*可以不写的*@ <table> @For Each p As MvcApplication1.Person In Model @<tr> <td>@p.ID</td> <td>@p.Name</td> <td>@p.Role</td> </tr> Next </table>
GetPeople 视图
@*@Imports MvcApplication1.Models 可以省略*@ @Code ViewData("Title") = "GetPeople" Dim ajaxOpts As AjaxOptions = New AjaxOptions With { .UpdateTargetId = "tableBody" } End Code <h2>GetPeople</h2> <table> <thead><tr><th>First</th><th>Last</th><th>Role</th></tr></thead> <tbody id="tableBody"> @Html.Action("GetPeopleData", New With {.selectedRole = Model}) </tbody> </table> @Using Ajax.BeginForm("GetPeopleData", ajaxOpts) @<div> //这里没有转换成功,需要学习 终于成功了
@Html.DropDownList("selectedRole", New SelectList(
New Object() {"All"}.Concat([Enum].GetNames(GetType(MvcApplication1.Role)))))
<button type="submit">Submit</button> </div> End Using
new[] 对应的vb.net 是 New Object()
没有实现,一直挂在心头,今天终于成功了!也是偶然。
显示效果 图
点击submit后显示
记录一下。