Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/17.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
C# 在控制器中,如何从http post获取数据?_C#_Asp.net Mvc_Model View Controller_Http Post - Fatal编程技术网

C# 在控制器中,如何从http post获取数据?

C# 在控制器中,如何从http post获取数据?,c#,asp.net-mvc,model-view-controller,http-post,C#,Asp.net Mvc,Model View Controller,Http Post,可能重复: 我只想从表单中获取数据。。下面是我的表格。。在我的控制器中,如何访问表单中的数据 <script type="text/javascript"> $(document).ready(function () { $("#SavePersonButton").click(function () { $("#addPerson").submit(); }); }); </script> <

可能重复:

我只想从表单中获取数据。。下面是我的表格。。在我的控制器中,如何访问表单中的数据

<script type="text/javascript">
    $(document).ready(function () {
        $("#SavePersonButton").click(function () {
            $("#addPerson").submit();
        });
    });
</script>
<h2>Add Person</h2>
<form id="addPerson" method="post" action="<%: Url.Action("SavePerson","Prod") %>">
    <table>
        <tr>
            <td colspan="3" class="tableHeader">New Person</td>
        </tr>
         <tr>
            <td colspan="2" class="label">First Name:</td>
            <td class="content">
                <input type="text" maxlength="20" name="FirstName" id="FirstName" />
            </td>
        </tr>
         <tr>
            <td colspan="2" class="label">Last Name:</td>
            <td class="content">
                <input type="text" maxlength="20" name="LastName" id="LastName" />
            </td>
        </tr>

        <tr>
            <td colspan="3" class="tableFooter">
                    <br />
                    <a id ="SavePersonButton" href="#" class="regularButton">Add</a>
                    <a href="javascript:history.back()" class="regularButton">Cancel</a>
            </td>
        </tr>
    </table>
</form>

只需确保操作的参数与输入字段具有相同的名称,并且数据绑定将为您处理其余的部分

[HttpPost]
public ActionResult YourAction(string inputfieldName1, int inputFieldName2 ...)
{
    // You can now access the form data through the parameters

    return RedirectToAction("SearchPerson", "Person");
}
如果模型的属性与输入字段同名,您甚至可以执行以下操作:

[HttpPost]
public ActionResult YourAction(YourOwnModel model)
{
    // You will now get a model of type YourOwnModel, 
    // with properties based on the form data

    return RedirectToAction("SearchPerson", "Person");
}
请注意,属性应该是
[HttpPost]
而不是
[HTTP POST]

当然可以通过
请求读取数据。表单也可以:

var inputData = Request.Form["inputFieldName"];
你可以

[HttpPost]
public ActionResult YourAction (FormCollection form)
{
    var inputOne = form["FirstName"];

    return RedirectToAction("SearchPerson", "Person");
}

您已经发布了相同的问题twice@gilly3它在几分钟内意外地由同一OP引发ago@HatSoft-当有人投票以重复方式结束问题时,会自动生成该评论。你是说这不是一个重复的问题吗?@gilly3-hmm我不知道那是干杯
[HttpPost]
public ActionResult YourAction (FormCollection form)
{
    var inputOne = form["FirstName"];

    return RedirectToAction("SearchPerson", "Person");
}