访问c#中的类成员?

时间:2013-07-03 23:14:30

标签: c# razor

我使用MvcScaffolding为我创建CRUD文件。我有这个cs课:

    public class addresses
{
    /* Some code */
    public DateTime date_created { get; set; } /* I need to access this */
    public DateTime date_modified { get; set; } /* And this */
}

我需要访问date_created变量。我想在为我创建的控制器(c#)或创建的_CreateOrEdit.cshtml文件中访问它。提前谢谢。

编辑

我想在这里访问它,例如

    public class addressesController : Controller
{
    private SoccerContext context = new SoccerContext();

    //
    // GET: /addresses/Create

    public ActionResult Create()
    {

我想在此处访问它以将其设置为当前时间

        ViewBag.PossibleUsers = context.users;
        ViewBag.PossibleContacts = context.contacts;
        return View();
    } 

    //
    // POST: /addresses/Create

    [HttpPost]
    public ActionResult Create(addresses addresses)
    {
        if (ModelState.IsValid)
        {
            context.addresses.Add(addresses);
            context.SaveChanges();
            return RedirectToAction("Index");  
        }

        ViewBag.PossibleUsers = context.users;
        ViewBag.PossibleContacts = context.contacts;
        return View(addresses);
    }
    //More code............

1 个答案:

答案 0 :(得分:1)

你可以这样访问:

var addr = new addresses();
var created = addr.date_created;
//same for date_modofied