Trying to create drop down login for Orchard CMS - asp.net-mvc

I am new to Orchard and to ASP.NET MVC. I am trying to override the login process for users so that instead of going to a dedicated login page (the default process) they get a drop down form from the user menu that logs them in. I have created overrides in my custom theme for LogOn.cshtml and User.cshtml. The code is as follows:
//User.cshtml
#using System.Web.Mvc;
#using Orchard.ContentManagement;
<nav class="navbar navbar-default navbar-fixed-top">
<div class="container-fluid">
<ul class="nav navbar-nav pull-right">
#if (WorkContext.CurrentUser != null) {
<li class="dropdown">
<button id="userDropdown" class="btn btn-primary navbar-btn dropdown-toggle" data-toggle="dropdown">
Welcome #Html.ItemDisplayText(WorkContext.CurrentUser) <span class="caret"></span>
</button>
<ul class="dropdown-menu">
<li>
#Html.ActionLink(T("Change Password").ToString(), "ChangePassword", new { Controller = "Account", Area = "Orchard.Users" })
</li>
<li>
#Html.ActionLink(T("Sign Out").ToString(), "LogOff", new { Controller = "Account", Area = "Orchard.Users", ReturnUrl = Context.Request.RawUrl }, new { rel = "nofollow" })
</li>
#if (AuthorizedFor(Orchard.Security.StandardPermissions.AccessAdminPanel)) {
<li class="divider"></li>
<li>
#Html.ActionLink(T("Dashboard").ToString(), "Index", new { Area = "Dashboard", Controller = "Admin" })
</li>
}
</ul>
</li>
}
else {
<li class="dropdown">
<button class="btn btn-primary navbar-btn dropdown-toggle" data-toggle="dropdown">
Sign In <span class="caret"></span>
</button>
<ul class="dropdown-menu dropdown-menu-right">
<li>
#Display.LogOn()
</li>
</ul>
</li>
}
</ul>
</div>
//LogOn.cshtml
#using System.Web.Mvc;
#using Orchard.ContentManagement;
<div>
#using (Html.BeginFormAntiForgeryPost(Url.Action("LogOn", new { ReturnUrl = Request.QueryString["ReturnUrl"] }))) {
<fieldset class="login-form group">
<legend>#T("Account Information")</legend>
<div class="form-group">
<label for="username-email">#T("Username")</label>
#Html.TextBox("userNameOrEmail", "", new { id = "username-email", autofocus = "autofocus", #class = "validate[required]" })
#Html.ValidationMessage("userNameOrEmail")
</div>
<div class="form-group">
<label for="password">#T("Password")</label>
#Html.Password("password", null, new { #class = "validate[required]" })
#Html.ValidationMessage("password")
</div>
<div class="checkbox">
<label class="forcheckbox" for="remember-me">
#Html.CheckBox("rememberMe", new { id = "remember-me" }) #T("Remember Me")
</label>
</div>
<div class="form-group">
<button class="primaryAction" type="submit">#T("Sign In")</button>
</div>
</fieldset>
}
</div>
The form displays fine in the dropdown, but when the submit button is clicked, I get a HTTP 404 error: Requested URL: /OrchardLocal/Contents/Item/LogOn.
I'm afraid that the default login process requires a separate view. I've been trying to figure out which existing override to use, but I'm just not seeing it. Any help would be greatly appreciated. Thanks.

You should be able to login properly by changing your form from
#using (Html.BeginFormAntiForgeryPost(Url.Action("LogOn", new { ReturnUrl = Request.QueryString["ReturnUrl"] })))
to something like this:
#using ( Html.BeginForm("LogOn", "Account", new { area = "Orchard.Users" /*other route values here*/ }, FormMethod.Post) )
{
#Html.AntiForgeryToken()
// ... your stuff here
}
This is the same for every other controller action. If in doubt: specify the area explicitly.

Related

System.NullReferenceException HResult=0x80004003 Message=Object reference not set to an instance of an object

