zoukankan      html  css  js  c++  java
  • .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>
    
    
    
  • 相关阅读:
    小程序中点击input控件键盘弹出时placeholder文字上移
    微服务学习记录-consul服务发现
    微服务学习记录-ocelot网关
    一些新了解到技术
    warning: rpmts_HdrFromFdno: Header V3 RSA/SHA256 Signature, key ID fd431d51: NOKEY
    linux 7 创建DNS服务器
    ESXi 主机创建datastore失败
    检查MD5
    为戴尔服务器下载ESXi
    vCenter Server上的报警消除
  • 原文地址:https://www.cnblogs.com/jiqing9006/p/6895384.html
Copyright © 2011-2022 走看看