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())
       { %>
       <input type="text" />
       <input type="submit" value="ok" />
    <% } %>




controller:


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




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


  <% Html.BeginForm();%>
       <input type="text" />
       <input type="submit" value="ok" />
    <% Html.EndForm(); %>






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


生成HTML:
 <input id="Name" name="Name" type="hidden" value="321455555555" />




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数据,自动完成绑定
 <div>
        <%:Html.DropDownList("stuname") %>
    </div>
    <p>
    <%: Html.ListBox("stuname") %>
    </p>






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


html:
 <input id="pwd" name="pwd" type="password" />






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


html:
 <input id="pwd" name="pwd" type="password" value="ewq" />






6.radio button
view:


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




html:


 <input id="radio" name="radio" type="radio" value="red" />


    <input checked="checked" id="radio" name="radio" type="radio" value="yellow" />






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:

<textarea class="aaa" cols="8" id="ta" name="ta" rows="10"> hello , world</textarea>



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") %>


 <div class="editor-field">
            <%: Html.TextBoxFor(model => model.stuNo) %>
            <%: Html.ValidationMessageFor(model => model.stuNo) %>
        </div>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值