Asp.net Mvc 学习资料总结

Mvc学习
Html.DropDownList使用
public ActionResult AddPerson() {
IList<MvcAppTest.Models.DataDemoModels> person = Helpers.xmlHelper.GetXmlElement(Server.MapPath(@"~/Content/PersonInfo.xml"));
/******************** //第一种方法。 *******************/
IEnumerable<SelectListItem> items = person.Select(
c => new SelectListItem
{
Value = c.UserID.ToString(),
Text = c.UserName
});
ViewData["UserNames"] = items;
/********************* end *****************************/

/******************** //第二种方法。 *******************/

var query = person.Select(c => new { c.UserID, c.UserName });
ViewData["query"] = new SelectList(query.AsEnumerable(), "UserID", "UserName",4);

/******************* end ******************************/

/*******使用方法绑定性别(sex)列******************/
List<SelectListItem> list = new List<SelectListItem> { 
new SelectListItem{Text="男",Value="true"},
new SelectListItem{Text="女",Value="false"}
};
ViewData["Sex"] = list;
return View();
}

      
页面写法:
<div>
<%= Html.DropDownList("UserNames","第一种方法实现") %>
</div>
<div>
<%= Html.DropDownList("DemoName", (IEnumerable<SelectListItem>)ViewData["query"], new { onchange="javascript:getOption();"})%>
</div>
<%= Html.DropDownList("Sex") %>

     
MVC中Html.RadioButton的使用:
<%= Html.RadioButton("PSex", "1", new { @id = "man1", onclick = "changeRadio('man1','man2');" })%>男
<%= Html.RadioButton("PSex", "2", new { @id="man2",onclick="changeRadio('man2','man1');"}) %>女

<script type="text/javascript">
var t = function(id) { return document.getElementById(id)}
var changeRadio = function(id1, id2) {
if (t(id1).checked) {
t(id2).checked = false;
} else {
t(id2).checked = true;
}
}
</script>

     
在Models中处理验证。如下:
[DataType(DataType.PhoneNumber,ErrorMessage="输入手机号码!")]
[DisplayName("手机")]
[Description("存放手机号码信息")]
[RegularExpression(@"^1[3,5,6]{1}\d{9}$",ErrorMessage="输入的手机号码不合法!")]
public string PTel { get; set; }

[DataType(DataType.EmailAddress,ErrorMessage="输入正确的Email地址!")]
[DisplayName("Email地址")]
[Description("存放EMail地址信息")]
[RegularExpression(@"^\w+([\-+.]\w+)*@\w+([\-.]\w+)*\.\w+([\-.]\w+)*$", ErrorMessage = "请输入正确的Email地址!")]
public string PEmail { get; set; }

在View页面中,在<% using (Html.BeginForm()) {%>前
加上 <% Html.EnableClientValidation(); %> 就可以实现客户端验证。
posted @ 2010-07-13 23:41  lass_name  阅读(459)  评论(2)    收藏  举报
$().ready(function(){ //导航距离屏幕顶部距离 var _defautlTop = $("#navigator").offset().top - $(window).scrollTop(); //导航距离屏幕左侧距离 var _defautlLeft = $("#navigator").offset().left - $(window).scrollLeft(); //导航默认样式记录,还原初始样式时候需要 var _position = $("#navigator").css('position'); var _top = $("#navigator").css('top'); var _left = $("#navigator").css('left'); var _zIndex = $("#navigator").css('z-index'); //鼠标滚动事件 $(window).scroll(function(){ if($(this).scrollTop() > _defautlTop){ //IE6不认识position:fixed,单独用position:absolute模拟 if($.browser.msie && $.browser.version=="6.0"){ $("#top").css({'position':'absolute','top':eval(document.documentElement.scrollTop),'left':_defautlLeft,'z-index':99999}); //防止出现抖动 $("html,body").css({'background-image':'url(about:blank)','background-attachment':'fixed'}); }else{ $("#navigator").css({'position':'fixed','top':0,'left':_defautlLeft,'z-index':99999}); } }else{ $("#navigator").css({'position':_position,'top':_top,'left':_left,'z-index':_zIndex}); } }); });