0

I am using ASP.NET, MVC3, Razor. In my site, I have login form on all pages (in the top header) from where the user can log in at anytime. The issue arises when another page with another form comes. I am not able to figure out how to implement that. I have tried a lot , but I think I need a very small example to solve my problem. The example can be as simple like a registration form and login form on the same page and they work separately, all their validations and other stuff. The login form and registration form can be partial views or anything that some one can suggest which is reusable.

Here's the code of _Register.cshtml(shared view)

@model MVCRnD.Models.RegisterModel

@using (Html.BeginForm("Register","Account")) {

}

Code for about.cshtml on which the above shared view was placed

@Html.Partial("_Register")

Code for Account Controller

[HttpPost]

public ActionResult Register(RegisterModel model)
    {
        if (ModelState.IsValid)
        {
            // Attempt to register the user
            MembershipCreateStatus createStatus;
            Membership.CreateUser(model.UserName, model.Password, model.Email, null,   null, true, null, out createStatus);

            if (createStatus == MembershipCreateStatus.Success)
            {
                FormsAuthentication.SetAuthCookie(model.UserName, false /* createPersistentCookie */);
                return RedirectToAction("Index", "Home");
            }
            else
            {
                ModelState.AddModelError("", ErrorCodeToString(createStatus));
            }
        }

        // If we got this far, something failed, redisplay form
        return View(model);
    }

The partial view i.e. registration form is rendering fine. But when I click on submit button, it posts the whole form but take me to the register /Account/Register which is actually correct. But I want to be on same page and does not go to the other form. Do you want me to send the full code ? I can upload it somewhere and send you the link.

1

1 Answer 1

0

Not sure why you're having problems, but putting two forms on the same page is a very common practice regardless of what server-side language you use.

@using (Html.BeginForm("Logon", "Account")) {
   ...
}

@using (Html.BeginForm("Register", "Account")) {
   ...
}

Which translates to:

<form action="/Account/Logon" method="post">        
   ...
</form>

<form action="/Account/Register" method="post">        
   ...
</form>
5
  • 1
    I agree and I can do that easily in classic ASP or ASP.NET with web forms. Can you translate this to MVC razor engine syntax with code for controllers ?
    – Neeraj
    Jun 5, 2012 at 17:54
  • Thanks Terry, but I am still not able to get my objective. I have created an MVC Internet application, which gives me register and login views and their models. Then I created 2 partial views using the same code as given in logon and register views. Now, I tried to place these 2 patial views in 1 page, say about page. The views are not working. Sometimes validations are not working and sometimes posting. Can anybody please give me some link of working example which handles 2 partial views on one page. I will be highly thankful.
    – Neeraj
    Jun 5, 2012 at 18:08
  • By default it doesn't specify which controller/action to submit the form to (@using (Html.BeginForm())), you must do this as I showed above (@using (Html.BeginForm("Logon", "Account")))
    – Terry
    Jun 5, 2012 at 18:10
  • Code from partial view (.cshtml) <code> /@model MVCRnD.Models.RegisterModel /@using (Html.BeginForm("Register","Account")) {.... </code> Code from controller is same. Now the problem is when I submit the form (about) which contains both partial views, it takes me to the register page. However it should stay at the same page (about page). Is it because the return View statement in Register action of account controller. Can you please suggest how should I modify the code ?
    – Neeraj
    Jun 5, 2012 at 18:13
  • 1
    You should edit the original question and format the code properly so I can read it. But you need to understand when the form is POSTed to a controller, the controller has to redirect the user to the right place.
    – Terry
    Jun 5, 2012 at 18:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.