简体   繁体   English

将对象传递给MVC中的控制器

[英]Passing an object to controller in mvc

I am in the middle of learning MVC and it's such a different way of thinking, I have taken a simple login form as an example but the one thing that confuses me is how I tie up the submit button to the Action and pass the object. 我正处于学习MVC的过程中,这是一种不同的思维方式,我以一个简单的登录表单为例,但令我困惑的一件事是如何将Submit按钮绑定到Action并传递对象。 Let me give you the example. 让我举个例子。

Here is the Model 这是模型

public class ExternalUser
    {

        [Display(Name = "User Name")]
        [MaxLength(50)]
        public string UserName { get; set; }
        [Display(Name = "Password")]

        public string Password { get; set; }
        [Display(Name = "Remember Me")]

        public bool RememberMe { get; set; }
        public bool IsValid()
        {
            return true;
        }
    }

Here is the View 这是视图

@model ExternalUser
@{
    ViewBag.Title = "Login";    
}
@using (Html.BeginForm())
{
    @Html.ValidationSummary(true, "Login failed. Check your login details.");
    <div>
        <fieldset>
            <legend>Login</legend>
            <div class="editor-label">
                @Html.LabelFor(u => u.UserName)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(u => u.UserName)
                @Html.ValidationMessageFor(u => u.UserName)
            </div>
            <div class="editor-label">
                @Html.LabelFor(u => u.Password)
            </div>
            <div class="editor-field">
                @Html.PasswordFor(u => u.Password)
                @Html.ValidationMessageFor(u => u.Password)
            </div>
            <div class="editor-label">
                @Html.CheckBoxFor(u => u.RememberMe)
                @Html.LabelFor(u => u.RememberMe)
            </div>
            <input type="submit" value="Log In" />
        </fieldset>
    </div>
}

Here is the Controller 这是控制器

 public class UserController : Controller
    {
        // GET: /User/
        public ActionResult Index()
        {
            return View();
        }

        [HttpGet]
        public ActionResult Login()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Login(ExternalUser user)
        {
            if (ModelState.IsValid)
            {
                if (user.IsValid())
                {
                    FormsAuthentication.SetAuthCookie(user.UserName, user.RememberMe);
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    ModelState.AddModelError("", "Login data is incorrect!");
                }
            }
            return View(user);
        }
        public ActionResult Logout()
        {
            FormsAuthentication.SignOut();
            return RedirectToAction("Index", "Home");
        }
    }

When I click the Login button, it's going to the first overload of public ActionResult Login() and not the Second overload of public ActionResult Login(ExternalUser user) which is what I would expect, can you tell me what I'm missing as to why it's not hitting the correct method? 当我单击“登录”按钮时,它将转到第一个public ActionResult Login()的重载,而不是第二个public ActionResult Login(ExternalUser user)的第二个重载,这是我所期望的,您能告诉我我所缺少的吗为什么没有找到正确的方法?

Thanks 谢谢

Can't think of a reason why it would not work. 想不到为什么它不起作用的原因。 But, none the less check the HTML generated in the browser using inspect element option. 但是,尽管如此,仍要使用inspect element选项inspect element在浏览器中生成的HTML。 If the <form> tag has action attribute value set to 'GET' or if the action attribute is missing altogether, try explicitly specifying it as post. 如果<form>标记的操作属性值设置为'GET'或完全缺少操作属性,请尝试将其明确指定为post。

Html.BeginForm("Login", "User", FormMethod.Post)

I realised that I had put in an extra <form> tag in the /Shared/SiteLayout.cshtml, therefore causing two <forms> to placed on the page, after removing the one in the shared folder it now fires the right method! 我意识到我已经在/Shared/SiteLayout.cshtml中添加了一个额外的<form>标记,因此导致在页面上放置了两个<forms> ,在删除共享文件夹中的一个后,它现在会触发正确的方法! I hope this helps someone in the future. 我希望这对以后的人有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM