日期:2014-05-16  浏览次数:20740 次

新手关于MVC以及 get set的简单问题
本帖最后由 u013793136 于 2014-02-26 17:20:59 编辑
新手小白 求指导

练习写mvc框架的网站 试用ViewModel来显示系统当前时间 
为什么运行后只显示“Time”字样,而不显示时间呢

timModels.cs:
namespace MvcApplication2.Models
{
    public class timModels
    {
        String t = DateTime.Now.ToString();
        public string Time
        {   get{ return t; }    }
    }   
}


然后View这样写显示时间

tim.aspx:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MvcApplication2.Models.timModels>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Index
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

<h2>Index</h2>
         <div class="editor-field">
                    <%: Html.LabelFor(m => m.Time) %>
             </div> 
              
</asp:Content>


Controller则如下
timController.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcApplication2.Controllers
{
    public class timController : Controller
    {
        public ActionResult tim()
        { return View(); }
    }
}

为什么运行后只显示“Time”字样,而不显示时间呢

单纯要实现View显示时间的话 用<%: DateTime.Now.ToString() %>这就好了
但我还是不明白上面那样写为什么不行 我是模仿VS提供的登录系统写的
------解决方案--------------------
Time是属性名,LabelFor默认输出属性名。
用TextBoxFor默认输出属性值。