如何在控制器接收HttpPost方法的参数? [英] How does the controller receive parameters on HttpPost methods?

查看:293
本文介绍了如何在控制器接收HttpPost方法的参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

把这个片段了控制器,例如:

Take this snippet out of a controller, for example:

public ActionResult Login()
{
    if (User.Identity.IsAuthenticated)
    {
        return RedirectToAction("Index", "Home");
    }
    else
    {
        return View();
    }
}

//
// POST: /User/Login

[HttpPost]
public ActionResult Login(LoginModel lm, string returnUrl)
{
    if (ModelState.IsValid)
    {
        if (Membership.ValidateUser(lm.UserName, lm.Password))
        {
            FormsAuthentication.SetAuthCookie(lm.UserName, lm.RememberMe);
            if (Url.IsLocalUrl(returnUrl) &&
                returnUrl.Length > 0 &&
                returnUrl.StartsWith("/") &&
                !returnUrl.StartsWith("//") &&
                !returnUrl.StartsWith("/\\"))
            {
                return Redirect(returnUrl);
            }
            else
            {
                return RedirectToAction("Index", "Home");
            }
        }
        else
        {
            ModelState.AddModelError("", "Username and Password combination is incorrect");
        }
    }

    return View();
}

我的问题是,用的重载方法登录()(对于HTTP POST),用的第一个参数为 LoginModel 类,并以此为字符串中的第二个参数?我Login.cshtml视图使用一个提交按钮,所以我很好奇,这些参数是如何被传递到登录()的方法?什么是从添加第三个参数阻止我?而如何将这个参数获得通过?

My question is, with the overloaded method of Login() (for the HTTP POST), what calls this method with the first parameter as a LoginModel class, and the second parameter as a string? My Login.cshtml view uses a submit button, so I am curious as to how these parameters get passed to the Login() method? What's to stop me from adding a third parameter? And how would that parameter get passed??

我知道这是一个基本的问题,我只是想了解视图和控制器之间的连接件。

I know this is a basic question, I'm just trying to understanding the connecting piece between Views and Controllers.

推荐答案

这过程被称为模型绑定,许多对IT资源的...我开始与如何实现自定义之一,因为从你就明白如何以及为什么过程的工作。

This process is called Model Binding, lots of resources on it...I would start with How to implement a custom one because from that you will then understand how and why the process works.

<一个href=\"http://buildstarted.com/2010/09/12/custom-model-binders-in-mvc-3-with-imodelbinder/\">http://buildstarted.com/2010/09/12/custom-model-binders-in-mvc-3-with-imodelbinder/
http://bradwilson.typepad.com/blog/2010/10/service-location-pt9-model-binders.html

http://buildstarted.com/2010/09/12/custom-model-binders-in-mvc-3-with-imodelbinder/ http://bradwilson.typepad.com/blog/2010/10/service-location-pt9-model-binders.html

编辑:

有是存在这需要项目集合后像表单数据和URL参数,并试图匹配起来与你告诉控制器期望项的默认模型粘合剂。它的工作原理是这样的...

There is a default model binder which exists which takes items in the post collection like form data and url parameters and tries to match them up with items which you have told the controller to expect. It works something like this...

请求时,
MVC决定哪个控制器应获得请求
MVC则着眼于HTTP方法,它是一个GET或POST?
如果这是一个职位,然后它看起来与HttpPost属性控制器动作
MVC然后看着你所定义的参数...

Request comes in, MVC decides which controller should get the request MVC then looks at the HTTP Method, was it a GET or POST? If it was a post then it looks for the controller action with the HttpPost attribute MVC then looks at the parameters you have defined...

在你的情况下,它说我有一个LoginModel和RETURNURL;
MVC现在是幸福的 - 它知道事情会那么现在看起来你发送的是什么?
它着眼于您的文章数据(表单字段和查询字符串参数)
一个个看起来在他们的名字,然后试图找到一个匹配你的控制器参数。

In your case it says I have a LoginModel and a returnUrl; MVC now is happy - it knows where things are going so now it looks at what you sent... It looks at your post data (the form fields and query string parameters) One by one it looks at their names and then tries to find a match in your controller parameters.

它找到的形式数据的用户名字段,这需要看你的参数,并询问是否有一个用户名?不,那也许是因为用户名是LoginModel的属性...那么它看起来通过LoginModel。啊哈它说,找到了一个。 MVC然后创建LoginModel的实例,并设置用户名属性为您发布的值。

It finds a Username field in the form data, it takes a look at your parameters and asks if there is a Username? No, well maybe Username is a property of LoginModel...it then looks through LoginModel. Aha it says, found one. MVC then creates an instance of LoginModel and sets the Username property to the value you posted.

这过程继续为每个找到你的形式和查询字符串的事。有一堆它遵循的规则,但你得到的图片 - 这就是匹配的MVC美约定优于配置的一部分。

This process continues for each of the things it finds in your form and query string. There are a bunch of rules it follows but you get the picture - this matching is part of the 'convention over configuration' beauty of MVC.

上面这整个过程模型绑定和自MVC实现了这个默认的行为,你看不到它在Global.asax或任何东西写出来,这是MVC的只是一部分。

This whole process above it model binding and since MVC implements this default behavior you do not see it written out in the Global.asax or anything, it is just part of MVC.

这篇关于如何在控制器接收HttpPost方法的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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