没有在formCollection中获取值

本文关键字:获取 formCollection | 更新日期: 2023-09-27 18:11:50

我使用的表格如下:

<form action="@Url.Action("Complete_Evaluation","PP")" method="POST" id="frmObservationScoring">
    <input type="hidden" id="hdnaccountid" value="@ViewBag.AccountID" />
    <input type="hidden" id="hdnObservationID" value="@ViewBag.ObservationID" />
    <button onclick="return CompleteObservation();" class="buttoncss">Complete</button>
</form>

在控制器页面上,我试图使用它作为:

public ActionResult Complete_Evaluation(FormCollection fc)
{
    int AccountID = Request.Form["hdnaccountid"];
    int ObservationID = fc.GetValues("hdnObservationID"); 
    return view();
}
AccountID = Request.Form["hdnaccountid"];

是我试图获取值和

的一种方法
ObservationID = fc.GetValues("hdnObservationID"); 

通过这两种方式,我得到空值。

我已经从我的代码中确定了隐藏字段包含正确的值。

那么我怎么能从表单收集的值??

没有在formCollection中获取值

您应该使用name,而不是使用id来标识表单中的字段,因为该属性用于标识表单中的字段:

<input type="hidden" name="hdnaccountid" value="@ViewBag.AccountID" />

你应该用name代替id。

<form action="@Url.Action("Complete_Evaluation","PP")" method="POST" id="frmObservationScoring">
<input type="hidden" name="hdnaccountid" value="@ViewBag.AccountID" />
<input type="hidden" name="hdnObservationID" value="@ViewBag.ObservationID" />
<button onclick="return CompleteObservation();" class="buttoncss">Complete</button>
</form>

hdnaccountid和hdnoobservationid应该是输入的名称