asp.net mvc view 使用小结2

1.actionLink:
一般使用:
 <%: Html.ActionLink("Edit", "Edit", new { id = Model.StuId })%>


跨controller调用:
<%: Html.ActionLink("invokeTest","method1","Test",new {id1="11",id2="22",id3="33"}) %>




controller:
 
[AsyncTimeout(6000)]
        public void method1Async(string id1, string id2, string id3, string id4 = "4")
        {
            Thread.Sleep(3000);
            
        }
        
        
        public void method1Completed(string id1,string id2,string id3,string id4 = "4") {
            Response.Write("id1: " + id1 + " id2 : " + id2 + " id3 :" + id3 + "id4: " + id4);
        }






2.form 标签:


view:


 <% using (Html.BeginForm())
       { %>
       
       
    <% } %>




controller:


[HttpPost]
public ActionResult Details(string a) {
            string aa = a;
            return View();
        }




view的代码可读性不够强,也可写成:


  <% Html.BeginForm();%>
       
       
    <% Html.EndForm(); %>






3.hidden的使用:
view代码:
 <%: Html.HiddenFor(m => m.Name) %>


生成HTML:
 




4.dropdown和listbox:


controller使用linq直接赋值:


var q = svm.StudentList;


            var ids = (from q1 in q select new SelectListItem() { Text = q1.name });
            ViewData["stuname"] = ids;




view直接使用viewdata数据,自动完成绑定
 
<%:Html.DropDownList("stuname") %>

<%: Html.ListBox("stuname") %>







5.Password:
view:
  <%: Html.Password("pwd") %>


html:
 






view:
 <%: Html.Password("pwd",Model.Name) %>


html:
 






6.radio button
view:


 <%: Html.RadioButton("radio", "red", false) %>
    <%: Html.RadioButton("radio", "yellow", true) %>




html:


 


    






7.partial view


view:


<% Html.RenderPartial("ajaxTest"); %>



 <% Html.RenderPartial("ajaxTest",Model); %>



8.textarea:


view:
 <%: Html.TextArea("ta","hello , world",10,8,new {@class="aaa"}) %>




html:



9.validation


常用验证规则:


Required:必须输入,不能为空
StringLength:字符串的长度不能大于设置的长度
Range:数字的可输入范围
RegularExpression:正则表达式匹配


model(EF自动生成):

Required 和 StringLength 为限制条件

 [Required(ErrorMessage="stu no is not null")]
  [StringLength(10,ErrorMessage="length is invalid")]
        public global::System.String stuNo
        {
            get
            {
                return _stuNo;
            }
            set
            {
                if (_stuNo != value)
                {
                    OnstuNoChanging(value);
                    ReportPropertyChanging("stuNo");
                    _stuNo = StructuralObject.SetValidValue(value, false);
                    ReportPropertyChanged("stuNo");
                    OnstuNoChanged();
                }
            }
        }



create action:


 if (ModelState.IsValid)
            {
                svm.AddStudent(s);
                return Index();
            }
            else
            {
                return View("Create");
            }



view:
 <%: Html.ValidationSummary("something is wrong") %>


 
<%: Html.TextBoxFor(model => model.stuNo) %> <%: Html.ValidationMessageFor(model => model.stuNo) %>


你可能感兴趣的:(ASP.NET,MVC)