部分更改属性

时间:2016-03-13 07:17:55

标签: asp.net-mvc asp.net-mvc-5 partial-views asp.net-mvc-partialview html.beginform

在我的ASP.NET MVC(5.2)项目中,我有一个名为register.cshtml的页面。它不包含任何形式或任何东西,只是简单的div。

在其中一个div中,我渲染了一个部分:

@Html.Partial("~/Views/Users/_x.cshtml")

_x.cshtml内,我有一个表单:

@using (Html.BeginForm("/users/x"))
{
   ...
}

当我进入我的注册页面时,我希望我的表单呈现为:

<form action="/users/x" method="post"> ... </form>

但相反,我得到了这个:

<form action="/users/register?Length=23" method="post" novalidate="novalidate"> ... </form>

什么是length=23,为什么添加了novalidate属性,为什么会发布错误的路径?

为什么我的表单无法正常呈现?

3 个答案:

答案 0 :(得分:1)

如果您想要发布到x中名为usersController的方法,则需要

@using (Html.BeginForm("x", "users"))
{
    ....
}

请注意,您当前正在使用object routeValues接受string的{​​{3}},并且因为其Length,该方法为string生成了路由值,因为它是只有/users/register的属性($(allQuestions).each(function( index ) { // Loop your entirely div + input + label and use the index to determine the question /* <div> <input type="radio" name="option" value="0" id="option0" checked> <label for='option0'></label> <br/> </div> */ // Increment current question or use index }); 是因为生成主视图的方法)

答案 1 :(得分:1)

从您的代码中

Html.BeginForm("/users/x")

我理解用户你的控制器和x是一种方法。所以你可以这样做 -

@using (Html.BeginForm("x", "users", FormMethod.Post, new { id = "YourFormID"}))
{
}

答案 2 :(得分:1)

@using (Html.BeginForm("action", "controller",new { QueryString = 1}, FormMethod.Post, null))
{

}

注意:由于在beginform构造函数中传递了错误的参数。

并在你的视图中

 @Html.Partial("~/Views/Shared/_x.cshtml")
相关问题