我有proccess这种方法“制造”的形式在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如果属性在你的模型上? – JamieD77
在将模型传递给视图(而不是在ViewBag中)并在视图中使用@ Html.HiddenFor(m => m.feedback_id)之前,只需在控制器中设置'feedback_id'的值即可。注意,为隐藏输入添加标签也没有意义,正如生成一个ValidationMessageFor()(默认情况下未验证隐藏输入) –