0

可能重复:
如何使用method=“post”从表单中获取数据?如何在我的控制器中请求它的数据?

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

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

控制器 控制器 控制器 控制器 控制器

[HTTP POST]
public  Action Result(Could i pass in the name through here or..)
{

Can obtain the data from the html over here using Request.Form. Pleas help
return RedirectToAction("SearchPerson", "Person");
}
4

2 回答 2

4

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

[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]

当然也可以通过以下方式读取数据Request.Form

var inputData = Request.Form["inputFieldName"];
于 2012-07-18T21:56:35.827 回答
3

你可以做

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

    return RedirectToAction("SearchPerson", "Person");
}
于 2012-07-18T22:20:00.283 回答