【发布时间】:2016-02-02 19:25:56
【问题描述】:
我正在尝试创建一个 MVC 站点,但 ViewBag 出现了一些问题。
好像不能包含元素。
我正在使用 MVC 4。
这是我在控制器中的功能:
public ActionResult Create()
{
ViewBag.DiscID = new SelectList(entities.Disc, "ID", "ID");
ViewBag.StarID = new SelectList(entities.Star, "ID", "Name");
ViewBag.Num = 7;
return View();
}
这是我的创建视图:
@model Movies.Models.StarAndRole
@{
ViewBag.Title = "Create";
int num;
int.TryParse(ViewBag.Num, out num);
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>StarAndRole</h4>
<h3>num is = @num</h3>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.StarID, "Star", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("StarID", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.StarID, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.DiscID, "Disc Num", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("DiscID", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.DiscID, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
在我看来,我放入 ViewBag 中的所有内容都是 NULL。
我做错了什么?
提前谢谢你!
【问题讨论】:
-
你怎么知道它是NULL?
标签: asp.net-mvc asp.net-mvc-4 viewbag