我为ASP.NET MVC 4网站创建了一个Controller,一个Model和一个View ... 但是,当我尝试显示名为GroupId的ViewBag时,它不在我看来!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AIO___Desafio_Interno_de_Inteligências.Models;
namespace AIO___Desafio_Interno_de_Inteligências.Controllers
{
public class LoginController : Controller
{
//
// GET: /Login/
public ActionResult Login()
{
var detail = new Details()
{
GroupId = 23,
GroupPassword = "280phcdd12",
};
return View();
ViewBag.GroupId = detail.GroupId;
ViewBag.GroupPassword = detail.GroupPassword;
}
}
}
而且,在我的查看代码中:
@{
ViewBag.Title = "Login";
}
<h2>Login</h2>
<p>@ViewBag.GroupId</p>
<p>@ViewBag.GroupPassword</p>
但是,我得到的是:
Just my default CSS,and these codes,as H1 and P... But the ViewBag.{...} is not there
[编辑]
我的模型页面(Details.cs):
namespace AIO___Desafio_Interno_de_Inteligências.Models
{
public class Details
{
public int GroupId { get; set; }
public string GroupPassword { get; set; }
}
}
答案 0 :(得分:1)
你的控制器方法应该是这样的
public ActionResult Login()
{
var detail = new Details()
{
GroupId = 23,
GroupPassword = "280phcdd12",
};
ViewBag.GroupId = detail.GroupId;
ViewBag.GroupPassword = detail.GroupPassword;
return View();
}
在返回视图之前设置ViewBag数据