ASP.NET on Linux (2) Model to Views

来源:互联网 发布:美工是干什么的 编辑:程序博客网 时间:2024/05/16 07:38

最近在摸索在Linux上开发ASP.NET, 开发框架是MVC3

最终目标是搭建一个功能完善的网站,之前只在Myeclipse上做过MVC的网站,没有C#基础,一切都是摸索阶段,。

大牛的写的博客都太精深了,反而一些我要的小细节都没有

这次是关于表单的,因为MONO比较精简,所以有些东西基本靠手动了


如何将Models转入View

1.首先你得有个Model

using System;
using System.Linq;
//项目叫做razortest2 命名空间得加上
namespace razortest2.Models
{
    public class Employee
    {
        public int ID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public DateTime Birthday { get; set; }
    }
}

2.然后是Controller

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;

namespace razortest2.Controllers
{
    public class HomeController : Controller
    {
        [HttpGet]
        public ActionResult Index ()
        {
            //ViewData ["Message"] = "Welcome to ASP.NET!";


            var emp = new razortest2.Models.Employee();

            return View(emp);
        }
    }
}

3.最后是Views

命名空间得加上项目名称

@model razortest2.Models.Employee
<h2>Index</h2>

@using (Html.BeginForm()) {
    //@Html.LableFor(model => model.ID)
    @Html.LabelFor(model => model.FirstName)
    @Html.EditorFor(model => model.FirstName)
}

直接F5


0 0
原创粉丝点击