【问题标题】:how to process hidden values in create method如何在create方法中处理隐藏值
【发布时间】: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 =&gt; m.feedback_id)。请注意,为隐藏输入添加标签也是毫无意义的,生成 ValidationMessageFor() 也是如此(默认情况下不验证隐藏输入)

标签: asp.net asp.net-mvc forms hidden-field


【解决方案1】:

也许 feedback_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>
}

【讨论】:

    【解决方案2】:

    您可以使用Html.Hidden("feedback_id", @ViewBag.feedback_id),其中feedback_id 是隐藏输入的名称,它应该与您绑定到的属性的名称相匹配。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-07
      • 1970-01-01
      • 2018-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多