在控制器中,我如何从http帖子获取数据? [英] In the contoller, how do i obtain data from an http post?

查看:136
本文介绍了在控制器中,我如何从http帖子获取数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

如何从表单中获取数据=“ post”?如何在我的控制器中请求数据?

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

I want to simply obtain data from a form.. Below is my form.. In my controller , how do i access the data from the form?

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

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

Contoller Controller Controller Controller Controller

[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");
}


推荐答案

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

Just make sure the parameters of the action has the same name as the input fields and the data-binding will take care of the rest for you.

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

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

如果您的模型的属性与输入字段的名称相同甚至可以这样做:

If you have a model whose properties have the same name as your input fields your could even do this:

[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 读取数据:

It is of course possible to read the data through Request.Form as well:

var inputData = Request.Form["inputFieldName"];

这篇关于在控制器中,我如何从http帖子获取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