MVC自我学习:
ViewData管理传值;
<%: %> 取值;
1:HtmlHelper
(1):超链接
//new { name = "admin", pwd = "123" }//路由参数
//new { @class="txt" }Html参数;
<%:Html.ActionLink("关于", "About", "Home", new { name = "admin", pwd = "123" },
new { @class="txt" } )%>
(2):DropDownList()
List<SelectListItem> list = new List<SelectListItem>(){
new SelectListItem(){ Text="北京", Value="1"},
new SelectListItem(){ Text="山东", Value="2", Selected=true}
};
ViewData["city"] = list;//ViewData的名字与下拉框的名字一样,那么自动填充到下拉框中。
(3):<%: %>与<%=%>
<%: %>:进行了HTML编码htmlEncode
<%= %>:原样输出
(4):RadioButton的应用
2:MVCHtmlHelper扩展方法
namespace System.Web.Mvc
{
//1:扩展方法所在的类必须是静态类
//2:扩展方法必须是静态方法
//3:第一个参数必须是this关键字,后面跟的是对哪个类型进行扩展(为哪个类添加的扩展方法)
//MvcHtmlString,不会再对HTML进行编码
public static class htmlHelperExt
{
public static MvcHtmlString MyLabel(this HtmlHelper helper, string txt)
{
//返回一个MvcHtmlString;
//MvcHtmlString mvcstring = new MvcHtmlString(string.Format("<span style='color:red'>
{0}</span>", txt));
//return mvcstring;
string temp= string.Format("<span style='color:red'>{0}</span>",txt);
return MvcHtmlString.Create(temp);
}
}
}
(1):伙伴类的使用
(2):自己定义验证规则
(3):使用Jquery的Validate进行校验
<script type="text/javascript">
$(function () {
$("#form1").validate({
rules: {
Email: {
required: true,
email: true
},
ID: { required: true },
UserName: { maxlength: 5, required: true }
},
messages: {
Email: "*错的邮箱",
ID: "*必填",
UserName: "*"
}
});
});
</script>
4:Ajax应用
(1):基本应用
<script src="../../Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$("#btnGetDate").click(function () {
$.ajax({
url: "/Home/GetDateNow",
type: "POST",
data: "",
success: function (data) {
$("#result").text(data);
}
});
//$.post("", "", function () { });
});
});
</script>
5:MVC实现分页
(1):基本分页完成
from c in db.UserInfo
.OrderBy(c => c.Uid)
.Skip<UserInfo>((pageIndex - 1) * pageSize)//跳过的页数
.Take<UserInfo>(pageSize)//取多少条
.ToList()
select c;
(2):使用HtmlHelper封装分页
MVC自我学习
最新推荐文章于 2024-08-30 22:22:47 发布