ASP.NET MVC 3.0 Razor 学习笔记之二

学习ASP.NET MVC 3.0 Razor  笔记,Get   Post  提交数据,以及3.0 自带的ajax 验证 ,个人觉得自带验证还是达不到要求。

首先根据业务规则创建Model ,在Model问价夹中点击右键添加新的cs 文件

 public class News
    {
        [Required]
        [Display(Name = "公告名称")]
        public string NewsName { get; set; }

        [Required]
        [Display(Name = "创建时间")]
        public DateTime CreateDate { get; set; }

        [Required]
        [Display(Name = "公告类型")]
        public string NewsType { get; set; }

        [Required]
        [Display(Name = "公告类容")]
        public string Content { get; set; }
    }

添加 View 页面

下面是选择了Create 后自动创建出来的页面。

@model RazorApplication.Models.News

<script src="@Url.Content("~/Scripts/jquery-1.4.4.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>News</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.NewsName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.NewsName)
            @Html.ValidationMessageFor(model => model.NewsName)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.CreateDate)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.CreateDate)
            @Html.ValidationMessageFor(model => model.CreateDate)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.NewsType)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.NewsType)
            @Html.ValidationMessageFor(model => model.NewsType)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Content)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Content)
            @Html.ValidationMessageFor(model => model.Content)
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}



然后我们就要到Controller 里面添加Get  和 Post

    public class AccountController : Controller
    {



        // GET
        public ActionResult News()
        {
            return View();
        }

        //POST
        [HttpPost]
        public ActionResult News(News news)
        {
            return RedirectToAction("LogOn", "Account");


        }
    }

完成一个简单的页面。

posted @ 2011-02-24 15:31  jackyong  阅读(1092)  评论(1编辑  收藏  举报