【发布时间】:2015-11-04 16:11:21
【问题描述】:
我在asp.net中有这个方法用于处理“创建”表单:
public ActionResult Create([Bind(Include = "question_id, feedback_id")] feedback_questions feedback_questions)
在question_id 表单中,我从选择框中获取,但feedback_id 我想通过隐藏值获取。
我的表格:
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>feedback_questions</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.question_id, "choose question: ", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("question_id", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.question_id, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.feedback_id, "", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
// here I want hidden attribute for feedback_id, which will have value @ViewBag.feedback_id
@Html.ValidationMessageFor(model => model.question_id, "", 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>
}
我该怎么做?
【问题讨论】:
-
如果属性在您的模型上,您为什么要使用 viewbag?
-
只需在控制器中设置
feedback_id的值,然后将模型传递给视图(不在ViewBag中)并在视图中使用@Html.HiddenFor(m => m.feedback_id)。请注意,为隐藏输入添加标签也是毫无意义的,生成ValidationMessageFor()也是如此(默认情况下不验证隐藏输入)
标签: asp.net asp.net-mvc forms hidden-field