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 に答える