-1

I know similar question already asked, but my question related to saving state of the page. I use same model for simplicity. Assume I have following model:

public class LoginRegisterViewModel   
{
    public string LoginUsername { get; set; }
    public string LoginPassword { get; set; }

    public string RegisterUsername { get; set; }
    public string RegisterPassword { get; set; }
    public string RegisterFirstName { get; set; }
    public string RegisterLastName { get; set; }
}

I am interested in 2 things:

1- I must know wich button has been clicked

2- I must preserve both form data. It means when user already filled LoginUsername for Login and then decide to register for sites and clicked Register button, in response both form state will be saved.

Using following approach:

@model LoginRegisterViewModel

@using (Html.BeginForm("Login", "MemeberController", FormMethod.Post, new {})) {

    @Html.LabelFor(m => m.LoginUsername)
    @Html.TextBoxFor(m => m.LoginUsername)

    @Html.LabelFor(m => m.LoginPassword)
    @Html.TextBoxFor(m => m.LoginPassword)

    <input type='Submit' value='Login' />

}

@using (Html.BeginForm("Register", "MemeberController", FormMethod.Post, new {})) {

    @Html.LabelFor(m => m.RegisterFirstName)
    @Html.TextBoxFor(m => m.RegisterFirstName)

    @Html.LabelFor(m => m.RegisterLastName)
    @Html.TextBoxFor(m => m.RegisterLastName)

    @Html.LabelFor(m => m.RegisterUsername)
    @Html.TextBoxFor(m => m.RegisterUsername)

    @Html.LabelFor(m => m.RegisterPassword)
    @Html.TextBoxFor(m => m.RegisterPassword)

    <input type='Submit' value='Register' />
}

I will know which button has been clicked. But I don't know what information other forms have. If I just use single form, I don't know which button has been submitted the form (I know I can user Request.Form["buttonvalue"], but I think it is not clean solution).

In the worst case it is possible that the model contains information about one grid:

public class LoginRegisterViewModel   
{
    public string LoginUsername { get; set; }
    public string LoginPassword { get; set; }

    public string RegisterUsername { get; set; }
    public string RegisterPassword { get; set; }
    public string RegisterFirstName { get; set; }
    public string RegisterLastName { get; set; }

    public List<RowModel> Rows {get; set;}
}

When I just send login form, I don't know current page, sorting, filters in my page. How MVC will help these problems?

Update 1:

I did not want to use JavaScript and jquery for some reasons.

Community
  • 1
  • 1
Seyed Morteza Mousavi
  • 6,855
  • 8
  • 43
  • 69
  • Could you rather not create a wrapper model that has all your models and post that? this way you can have all the data in one place – mahlatse Oct 04 '15 at 19:28
  • You have 2 separate forms which post back to 2 separate method. You know which button is clicked by which action method is hit. You cannot get the values in the other form. You need 2 separate models. I suggest you study the code in the `AccountController` when you create a new project in VS to understand what you should be doing. –  Oct 04 '15 at 23:11

1 Answers1

0

If I understand your problem correctly, following are the points that could help you:

1) Notice you will only have access to the form field data from the form that you submit.

2) Form the scenario you mentioned, If you can use jQuery/Ajax posting then on click of login button only post relevant input data and on click of register button post both forms.Eg:

var dataToPost = $("#frmLogin, #frmRegister").serialize();// Onclick of Register button posting both form data

NOTE: To handle above point, you need to have a view model that can hold both login & register information.

3) Per the form structure you put in your question, as a good practice always check for credential info against your db either only on login submit OR register button click.

4) The better way to check data state will be by validating against database in case session/cache is not an option.

Hope this help you...

Siva Gopal
  • 3,474
  • 1
  • 25
  • 22