Asp.net 如何在create方法中处理隐藏值

Asp.net 如何在create方法中处理隐藏值,asp.net,asp.net-mvc,forms,hidden-field,Asp.net,Asp.net Mvc,Forms,Hidden Field,我在asp.net中使用此方法处理“创建”表单: public ActionResult Create([Bind(Include = "question_id, feedback_id")] feedback_questions feedback_questions) 在表单question\u id中,我从选择框中获取,但是feedback\u id我希望通过隐藏值获取 我的表格: @using (Html.BeginForm()) { @Html.AntiForgeryToken

我在asp.net中使用此方法处理“创建”表单:

public ActionResult Create([Bind(Include = "question_id, feedback_id")] feedback_questions feedback_questions)
在表单
question\u id
中,我从选择框中获取,但是
feedback\u 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>
}
@使用(Html.BeginForm()){
@Html.AntiForgeryToken()
反馈和问题

@Html.ValidationSummary(true,“,new{@class=“text danger”}) @LabelFor(model=>model.question_id,“选择问题:”,htmlAttributes:new{@class=“controllabel col-md-2”}) @DropDownList(“question_id”,null,htmlAttributes:new{@class=“form control”}) @Html.ValidationMessageFor(model=>model.question_id,“,new{@class=“text danger”}) @LabelFor(model=>model.feedback_id,”,htmlAttributes:new{@class=“controllabel col-md-2”}) //在这里,我想要反馈\u id的隐藏属性,它的值为@ViewBag.feedback\u id @Html.ValidationMessageFor(model=>model.question_id,“,new{@class=“text danger”}) }

如何操作?

您可以使用
Html.Hidden(“feedback\u id”、@ViewBag.feedback\u id)
其中
feedback\u id
是隐藏输入的名称,应该与您绑定到的属性的名称匹配。

可能
feedback\u id
正在从另一个视图或布局传递到视图,但是既然它在您的模型上,为什么不将它传递到Get操作上的Create视图中呢

public ActionResult Create()
{
    return View(new feedback_questions() { feedback_id = ?? });
}

[HttpPost]
public ActionResult Create([Bind(Include = "question_id, feedback_id")] feedback_questions feedback_questions)
{ 
    return View(feedback_questions);      
}
然后您就可以使用HiddenFor帮助器了

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    @Html.HiddenFor(model => model.feedback_id)
    <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">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.HiddenFor(model=>model.feedback\u id)
反馈和问题

@Html.ValidationSummary(true,“,new{@class=“text danger”}) @LabelFor(model=>model.question_id,“选择问题:”,htmlAttributes:new{@class=“controllabel col-md-2”}) @DropDownList(“question_id”,null,htmlAttributes:new{@class=“form control”}) @Html.ValidationMessageFor(model=>model.question_id,“,new{@class=“text danger”}) }
如果属性在模型上,为什么要使用viewbag?在将模型传递到视图(不在
viewbag
中)之前,只需在控制器中设置
反馈id
的值,并在视图中使用
@Html.HiddenFor(m=>m.feedback\u id)
。请注意,为隐藏输入添加标签也是毫无意义的,因为生成
ValidationMessageFor()
(默认情况下,隐藏输入不会被验证)