I have an error when trying to run my MVC ASP.NET C# web application for the shared layout razor page.
The error I faced is at the code:
<div class="navbar navbar-inverse navbar-fixed-top height5">
<div class="navbar-header">
<button type="button" class="btn" data-toggle="collapse" data-target=".navbar-collapse" id="make-small-nav">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
#if (System.Configuration.ConfigurationManager.AppSettings["Applicant Tracking System"].ToLower().ToString() == "true")
{
#Html.ActionLink("Applicant Tracking System" + #System.Configuration.ConfigurationManager.AppSettings[""], "Index", "Home", new { area = "" }, new { #class = "navbar-brand" })
}
else
{
#Html.ActionLink(#System.Configuration.ConfigurationManager.AppSettings["Applicant Tracking System"], "Index", "Home", new { area = "" }, new { #class = "navbar-brand" })
}
</div>
<div style="float:right">
#if (Session["username"] != null)
{
<text> Welcome, #Session["username"].ToString()!</text><span>
#Html.ActionLink(" ", "Index", "Home", new { area = "" }, new { #class = "fa fa-sign-out fa-2x" })
</span>
}
</div>
</div>
I was expecting to make a menu button to hover down the side bar menu for the code:
<div class="maincover LeftBar">
<div class="navbar-collapse collapse">
<div class="nav navbar-nav">
<div class="Ul-span">MASTER MAINTENANCE</div>
<div class="list-group">
<a class="list-group-item" role="button" href="#Url.Action("PositionList", "Position")"><span class="fa fa-file"></span> <b>MM POSITION</b></a>
</div>
<div class="list-group">
<a class="list-group-item" role="button" href="#Url.Action("CandidateList", "Candidate")"><span class="fa fa-file"></span> <b>MM CANDIDATE</b></a>
</div>
<div class="Ul-span">INTERVIEW TRACKING</div>
<div class="list-group">
<a class="list-group-item" role="button" href="#Url.Action("Index", "Interview")"><span class="fa fa-file"></span> <b>INTERVIEW</b></a>
</div>
</div>
</div>
</div>
Is there any ways to fix this issue?
possible null return System.Configuration.ConfigurationManager.AppSettings[KeyName] If KeyName Not Exists In AppSettings
Please Check your web.config appsettings section.

ASP.NET MVC: How do I link correctly to my Delete method using Html.BeginForm?

So far I came up with this:
<ul class="menu" id="#Model[i].VideoId">
<li class="menu-item">
#using (Html.BeginForm("Delete", "Video", FormMethod.Post, new { id = Model[i].Id }))
{
#Html.HttpMethodOverride(HttpVerbs.Delete)
<button class="btn btn-link" type="submit">Delete</button>
}
</li>
<li class="menu-item">Edit </li>
</ul>
But it doesn't work. My controller is Video and the method is Delete, which takes an id parameter and deletes by id from the database.

How do I get my navbar-header with a logo and navbar-brand to line up?

I'm pretty new to CSS and Bootstrap in general.
I have created a standard mvc web app.
I have the standard _layout partial view with the part code shown.
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
#Html.ActionLink("Application name", "Index", "Home", new { area = "" }, new { #class = "navbar-brand" })
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>#Html.ActionLink("Home", "Index", "Home")</li>
<li>#Html.ActionLink("About", "About", "Home")</li>
<li>#Html.ActionLink("Contact", "Contact", "Home")</li>
</ul>
#Html.Partial("_LoginPartial")
</div>
</div>
</div>
In the _LoginPartial view, part code is show:
<ul class="nav navbar-nav navbar-right">
<li>#Html.ActionLink("Register", "Register", "Account", routeValues: null, htmlAttributes: new { id = "registerLink" })</li>
<li>#Html.ActionLink("Log in", "Login", "Account", routeValues: null, htmlAttributes: new { id = "loginLink" })</li>
</ul>
I am trying to add a logo to be placed before the Application name ActionLink. The image is too large and does not size down to the navbar-header's size and the Register and Login text is no longer at the bottom of the navbar.
Its probably something simple, but I cannot figure it out.
I have followed:
https://www.youtube.com/watch?v=4dG2ke1JUNs
but that does not work as in the video.
I have also tried: Brand Image too big and not aligned on Bootstrap navbar
but then the image doesn't show.
Any ideas? As said, I'm new to css and bootstrap and have tried using the Developer Tools but cannot figure out why its not working.
Thanks.

MVC 5 does not contain a definition for bootstrap

