【问题标题】:MVC 4 submit specific form valueMVC 4 提交特定的表单值
【发布时间】:2013-03-26 14:28:31
【问题描述】:

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

我有以下情况。

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

我想做的就是这个。 如果第一个下拉列表正在执行回发,我只想发布第一个列表的选定值,并且 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>
}

提前致谢

【问题讨论】:

    标签: jquery asp.net-mvc-4


    【解决方案1】:

    在正常发布情况下,您不能提交特定值。将提交所有表单字段。没有办法阻止这种情况。

    但是,您可以做以下三件事之一:

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

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

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

    【讨论】:

    • 我在想,设置两个表单并每次提交整个表单。一个包含第一个下拉列表的表单和另一个包含第二个下拉列表的表单。
    【解决方案2】:

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

    >

    【讨论】:

    • 请解释您的代码。只是不加解释地发布它并不能完全回答问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-22
    • 2012-05-10
    • 2015-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多