Whenever I run the program, it works perfectly. However, as soon as I hit the "Login" button it tells me
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Requested URL: /LoginController/Authorize
I checked everything and the spelling is correct. I am new to this and cannot figure out what I am doing wrong. Any guidance would be highly appreciated it.
Inside of the App_Start folder I have my RouteConfig.cs file. It contains the following:
namespace CoffeeShop_Web_App
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "LoginController", action = "Index", id = UrlParameter.Optional }
);
}
}
}
I have one controller which is my LoginController.cs which contains the following.
namespace CoffeeShop_Web_App.Controllers
{
public class LoginController : Controller
{
// GET: Login
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Authorize()
{
return View();
}
}
}
Lastly, my only view Index.cshtml which contains the following.
#model CoffeeShop_Web_App.Models.OwnerLogin
#{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Login</title>
<style>
#login-div {
position: absolute;
left: 40%;
top: 40%;
border: 1px solid #ccc;
padding: 10px 10px;
}
</style>
</head>
<body>
<div id="login-div">
#using (Html.BeginForm("Authorize", "LoginController", FormMethod.Post))
{
<table>
<tr>
<td></td>
<td style="text-decoration:underline">Coffee Shop</td>
</tr>
<tr>
<td>
#Html.LabelFor(model => model.USERNAME)
</td>
<td>
#Html.EditorFor(model => model.USERNAME)
</td>
</tr>
<tr>
<td></td>
<td>#Html.ValidationMessageFor(model => model.USERNAME)</td>
</tr>
<tr>
<td>
#Html.LabelFor(model => model.PASSWORD)
</td>
<td>
#Html.EditorFor(model => model.PASSWORD)
</td>
</tr>
<tr>
<td></td>
<td>#Html.ValidationMessageFor(model => model.PASSWORD)</td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" name="name" value="Login" />
<input type="reset" name="name" value="Clear" />
</td>
</tr>
</table>
}
</div>
<script src="~/Scripts/jquery-3.4.1.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
</body>
</html>
Your form is posting back to the Authorize method of LoginController controller:
#using (Html.BeginForm("Authorize", "LoginController", FormMethod.Post))
You don't have to specify controller suffix for the name of the controller. The following should fix it:
#using (Html.BeginForm("Authorize", "Login", FormMethod.Post))
Also you did the same mistake when setting up the routing too:
defaults: new { controller = "LoginController", action = "Index",
id = UrlParameter.Optional }
which should have been just:
defaults: new { controller = "Login", action = "Index",
id = UrlParameter.Optional }
Related
I want to get the output as single row like group name and visible or not. but in my scenario i am getting different output .
enter image description here
Here is my html code.
#model List<F3CentricMVCApp.Areas.KnowledgeBox.Models.GroupResponse>
<script src="~/lib/jquery-validation/dist/jquery.validate.js"></script>
<script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.js"></script>
<div class="col-12">
<table id="tblSectionGroups" class="table responsive table-striped" bPaginate="true">
<thead>
<tr>
<th width="95%">Groups</th>
<th width="5%" class="no-sort"></th>
</tr>
</thead>
<tbody>
#if (#ViewBag.UnAssidnedGroups is not null)
{
#foreach (var item in Model)
{
<tr>
<td>#item.Name</td>
#foreach (var unassinedGroup in ViewBag.UnAssidnedGroups)
{
#if (#unassinedGroup.GroupId == #item.Id)
{
<td>
<input type="checkbox" class="chkSectionVisibility" />
</td>
}
else
{
<td>
<input type="checkbox" class="chkSectionVisibility" checked="checked" />
</td>
}
}
</tr>
}
}
</tbody>
</table>
</div>
<script>
$('#tblSectionGroups').DataTable({
"bLengthChange": false,
"pageLength": 5,
"bPaginate": true,
"stripeClasses": [],
"info": false,
language: {
searchPlaceholder: "Type to filter list",
search: ""
},
"order": [],
"columnDefs": [{
"targets": 'no-sort',
"orderable": false,
}]
});
</script>
<div class="col-md-12 text-right">
<button type="button" class="btn btn-custom" tabindex="3" id="btnSave">Save</button>
</div>
Single checkbox for each group in a row is my requirement. any body can guide me how to deal with two collections in a code which is going through 2 foreach statements. but the logic should not be disturbed after all.
Update your foreach code as below i.e.
#if (#ViewBag.UnAssidnedGroups is not null)
{
#foreach (var item in Model)
{
int isUnassigned = 0;
<tr>
<td>#item.Name</td>
#foreach (var unassinedGroup in ViewBag.UnAssidnedGroups)
{
#if (#unassinedGroup.GroupId == #item.Id)
{
<td>
<input type="checkbox" class="chkSectionVisibility" />
</td>
// Setting.
isUnassigned++;
}
}
if (isUnassigned <= 0)
{
<td>
<input type="checkbox" class="chkSectionVisibility" checked="checked" />
</td>
}
</tr>
}
}
Hopefully this will solve your issue.
In the following code from my MVC 5 View, I am dynamically building label and textbox controls but I need to format them in a table so I am not sure how I can do this.
#using InFlowConvertWeb.WebUI.Models
#model InFlowConvertWeb.WebUI.Models.SearchControlListViewModel
#{
ViewBag.Title = "List";
}
#using (Html.BeginForm())
{
int searchControlIndex = 0;
foreach (SearchControl searchControl in Model.SearchControls)
{
switch (searchControl.ControlType)
{
case SearchControl.ControlTypes.TextBox:
{
<div class="form-group" style="margin-left: 15px">
#Html.Label(searchControl.FieldName,
new { #class = "col-md-12 control-label" })
#Html.TextBoxFor(
x => x.SearchControls[searchControlIndex].SearchValue)
#Html.HiddenFor(x => x.SearchControls[searchControlIndex].DataTable)
#Html.HiddenFor(x => x.SearchControls[searchControlIndex].FieldName)
</div>
break;
}
}
searchControlIndex += 1;
}
<div class="col-md-2">
<h2>
<input type="submit" value="Submit Selections" />
</h2>
</div>
Any suggestions would be greatly appreciated,
Bob
Try this Example:
#using InFlowConvertWeb.WebUI.Models
#model InFlowConvertWeb.WebUI.Models.SearchControlListViewModel
#{
ViewBag.Title = "List";
}
#using (Html.BeginForm())
{
<table id="dataTable" class="table table-bordered table-hover">
<tbody>
#{int searchControlIndex = 0;}
#foreach (SearchControl searchControl in Model.SearchControls)
{
switch (searchControl.ControlType)
{
case SearchControl.ControlTypes.TextBox:
{
<tr>
<td>
#Html.Label(searchControl.FieldName, new { #class = "col-md-12 control-label" })
</td>
<td>
#Html.TextBoxFor(x =>x.SearchControls[searchControlIndex].SearchValue)
#Html.HiddenFor(x =>x.SearchControls[searchControlIndex].DataTable)
#Html.HiddenFor(x =>x.SearchControls[searchControlIndex].FieldName)
</td>
</tr>
break;
}
}
searchControlIndex += 1;
}
</tbody>
</table>
<div class="col-md-2">
<h2> <input type="submit" value="Submit Selections" /> </h2>
</div>
}
I have the following query in my model(Supplier) class :
#NamedQuery(name = "Supplier.findSupplierKeyId", query = "SELECT s FROM Supplier s WHERE s.supplierid LIKE (':supplieridkey%')")
I have following function in my Controller(SupplierSerivce) class:
public List<Supplier> findSupplierKeyId(String supplierkeyid){
List<Supplier> supplierList = mgr.createNamedQuery("Supplier.findSupplierKeyId").setParameter("supplieridkey", supplierkeyid).getResultList();
return supplierList;
}
I want to get in a html textfield :
<form action="SearchSupplierIdKey.jsp" method="POST">
<div>
<input type="text" name="supIdKey"/>
<input type="submit" value="Search" name="button"/>
</div>
</form>
then get parameter from the textfield and pass it into supService.findSupplierKeyId through servlet:
public class SearchSupplierIdKey extends HttpServlet {
#PersistenceContext
EntityManager em;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
SupplierService supService = new SupplierService(em);
HttpSession session = request.getSession();
String supId = (String) session.getAttribute("supId");
String button = (String) session.getAttribute("button");
List<Supplier> supplierListResult = supService.findSupplierKeyId(supId);
session.setAttribute("supplierListResult", supplierListResult);
if (button.equals("Search")) {
response.sendRedirect("ViewSupplierByIdKey.jsp");
}
} catch (Exception ex) {
Logger.getLogger(AddSupplier.class.getName()).log(Level.SEVERE, null, ex);
}
}
Then show the result in ViewSupplierByIdKey.jsp :
<%#page import="java.util.List"%>
<%#page import="model.Supplier"%>
<!-- retrieve session object, itemList -->
<%
List<Supplier> supplierListResult = (List)session.getAttribute("supplierListResult");
%>
<html>
<head>
<title>Supplier Search Result</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<table cellspacing="0" cellpadding="0" style="margin-left:auto; margin-right:auto; border:solid; width: 1000px">
<tr style="border:solid">
<td style="border:solid ">
<h3 style="text-align: center">ABC Health Supplement Shop System</h3>
</td>
</tr>
<tr style="border: solid">
<td style="border: solid">
<center><h1><i><b><font face="Segoe Script" color="#FF0000">Supplier Search Result(By ID Key)</font></b></i></h1></center>
</td>
</tr>
<tr style="border:solid">
<td style="border:solid">
<center><div>
<table border="1">
<tr>
<th>Supplier ID</th>
<th>Supplier Name</th>
<th>Manufacturer</th>
<th>Contact Num</th>
<th>Address</th>
</tr>
<% for (Supplier supplier: supplierListResult){ %>
<tr>
<td><%= supplier.getSupplierid() %></td>
<td><%= supplier.getSuppliername()%> </td>
<td><%= supplier.getManufacturer()%> </td>
<td><%= supplier.getContactnum()%> </td>
<td><%= supplier.getAddress()%> </td>
</tr>
<% } %>
</table>
<br><br>
<p>Back to Menu page</p>
</div>
</center>
</td>
</tr>
</table>
</body>
</html>
but i dont knw why i cant proceed to ViewSupplierByIdKey.jsp, it stuck at the controller class
(SearchSupplierIdKey.java). Please Help :( :(
One thing that I notice is the request parameter is retrieved using the name supId:
String supId = (String) session.getAttribute("supId");
but specified as supIdKey in the HTML:
<input type="text" name="supIdKey"/>
The name attribute used on the input should match the key being used to retrieve the attribute.
I'm missing something here. I have a table of items with checkboxes that I want to be able to select items from and then send that list of checked items to the controller. I can't get anything passed to the controller. I want a list or array that I can use in the controller.
Here is what I have:
#model Recipe2Grocery.ViewModels.GroceryListViewModel
#{
ViewBag.Title = "Index";
}
#section Scripts{
<style type="text/css">
table {
color: white;
}
</style>
}
<div style="margin: 20px;">
#using (Html.BeginForm("Index", "RecipeIngredient", FormMethod.Post))
{
<table class="table">
<tr>
<th></th>
<th>
Quantity
</th>
<th>
IngredientName
</th>
<th>
Category
</th>
#foreach (var item in Model.ShoppingList)
{
<tr>
<td>
#Html.CheckBoxFor(modelItem => item.IsChecked)
</td>
<td>
#Html.DisplayFor(modelItem => item.Ingredient.Quantity)
</td>
<td>
#Html.DisplayFor(modelItem => item.Ingredient.IngredientName)
</td>
<td>
#Html.DisplayFor(modelItem => item.Ingredient.Category)
</td>
</tr>
}
</table>
<input type="submit" />
}
Then I have a viewmodel as such:
public class ListItem
{
public bool IsChecked { get; set; }
public RecipeIngredient Ingredient { get; set; }
public ListItem()
{
Ingredient = new RecipeIngredient();
}
}
public class GroceryListViewModel
{
public List<ListItem> ShoppingList { get; set; }
public GroceryListViewModel()
{
ShoppingList = new List<ListItem>();
}
}
Then my controller wants to be something like:
[HttpPost]
public ActionResult Index(GroceryListViewModel vm)
{
// blah
return RedirectToAction("Index", "Home");
}
Hi I have created the code below to check if a user is valid to login to a database, I am using SSL to secure the connection, but I dont know if this is still a good way in which I have done it. Could anyone give advice? Thanks.
(sorry its abit long)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="login_styles.css" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>
<script src="../jquery/ui/effects.core.js"></script>
<script src="../jquery/ui/effects.shake.js"></script>
<title>Login</title>
</head>
<body>
<script type="text/javascript" >
var clicked = 0;
$(document).ready(function()
{
document.form.username.focus();
})
function make_request()
{
try
{
// Firefox, Opera 8.0+, Safari
httpxml=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
httpxml=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
httpxml=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("Your browser does not support AJAX!");
return false; }
}
}
}
function checkCanSubmit()
{
if (fnameok && lnameok && emailok && projectnameok && descriptionok)
{
document.getElementById("button").disabled= false;
}
else
{
document.getElementById("button").disabled= true;
}
}
function counting()
{
clicked++;
}
function check_login(username, password)
{
make_request()
$("#login_loading").show();
var parameters = 'username=' + document.getElementById("username").value + '&password=' + document.getElementById("password").value;
httpxml.onreadystatechange = stateck;
httpxml.open('POST', 'login.php', true);
httpxml.setRequestHeader('Content-Type', "application/x-www-form-urlencoded");
httpxml.setRequestHeader('Content-Length', parameters.length);
httpxml.send(parameters);
function stateck()
{
if(httpxml.readyState==4)
{
if (httpxml.responseText.indexOf("Login Successful") >= 0)
{
$("#login_loading").hide("fast");
$("#login_error").hide("slow");
$("#forgot").hide("slow");
$("#button").fadeOut("slow");
$("#loading_big").fadeIn("slow");
setTimeout( "window.location.href = '../members'", 2000);
}
else
{
focus();
if (clicked > 1)
{
$("#login_error").effect("shake", { times:2 }, 100);
$("#login_loading").fadeOut(1000);
$("#forgot").show("slow");
}
else
{
$("#login_loading").fadeOut(1000);
$("#login_successful").hide("normal");
$("#login_error").show("slow");
}
}
}
}
}
function focus()
{
document.form.username.focus();
}
</script>
<div class="login_loading" id="login_loading"><img src="images/login_loader.gif" alt="loading1" id="loading1" /></div>
<div class="loading_big" id="loading_big"> Login successful please wait . . .<br /><img src="images/loading_big.gif" alt="loading" /></div>
<div class="form">
<table class="form" id="form" cellpadding="10" >
<form id="form" name="form" method="post" action="login.php" />
<tr>
<td>Username</td>
<td><input type="text" name="username" id="username" size="26" /></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="password" id="password" size="26"/>
<div id="login_error" class="login_error" ><img src="images/error.png" alt="Error" id="error" />Username or password incorrect.</div>
<div id="forgot" class="login_error">Forgot Password?</div>
<div id="login_successful" class="login_successful" >Login successful please wait . . . </div></td>
</tr>
<tr>
<td>
<input type="submit" name="button" id="button" value="Submit" onclick="check_login(username, password); counting(); return false;"/>
</td>
</tr>
</form>
</table>
</div>
</body>
</html>
Code formatting goes abit of a mess on here :p
Thanks again.
Aside from some obvious cleanup points, and the fact that since you're importing jQuery anyway you might as well use its Ajax methods rather than hand-coding your own, I see nothing terribly wrong here. With one huge caveat - "members" had better validate that the user has a legitimate login session. What happens if the user accesses ../members without coming to this login page first, or following a failed login? Are they redirected to the login page?