I am trying to add a drop down on the nav bar for this MVC project and getting a "does not contain a definition for bootstrap error." Sorry if this question is stupid but this is my first time creating a website using ASP and MVC.
Whenever I run it I get an Exception of type 'System.Web.HttpCompileExpection' occured in System.Web.dll but was not handled in uesr code.
Here is my _LoginPartial view
#using Microsoft.AspNet.Identity
#if (Request.IsAuthenticated)
{
using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm", #class = "navbar-right" }))
{
#Html.AntiForgeryToken()
<ul class="nav navbar-nav navbar-right">
<li>
#using (var dd = Html.Bootstrap().Begin(new DropDown("Manage")))
{
#dd.ActionLink("Account", "Manage", "Account", routeValues: null, htmlAttributes: new { title = "Manage" })
#dd.Action("Posts", "Manage", "Book", routeValues: null, htmlAttributes: new { title = "Manage" })
}
</li>
<li>Log off</li>
</ul>
}
}
else
{
<ul class="nav navbar-nav navbar-right">
<li>#Html.ActionLink("Log in", "Login", "Account", routeValues: null, htmlAttributes: new { id = "loginLink" })</li>
</ul>
}
Here is the head of my _layout.cshtml
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>#ViewBag.Title - Books4CSUSM</title>
#Styles.Render("~/Content/css")
#Scripts.Render("~/bundles/modernizr")
</head>
Here is the body of my _layout.cshtml
<body>
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
#Html.ActionLink("Books4CSUSM", "Index", "Home", null, new { #class = "navbar-brand" })
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>#Html.ActionLink("Home", "Index", "Home")</li>
<li>#Html.ActionLink("About", "About", "Home")</li>
<li>#Html.ActionLink("Contact", "Contact", "Home")</li>
<li>#Html.ActionLink("Books", "Index", "Book")</li>
</ul>
#Html.Partial("_LoginPartial")
</div>
</div>
</div>
<div class="container body-content">
#RenderBody()
<hr />
<footer>
<p>© #DateTime.Now.Year - Books4CSUSM</p>
</footer>
</div>
#Scripts.Render("~/bundles/jquery")
#Scripts.Render("~/bundles/bootstrap")
#RenderSection("scripts", required: false)
Based on the included code, I assume this reference Html.Bootstrap() is the issue. Bootstrap() is defined as an extension method of HtmlHelper, but MVC can't find it, hence this exception. Add a using statement to add the namespace where the bootstrap framework is defined.
I figured this out. I did not install TwitterBootstrapMVC with nuget. I installed TwitterBootstrap 3 and just used the regular syntax for html.
<li class="dropdown">
Manage <b class="caret"></b>
<ul class="dropdown-menu">
<li>#Html.ActionLink("Manage Account", "Manage", "Account", routeValues: null, htmlAttributes: new { title = "Manage" })</li>
<li class="divider"></li>
<li>#Html.ActionLink("Manage Posts", "Manage", "Book", routeValues: null, htmlAttributes: new { title = "Manage" })</li>
</ul>
</li>
<li>Log off</li>
</ul>
Thanks for your help.

How to put jqm data-role="page" in a MVC view page?

I'm using MVC3 to create a beta mobile web app. It was working fine until I started to try and get inline pages inside of a view similar to this
This is what I currently have in my view called Index. How come when I click the links in the navbar it doesn't go to the page?
#model List<ShawGoVersion1.Models.NewsItem>
#{
ViewBag.Title = "News";
}
#section Header {
#Html.ActionLink("Back", "Index", "Home", null, new { data_icon = "arrow-l"})
<h1>#ViewBag.Title</h1>
<div data-role="navbar">
<ul>
<li>My News</li>
<li>Press Releases</li>
<li>All News</li>
</ul>
</div>
}
<div data-role="page" id="MyNews">
<div data-role="content">
<h1>This feature coming soon</h1>
</div>
</div>
<div data-role="page" id="AllNews">
<div data-role="content">
<ul data-role="listview">
#for (int i = 0; i < Model.Count; i++ )
{
if (Model[i].type == "Article")
{
<li>
<a href="#Url.Action("NewsItemDetails", "News", new { id = i })">
#if (Model[i].pictureURL != null)
{
<div class="ui-grid-c">
<div class="ui-block-a" style="width:31%;vertical-align:middle">
<img src="#Model[i].pictureURL" style="width:100%;height:auto"/>
</div>
<div class="ui-block-b" style="width:4%; vertical-align:middle"></div>
<div class="ui-block-c" style="width:65%;vertical-align:middle">
<h3 style="font-size:small; white-space:normal">#Model[i].title</h3>
<p><strong>#Model[i].date.ToShortDateString()</strong></p>
</div>
</div>
}
else
{
<h3 style="font-size:small; white-space:normal">#Model[i].title</h3>
<p><strong>#Model[i].date.ToShortDateString()</strong></p>
}
</a>
</li>
}
}
</ul>
</div>
</div>
<div id="PressReleases" data-role="page">
<div data-role"content">
<ul data-role="listview">
#for (int i = 0; i < Model.Count; i++ )
{
if (Model[i].type == "Press Release")
{
<li>
<a href="#Url.Action("NewsItemDetails", "News", new { id = i })">
#if (Model[i].pictureURL != null)
{
<div class="ui-grid-c">
<div class="ui-block-a" style="width:31%;vertical-align:middle">
<img src="#Model[i].pictureURL" style="width:100%;height:auto"/>
</div>
<div class="ui-block-b" style="width:4%; vertical-align:middle"></div>
<div class="ui-block-c" style="width:65%;vertical-align:middle">
<h3 style="font-size:small; white-space:normal">#Model[i].title</h3>
<p><strong>#Model[i].date.ToShortDateString()</strong></p>
</div>
</div>
}
else
{
<h3 style="font-size:small; white-space:normal">#Model[i].title</h3>
<p><strong>#Model[i].date.ToShortDateString()</strong></p>
}
</a>
</li>
}
}
</ul>
</div>
</div>
Your navbar should appear within data-role="page". Within the data-role="page" you should have a data-role="header" and your navbar should go in there if you want it to be in the header of the jqm page.
Alternatively if you don't want to do it that way(which you should do, as its the correct way). You could try catch the onclick event of those links in your navbar yourself and then fire
$.mobile.changePage();
This may work.

Resources