.Net 自动属性结合手动属性

Model

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

namespace LanguageFeatures.Models
{
    //public class Product
    //{
    //    private int productID;
    //    private string name;
    //    private string description;
    //    private decimal price;
    //    private string category;

    //    public int ProductID
    //    {
    //        get { return productID; }
    //        set { productID = value; }
    //    }

    //    public string Name
    //    {
    //        get { return name; }
    //        set { name = value; }
    //    }

    //    public string Description
    //    {
    //        get { return description; }
    //        set { description = value; }
    //    }

    //    public decimal Price
    //    {
    //        get { return price; }
    //        set { price = value; }
    //    }

    //    public string Category
    //    {
    //        get { return category; }
    //        set { category = value; }
    //    }
    //}

    // 自动属性
    //public class Product
    //{
    //    public int ProductID { get; set; }
    //    public string Name { get; set; }
    //    public string Description { get; set; }
    //    public decimal Price { get; set; }
    //    public string Category { set; get; }
    //}

    // 结合起来
    public class Product
    {
        private string name;
        public int ProductID { get; set; }

        public string Name
        {
            get
            {
                return ProductID + name;
            }
            set
            {
                name = value;
            }
        }

        public string Description { get; set; }
        public decimal Price { get; set; }
        public string Category { set; get; }
    }
}

Controller

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LanguageFeatures.Models; // 引入Models

namespace LanguageFeatures.Controllers
{
    public class HomeController : Controller
    {
        public string Index()
        {
            return "Navigate to a URL to show an example";
        }

        public ViewResult AutoProperty()
        {
            Product myProduct = new Product();
            myProduct.Name = "Kayak";

            string productName = myProduct.Name;

            return View("Result", (object)String.Format("Product name:{0}", productName));
        }

        public ViewResult CreateProduct()
        {
            Product myProduct = new Product();
            myProduct.ProductID = 100;
            myProduct.Name = "Kayak";
            myProduct.Description = "A boat for one person";
            myProduct.Price = 275M;
            myProduct.Category = "Watersports";
            return View("Result", (object)String.Format("Product name:{0}", myProduct.Name));
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }
}

View

@model String
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Result</title>
</head>
<body>
    <div>
        @Model
    </div>
</body>
</html>


posted @ 2017-05-23 18:17  TBHacker  阅读(264)  评论(0编辑  收藏  举报