【发布时间】:2015-09-29 15:25:05
【问题描述】:
我有一个使用数据脚手架生成的视图。该视图有一个文本字段:
创建视图:
<div class="form-group">
@Html.LabelFor(model => model.GroupId, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.GroupId, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.GroupId, "", new { @class = "text-danger" })
</div>
</div>
我想将一个值从控制器传递到此文本字段。我做了以下,这似乎不起作用。
控制器:
public ActionResult Create(int id)
{
ViewBag.GroupId = id;
Debug.WriteLine("DEBUG: "+id);
return View();
}
【问题讨论】:
-
您的
ViewBag不是您的model,您不能只是互换它们。如果您在视图中检查ViewBag.GroupId,您将获得价值。如果你需要一个模型,你应该将它传递到你的视图中。
标签: c# asp.net-mvc-4 model-view-controller view controller