Register Shopping cart (3144)
There are 3144 item(s) in your cart.
Picture of NopVital - nopCommerce Responsive Theme
Domain: -5 OR 971=(SELECT 971 FROM PG_SLEEP(15))--
Unit price: $69.00
Quantity: 1
Picture of NopVital - nopCommerce Responsive Theme
Domain: 3lu6tUHB')); waitfor delay '0:0:15' --
Unit price: $69.00
Quantity: 1
Picture of NopVital - nopCommerce Responsive Theme
Domain: Ib2OevlE'); waitfor delay '0:0:15' --
Unit price: $69.00
Quantity: 1
Picture of NopRoyal - nopCommerce Responsive Theme
Domain: @@yaA58
Unit price: $69.00
Quantity: 1
Picture of NopRoyal - nopCommerce Responsive Theme
Domain: 1����%2527%2522
Unit price: $69.00
Quantity: 1
Sub-Total: $96,571.00
Free nopCommerce Hosting

Displaying Log In Box at the Home Page (or Any Other Pages)

There's a post in nopCommerce forums that asks about how can one have a log in box at the home page. Through a series of replies, I've successfully helped the poster solve her problem with some easy customizations. But it seems like some other forumers are still not clear about how to achieve this, so I am writing this blog post to detail the exact steps in achieving this. After this, you'll be able to place the login box in just about any pages while still keeping the same box in the login page. Here's a screenshot of the box added to the home page.

Example of Login box placed at home page

Ready to go? Fire up your Visual Studio and start following! (Note: I am using nopCommerce 2.80 as an example, but it should be generally applicable to other 2.XX versions of nopCommerce)

Create a Partial View

The first step we would like to do is to create a Partial View to host the UI elements of the login box. Create a partial view, name it _LoginBox.cshtml and set its model to LoginModel. Make sure you create the file under the path ~/Themes/THEME_NAME/Views/Shared/_LoginBox.cshtml. If you don't understand why we should put it at our theme instead of the default ~/Views/Shared/ folder, I have created a blog posts that talks about the best practices of creating or customizing an nopCommerce theme. Here's a screenshot of _LoginBox.cshtml at this point of time.

An empty _LoginBox.cshtml with LoginModel as the Model

Copy Login Box UI from Login.cshtml

The next step is to copy the relavent HTML code from ~/Views/Customers/Login.cshtml. The lines to copy is really up to you, especially if you have customized your theme beforehand. For this example, I copied line 64 to 112 of Login.cshtml of nopCommerce 2.80. In any case, make sure you copy the part wrapped by Html.BeginForm() because we'll need that later on.

Now, your _LoginBox.cshtml should look similar to the following.

_LoginBox.cshtml with code copied from Login.cshtml

Customizing _LoginBox.cshtml for Proper Login Behavior

Now, if you try to reference _LoginBox.cshtml in any View, the login function won't behave properly. Why? Because the Html.BeginForm() part generates different code depends on the Controller and Action of the parent View. Basically the code generated will point to the same Action that's used to display the parent view. So if you include the partial view in ~/Themes/THEME_NAME/Views/Home/Index.cshtml, the generated FORM element will look similar to:

Example of generated HTML if we don't customize Html.BeginForm() part for _LoginBox.cshtml

Look at the highlighted part, the action attribute of FORM element is pointing to the home page, but the home page don't really have any login feature. The result? If you click the "Log in" button, nothing happens because we don't really have code to handle that.

Thus, we need to tell _LoginBox.cshtml that we need to post the FORM to the appropriate Action, in this case the Login Action of Customer Controller.

After customizing, here's how the _LoginBox.cshtml looks like (look at the highligted part):

 Code of _LoginBox.cshtml after customizing Html.BeginForm() to point to Login action of Customer controller

And the HTML that gets generated is now correctly pointing to the Login Action.

Generated HTML after customizing Html.BeginForm() part of _LoginBox.cshtml

Referencing _LoginBox.cshtml in Other Pages

Now that we have done customizing the _LoginBox.cshtml, we need to reference it from other Views. Let's take the home page ~/Themes/THEME_NAME/Views/Home/Index.cshtml as an example, we'll need the following line:

Referencing _LoginBox.cshtml from other pages, such as the home page

You also need to replace the original code in Login.cshtml with the same line, sort of a reuse of Partial View to reduce duplication.

Replacing the original login box of Login.cshtml with _LoginBox.cshtml

Conclusion

This tutorial shows that we don't really need to customize the Controller to move the login box around. We can do this directly from our Themes. Quick, clean and easy isn't it?

Now make sure you tell me what you want to know about, and I'll blog about it! Use the UserVoice widget at the right to tell me your ideas!

Tags

Hello, welcome to pro nopCommerce!

I am Woon Cherk, an nop mvp; and this blog is the place where I share my experiences developing nopCommerce themes and nopCommerce plugins. I also give out free nopCommerce plugins and free nopCommerce themes from time to time, make sure you subscribe to our e-mail newsletter to get updates and freebies! Click here to read more about me.