HtmlHelper学习笔记

来源:互联网 发布:日本第五师团知乎 编辑:程序博客网 时间:2024/06/05 18:16

一、@Html.ActionLink()

@Html.ActionLink("这是显示出来的链接","Action方法","controller")

@Html.ActionLink("这是显示出来的链接","Action方法","controller",new { id = item.Id }) 带参数的连接

二、@Html.DropDownListFor()

 @Html.DropDownListFor(c => c.h_year, new SelectList(Basic.GetYearList(), "Value", "Text"))                                
生成一个 name= h_year的下拉框 
也可以这样从Action中传递列表

@Html.DropDownListFor(c => c.h_year, ViewBag.list, "Value", "Text"))
一般会遇到要求默认选择的需求

@Html.DropDownListFor(c => c.h_year, new SelectList(Basic.GetYearList(), "Value", "Text"),"-请选择-")    
或者传递一个对象,有默认值的对象

public ActionResult Index()        {            var header = new FertilizerHeader { h_year = DateTime.Now.Year };            return View(header);        }
那么前台的name = h_year的list的默认值就是2014了

三、@Html.AntiForgeryToken() 防黑

@using (Html.BeginForm("Text","Home",FormMethod.Post)){    @Html.AntiForgeryToken()    @:网站公告:<input type="text" name="Notice" id="Notice" /><input type="submit" value="Submit" />}

后台
[HttpPost]        [ValidateAntiForgeryToken]        public ActionResult Text()        {            ViewBag.Notice = Request.Form["Notice"].ToString();            return View();        }

防止跨站攻击


四、@Html.ValidationMessageFor(model => model.Name)

关于这个错误提示相关的组件,这点击打开链接 里大神说的很清楚,我就不造轮子了

MARK  这里出了问题,感觉没那么简单蒋金楠的详解看的头晕晕的


哈哈,我先挖个坑

0 0
原创粉丝点击