我有一个使用数据脚手架生成的视图。该视图有一个文本字段:
创建视图:
<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
储藏GroupId
正如您目前的情况,但由于您的视图正在使用模型,因此最好采用这种方式。
所以你会:
Model:
public class GroupModel
{
public int GroupId { get; set; }
}
控制器:
public ActionResult Create(int id)
{
var model = new GroupModel();
model.GroupId = id
return View(model);
}
在视图的顶部,您将引用您的模型:
@model GroupModel
然后,您可以使用以下代码访问视图中的模型model.GroupId
正如脚手架所做的那样。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)