2015-11-04 41 views
1

我有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> 
} 

我该怎么办呢?

+1

为什么你使用viewbag如果属性在你的模型上? – JamieD77

+0

在将模型传递给视图(而不是在ViewBag中)并在视图中使用@ Html.HiddenFor(m => m.feedback_id)之前,只需在控制器中设置'feedback_id'的值即可。注意,为隐藏输入添加标签也没有意义,正如生成一个ValidationMessageFor()(默认情况下未验证隐藏输入) –

回答

0

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

1

也许feedback_id被传递到从另一个视图或布局视图,但因为它是你的模型,为什么不把它传递到创建视图的获取动作

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> 
}