Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 3 ActionMethod参数每次为0_Asp.net Mvc 3 - Fatal编程技术网

Asp.net mvc 3 ActionMethod参数每次为0

Asp.net mvc 3 ActionMethod参数每次为0,asp.net-mvc-3,Asp.net Mvc 3,我有一个表单(这里是它的一些代码): 我的相关控制器的更新方法有: [HttpPost] [ValidateInput(false)] public ActionResult Update(FormCollection col, int id = 0) { ... rest of code } 提交表单时,我会不断得到一个id 0,而不是我从模型中看到的id,当我调试时,该id显然在表单呈现期间使用。就在我提交时,它没有正确传递i

我有一个表单(这里是它的一些代码):

我的相关控制器的更新方法有:

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Update(FormCollection col, int id = 0)
        {
... rest of code
}
提交表单时,我会不断得到一个id 0,而不是我从模型中看到的id,当我调试时,该id显然在表单呈现期间使用。就在我提交时,它没有正确传递id


我在语法方面做错了什么吗?Html.BeginForm中的新{}我想我不明白当表单提交时,新的匿名类型是如何与此id匹配的,当它是一个id集合和一个类时,如您所见。

当您执行
新{id=Model.discussionId…}
时,您正在设置表单元素的id属性。要使id正确显示,您必须将其作为表单的输入。因此,在表单中,您可以放置一个隐藏的输入元素,如下所示:
@Html.hidden(“id”,Model.discussionId)

因此,您当前的表单呈现如下内容:

<form id="theDiscussionId" class="update" action="/Discussion/Update">
    ... rest of the view code
</form>
<form class="update" action="/Discussion/Update">
    <input type="hidden" name="id" value="theDiscussionId" />
    ... rest of the view code
</form>

... 视图代码的其余部分
你需要这样:

<form id="theDiscussionId" class="update" action="/Discussion/Update">
    ... rest of the view code
</form>
<form class="update" action="/Discussion/Update">
    <input type="hidden" name="id" value="theDiscussionId" />
    ... rest of the view code
</form>

... 视图代码的其余部分