将模型从视图传递到不同的控制器

时间:2015-07-29 12:22:26

标签: asp.net asp.net-mvc asp.net-mvc-4 razor

我有这个按钮:

<button type="button" onclick="location.href='@Url.Action("Index", "Registration">Register</button>

它在登录页面上,该链接指向注册页面。当用户点击链接转到注册页面时,我想保留在此页面上输入的用户名和密码,那么有没有办法将新模型传递给用户,以便用户进入注册页面并且已经有他们输入了他们的ID和密码?

例如,我正在尝试做这样的事情:

<button type="button" onclick="location.href='@Url.Action("Index", "Registration", new RegistrationModel { ID = Model.ID, Password = Model.Password })'">Register</button>

1 个答案:

答案 0 :(得分:3)

您需要做的只是将您的值发布到您想要处理它的控制器并共享相同的文本框名称。

假设您有这样的视图,其中包含您的表单:

@model LoginFormViewModel

@* Form element defaults to posting to Login controller, but has an attribute containing the URL to the Registration controller should we need it *@
<form id="login_form" method="post" action="@Url.Action("Index", "Login")" data-registration-url="@Url.Action("Index", "Registration")">

    @* Username field *@
    @Html.TextBoxFor(x => x.Username)

    @* Password field *@
    @Html.PasswordFor(x => x.Password, new { value = Model.Password })

    @* Hidden value that we can check in the controller *@
    <input type="hidden" name="FromLoginPage" value="true" />

    <input type="submit" value="Register" id="register_submit" />
    <input type="submit" value="Login" />
</form>

我会使用jQuery来控制表单的提交:

// User clicked regstration button, not login button
$(document).delegate('input#register_submit', 'click', function(e) {

    // Prevent default submitting of the form to the login controller
    e.preventDefault();

    // Get registration URL from the attribute
    var registerActionUrl = $('form#login_form').attr('data-registration-url');

    // Submit the form to the registration controller
    $('form#login_form').attr('action', registerActionUrl).submit();
});

以下是共享具有相同名称(LoginViewModelRegistrationViewModel)的属性的模型UsernamePassword,这对于绑定非常有用取决于我们将表单提交给哪个控制器:

// Login model
public LoginViewModel
{
    public string Username { get; set; }
    public string Password { get; set; }
}

// Registration view model    
public RegistrationViewModel
{
    public string Username { get; set; }
    public string Password { get; set; }
    public bool FromLoginPage { get; set; }

    // ... other fields that won't get bound when submitted from login page
    public string ConfirmPassword { get; set; }
}

在Login控制器中,我们正常处理事情,但在注册表单中,我们可以快速检查从登录页面提交的FromLoginPage,以便将注册页面返回给填充了字段的用户:

// Login controller
[HttpPost]
public LoginController : Controller
{
    public Index(LoginViewModel requestmodel)
    {
        // Validate login
        // Process login

        // Return the view
        return View(requestmodel);
    }
}

// Registration controller
[HttpPost]
public RegistrationController : Controller
{
    public Index(RegistrationViewModel requestModel)
    {
        // Submitted from the login page?
        if (requestModel.FromLoginPage)
        {
            // Clear any model validation errors so far
            this.ModelState.Clear();

            // Just load the registration page with the values populated
           return View(requestmodel);
        }

        // A normal registration request from registration page, perform validation on entire model

        // Process login here
        return View(requestmodel);
    }
}