MVC 4提交具体的表格价值

时间:2013-03-26 14:28:31

标签: jquery asp.net-mvc-4

我想知道如何使用razor从MVC 4应用程序提交特定的表单值。

我有以下感觉。

2下拉列表。第一个是在控制器中使用模型数据填充,列出一组类型(第一次自动选择默认值)。第二个是由类型对象填充的(如果回发来自第一个下拉列表,则自动选择第一个)。

我想做的就是这个。 如果第一个下拉列表正在进行回发,我只希望发布第一个列表的选定值,并且dropDownList2为0。

如果第二个下拉列表导致回发,我想要两个值。

public ActionResult Index(int dropDownList1 = 0, int dropDownList2 = 0)
{
   // Get data for the dropdown lists
}

这是视图文件

<p>
@Html.ActionLink("Create New", "Create")
@using (Html.BeginForm("Index","EditItems",FormMethod.Get)){     
     <p>Γονείς τύπων: @Html.DropDownList("dropDownList1") </p> 
     <p>Τύποι σχολείων: @Html.DropDownList("dropDownList2")  </p> 
    } 
</p>

@section Scripts {
    <script type='text/javascript'\>
        $("#parentUnit").change(function () {
            $(this).parents('form').submit();
        });

        $('#childUnit').change(function () {
            $(this).parents('form').submit();
        });
    </script>
}

提前致谢

2 个答案:

答案 0 :(得分:1)

在正常的帖子情况下,您无法提交特定值。将提交所有表单字段。没有办法阻止这种情况。

然而,你可以做的是三件事之一:

1)您可以使用javascript更改各个字段以清除您不想发送的字段,并在您要发送的字段中设置正确的值。

2)您可以使用Ajax发布,然后您可以控制发布的数据。

3)您可以使用多个隐藏表单,当用户点击提交时,您可以使用javascript仅将要提交的值复制到仅包含这些字段的表单,然后通过javascript提交该表单。

答案 1 :(得分:0)

<!DOCTYPE html>
<html>
<body>

<form action="demo_form.asp" method="get">
  First name: <input type="text" name="fname"><br>
  Last name: <input type="text" name="lname"><br>
  <button type="submit">Submit</button><br>
  <button type="submit" formaction="demo_admin.asp">Submit as admin</button>
</form>

<p><strong>Note:</strong> The formaction attribute of the button tag is not supported in Internet Explorer 9 and earlier versions.</p>

</body>
</html>

&GT;