I'm working with MVC and spent whole day getting stuck with this problem. Here is code at the controller:
public class CMController : Controller
{
public ActionResult SignUp()
{
return View("SignUpPage");
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult SignUp(User u)
{
if (ModelState.IsValid)
{
using (CmdbEntities dc = new CmdbEntities())
{
dc.Users.Add(u);
dc.SaveChanges();
ModelState.Clear();
u = null;
ViewBag.Message = ("Successfully sign on");
}
}
else
{
ViewBag.Message = ("Error");
}
return View("Welcome");
}
}
Here is the SignUpPage.cshtml view:
#using (Html.BeginForm("SignUp", "CM", FormMethod.Post))
{
#Html.AntiForgeryToken()
<div class="form-group" style="padding:5px 50px">
<label> First name:</label>
<div>
#Html.TextBoxFor(model => model.FirstName, new {#class="form-control", placeholder="Enter first name", ID="FirstName"})
</div>
</div>
<div class="form-group" style="padding:5px 50px">
<label >Last name:</label>
<div>
#Html.TextBoxFor(model => model.LastName, new {#class="form-control", placeholder="Enter first name", ID="LastName"})
</div>
</div>
<div class="form-group" style="padding:5px 50px">
<label for="email">Email:</label>
<div>
#Html.TextBoxFor(model => model.Email, new {#class="form-control", placeholder="Enter email", ID="Email", type="email"})
</div>
</div>
<div class="form-group" style="padding:5px 50px">
<label >User name:</label>
<div>
#Html.TextBoxFor(model => model.UserName, new {#class="form-control", placeholder="Enter user name", ID="UserName"})
</div>
</div>
<div class="form-group" style="padding:5px 50px">
<label for="pwd">Password:</label>
<div>
#Html.TextBoxFor(model => model.Password, new {#class="form-control", placeholder="Enter password", ID="Password", type="password"})
</div>
</div>
<div class="form-group" style="padding:5px 50px">
<label for="pwd">Confirm password:</label>
<div>
#Html.TextBoxFor(model => model.ConfirmPassword, new {#class="form-control", placeholder="Re-enter password", ID="_password", type="password"})
</div>
</div>
<div class="form-group" style="padding:5px 50px">
<div>
<input type="submit" ID="SignUp" class="btn btn-success" value="Sign Up" />
</div>
</div>
}
The problem is, when load page first time, the action SignUp() is called which is correct. But when submit the form, SignUp() is called again instead of the post action SignUp(User u).
One more problem, even the form has post method, when click submit the url contains input information just like get method, I don't understand.
Here is the page html source of form tag.
<form action="/CM/SignUp" method="post"><input name="__RequestVerificationToken" type="hidden" value="VJ0YcQr1Z-l3Qo717pRpZNT-QtL59G2EJXy2JQL8NFnOm-XoNnAD-8T-Itz3i1EboGj0bhpBf2G26ifxm4F5ZRyKimkOyZW1AxZ3ckPNuRk1" />
Please help, thank you.
Can you please try adding
[AllowAnonymous]
to the post method?
Did you activate global authorization anywhere
System.Web.Mvc.AuthorizeAttribute
?
Related
I have a ASP.NET MVC app running with Forms Authenticated enabled.
In my own machine and browser (Edge,IE,chrome), I am able to login in my website.
It works for other users as well. But, for some users, they are not able to login to the home page of the website and it is going to 404 page not found error....
I am sharing my code behind..
Login action method
[HttpPost]
[AllowAnonymous]
public ActionResult LoginUser(LoginUser login)
{
if (ModelState.IsValid)
{
if(AuthenticateUserAD(login) != "")
{
FormsAuthentication.SetAuthCookie(login.UserName, login.RememberMe);
if (login.RememberMe)
{
HttpCookie cookie = new HttpCookie("Login");
cookie.Values.Add("UserName", login.UserName);
cookie.Values.Add("Password", login.Password);
cookie.Expires = DateTime.Now.AddDays(10);
Response.Cookies.Add(cookie);
}
ViewBag.IsLoginValid = "Valid";
return RedirectToAction("Index", "Service");
}
else
{
ViewBag.Invalid = Session["error"].ToString();
ViewBag.IsLoginValid = "Invalid";
return View("LoginUser");
}
}
else
{
ModelState.AddModelError("", "The user name or password provided is incorrect");
}
return View(login);
}
LoginUser.cshtml
#using (Html.BeginForm("LoginUser", null, FormMethod.Post))
{
<div class="row mt-5">
<div class="col-6 col-sm-6 offset-3 bodyColor" style="border-radius: 25px;">
<div class="col-8 col-sm-8 offset-2">
<div style="max-width: 400px;">
<div class="text-center section-title">
<h2>Login</h2>
</div>
#Html.LabelFor(model => model.UserName)
<div class="input-group form-group">
<div class="input-group-prepend">
<span class="input-group-text date-Textbox"><i class="fa fa-user"></i></span>
</div>
#Html.TextBoxFor(model => model.UserName, new { #class = "form-control textbox-font" })
#Html.ValidationMessageFor(model => model.UserName, "", new { #class = "textRed" })
</div>
#Html.LabelFor(model => model.Password)
<div class="input-group form-group">
<div class="input-group-prepend">
<span class="input-group-text date-Textbox"><i class="fa fa-lock"></i></span>
</div>
#Html.TextBoxFor(model => model.Password, new { #class = "form-control textbox-font", type = "password" })
#Html.ValidationMessageFor(model => model.Password, "", new { #class = "textRed" })
</div>
#Html.CheckBoxFor(model => model.RememberMe)
#Html.LabelFor(model => model.RememberMe)
<input type="hidden" name="ReturnUrl" value="#Request.QueryString["ReturnUrl"]" />
<div class="col-sm-12 col-xs-12">
<input type="submit" value="Login" id="btnLogin" style="width:100%;" autofocus class="btn btn-main-custom" />
</div>
</div>
</div>
}
I really do not know whether this is a browser problem or a development problem or cookies problem.
Appreciate any help, Could anyone find a solution or a workaround?
Thanks
In my ASP.NET MVC application in the view, I'm calling another view that is not related to the current model. There I need some help that how to call the different model views from another view.
#model Asp_PASMVC.Models.VehicleService
#using Asp_PASMVC.Infrastructure
#{
ViewBag.Title = "View";
Layout = "~/Views/Shared/_Layout.cshtml";
List<SelectListItem> CompanyList = (List<SelectListItem>)TempData.Peek("ComapnyList");
List<SelectListItem> ReqTypes = (List<SelectListItem>)TempData.Peek("RequestTyleList");
List<SelectListItem> Employees = (List<SelectListItem>)TempData.Peek("EmployeeList");
List<SelectListItem> Location = (List<SelectListItem>)TempData.Peek("LocationList");
Asp_PASMVC.Models.AppRequest RequestDetails = (Asp_PASMVC.Models.AppRequest)TempData.Peek("RequestDetails");
}
#{
Html.RenderPartial("_MainRequestView", RequestDetails);
}
#using (Html.BeginForm("WorkshopUpdate", "VehicleService", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
#Html.HiddenFor(model => model.Req_Id)
#Html.AntiForgeryToken()
if (Model != null && Model.VehicleServiceApproveDetails != null)
{
foreach (Asp_PASMVC.Models.VehicleServiceApproveDetails Emp in Model.VehicleServiceApproveDetails)
{
Html.RenderPartial("_WorkshopUpdate", Emp);
}
}
<section class="content">
<div class="container-fluid">
<div class="row">
<div class="col-12">
<!-- Default box -->
<div class="card">
<div class="card-header">
<h3 class="card-title">Approver Details</h3>
<div class="card-tools">
<button type="button" class="btn btn-tool" data-card-widget="collapse" title="Collapse">
<i class="fas fa-minus"></i>
</button>
</div>
</div>
<div class="card-body">
<div>
<fieldset id="pnlApproverList" style="display:none">
<legend><h5>To whom you want to send this request for approval ? </h5> </legend>
<br />
<ul id="RequApprover" style="list-style-type: none">
#if (Model != null && Model.ApprovalPartyList != null)
{
foreach (Asp_PASMVC.Models.ApprovalParty Emp in Model.ApprovalPartyList)
{
Html.RenderPartial("_ApprovalView", Emp);
}
}
</ul>
<button type="button" id="addAnotherApprover" class="btn btn-success" href="#" onclick="this.style.display = 'none';">Add</button>
<script type="text/javascript">
$(function () {
// $("#movieEditor").sortable();
$("#addAnotherApprover").click(function () {
$.get('/VehicleService/AddApproverToReq', function (template) {
$("#RequApprover").append(template);
});
});
});
</script>
<br />
</fieldset>
</div>
</div>
<!-- /.card-footer-->
</div>
<!-- /.card -->
</div>
</div>
</div>
</section>
<div class="card-footer">
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Update and Sent" class="btn btn-success" />
</div>
</div>
</div>
}
<p>
#Html.ActionLink("Back to List", "Index")
</p>
So likewise here the model is VehicleService. So within that view, I want to call another view that is not within the vehicleservice model.
But I cannot load that partial view within this view. Is there any way to do this?
#model Asp_PASMVC.Models.ApprovalParty
#using Asp_PASMVC.Infrastructure
#{
string UserLvel = TempData.Peek("UserLevelClaims").ToString();
}
<li style="padding-bottom:15px">
#using (Html.BeginCollectionItem("ApprovalPartyList"))
{
<div class="row">
<div class="col-md-5 col-sm-5">
<div class="form-group">
<label>
#Html.RadioButtonFor(m => m.Approve_Type, false)
<span class="radiomargin">For Manager</span>
</label>
<br />
#if (UserLvel != "1")
{
<label>
#Html.RadioButtonFor(m => m.Approve_Type, true)
<span class="radiomargin">For Top Manager </span>
</label>
#Html.ValidationMessageFor(model => model.Approve_Type, "", new { #class = "text-danger" })
}
</div>
</div>
</div>
<br />
<div class="row">
<div class="col-md-6 col-sm-6">
<div class="form-group row">
Select the Approver
<div class="col-sm-8">
#Html.DropDownListFor(model => model.Approver_Id, new List<SelectListItem>(), new { #id = "ddlEmployees", #class = "js-dropdown" })
#Html.ValidationMessageFor(model => model.Approver_Id, "", new { #class = "text-danger" })
</div>
</div>
</div>
</div>
}
</li>
Create a ViewModel which can have both Properties and pass that viewmodel to View
Model class:
public class VehicleSerivceViewModel
{
public VehicleService VehicleService { get; set; }
public ApprovalParty ApprovalParty { get; set; }
}
In View :
#model Asp_PASMVC.Models.VehicleServiceVewModel
pass ViewModel to partial as below:
#Model.ApprovalParty
In my submit of my login form which is in pop-up it is not hitting the controller even though I provided the code in beginform with post method. I need to send the username and password to my controller for verification. Unable to figure out why it is not hitting.
[HttpPost]
public ActionResult LoginUser(UserInfo objUser)
{
int res = udaObj.CheckUser(objUser.UserName, objUser.Password);
if (res >= 1)
{
return RedirectToAction("Appointment", "Home");
}
else
{
//For testing purpose
return RedirectToAction("Appointment", "Home");
}
}
My view is as follows:
#using (Html.BeginForm("LoginUser", "Home", FormMethod.Post, new { id = "loginForm" }))
{
<div class="tab-pane fade active in" id="signin">
<fieldset>
<!-- Sign In Form -->
<!-- Text input-->
<div class="control-group">
<label class="control-label" for="userid">Alias:</label>
<div class="controls">
#* <input required="" id="userid" name="userid" type="text" class="form-control" placeholder="JoeSixpack" class="input-medium" required="">*#
#Html.TextBoxFor(x => x.UserName, new { #class = "form-control input-large", #placeholder = "Joek#irawath.com", #required = "" , #id="userid" })
</div>
</div>
<!-- Password input-->
<div class="control-group">
<label class="control-label" for="passwordinput">Password:</label>
<div class="controls">
#*<input required="" id="passwordinput" name="passwordinput" class="form-control" type="password" placeholder="********" class="input-medium">*#
#Html.TextBoxFor(x => x.Password, new { #class = "form-control input-large", #placeholder = "********", #required = "", #type = "password" , #id="passwordinput" })
</div>
</div>
<!-- Multiple Checkboxes (inline) -->
<div class="control-group">
<label class="control-label" for="rememberme"></label>
<div class="controls">
<label class="checkbox inline" for="rememberme-0">
<input type="checkbox" name="rememberme" id="rememberme-0" value="Remember me" style="margin-left: 0px">
Remember me
</label>
</div>
</div>
<button id="btnsignin" type="submit" name="signin" class="btn btn-success">Sign In</button>
<!-- Button -->
<div class="control-group">
<label class="control-label" for="signin"></label>
<div class="controls">
#* <button id="btnsignin" type="submit" name="signin" class="btn btn-success">Sign In</button>*#
</div>
</div>
</fieldset>
</div>
}
Check if the object UserInfo is the same model you are using in your view. Try using a FormCollection instead and check.
first time use MVC and i want to change the position and want to change the logo. after this i write the #using
the error is Parser Error what happened ?
#using Angga.Models
#model LoginViewModel
#{ ViewBag.Title = "Log in"; }
<div class="col-sm-4">
</div>
<div class="col-sm-4">
#using (Html.BeginForm("Login", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { #class = "form-horizontal", role = "form"})) { #Html.AntiForgeryToken()
<div class="page animsition vertical-align text-center" data-animsition-in="fade-in" data-animsition-out="fade-out" style="animation-duration: 800ms; opacity: 1;">
<div class="page-content vertical-align-middle">
<div class="panel">
<div class="panel-body">
<div class="brand">
<img class="brand-img" src="~/image/logo-blue.png" alt="..." />
<h2 class="brand-text font-size-18">MYLOYALTY</h2>
</div>
<form method="post" action="customer_search.php" autocomplete="off">
#Html.ValidationSummary(true, "", new {#class = "text-danger"})
<div class="form-group form-material floating">
<!-- Untuk Email-->
<!-- <label class="floating-label">Email</label> -->
<div class="form-group">
#Html.LabelFor(m => m.Email) #Html.TextBoxFor(m => m.Email, new { #class = "form-control" }) #Html.ValidationMessageFor(m => m.Email, "", new { #class = "text-danger" })
</div>
#*
<input style="margin-left: auto; margin-right: auto; text-align: center;" type="email" class="form-control" name="email" />*#
</div>
<!-- Untuk Password-->
<div class="form-group form-material floating">
#Html.LabelFor(m => m.Password) #Html.PasswordFor(m => m.Password, new {#class= "form-control"}) #Html.ValidationMessageFor(m => m.Password, "", new { #class = "text-danger"}) #*
<input style="margin-left: auto; margin-right: auto; text-align: center;" type="password" class="form-control" name="password" />*#
</div>
<div class="form-group clearfix">
<div class="checkbox-custom checkbox-inline checkbox-primary checkbox-lg pull-left">
<input type="checkbox" id="inputCheckbox" name="remember" />
<label for="inputCheckbox">Remember me</label>
</div>
</div>
<button style="" class="btn btn-primary btn-block btn-lg" onclick="location.href='#Url.Action(" About ")'">Sign in</button>
</form>
</div>
</div>
}
<footer class="page-copyright page-copyright-inverse">
<p>© 2016. All RIGHT RESERVED.</p>
</footer>
</div>
</div>
</div>
<div class="col-sm-4">
</div>
#section Scripts { #Scripts.Render("~/bundles/jqueryval") }
and this my code :D, the error is Parser Error
The Error is
enter image description here
this arise for your nested form
because you already introduce a form in html div
#using (Html.BeginForm("Login", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { #class = "form-horizontal", role = "form"})) { #Html.AntiForgeryToken()
........
and you try to introduce another form in the same form
<form method="post" action="customer_search.php" autocomplete="off">
......
I am use MVC Partial view .I have a two of Page AddPoll.cshtml and Listpoll.cshtml. page AddPoll.cshtml use Partial view.
I use AddPoll:partial view page to be placed a partial view with Listpoll.
#Html.Partial("/Areas/admin/Views/Poll/AddPoll.cshtml",new Mvc_baker.Areas.admin.Models.pollquestion());
AddPoll.cshtml
#model Mvc_baker.Areas.admin.Models.pollquestion
<link href="~/Content/bootstrapvalidform.css" rel="stylesheet" />
#using (Html.BeginForm(actionName: "AddPoll", controllerName: "Poll"))
{
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<form class="">
<div class="col-md-6">
<div class="panel panel-primary">
<label class="control-label">پرسش</label>
#Html.TextBoxFor(m => m.QuestionText, new { id = "Name",style="width:450px", #class = "form-control" })
#Html.ValidationMessageFor(m => m.QuestionText)
<input class="btn btn-primary btn-default" name="commit" value="register" type="submit">
</div>
</div>
</form>
}
When I click the register button control action is Not applicable
When the breakpoint action on control and I can not run
You seem to have 2 nested html forms which is invalid markup. The Html.BeginForm helper already generates a <form> tag. So you should get rid of the inner form.
#model Mvc_baker.Areas.admin.Models.pollquestion
<link href="~/Content/bootstrapvalidform.css" rel="stylesheet" />
#using (Html.BeginForm(actionName: "AddPoll", controllerName: "Poll"))
{
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="col-md-6">
<div class="panel panel-primary">
<label class="control-label">پرسش</label>
#Html.TextBoxFor(m => m.QuestionText, new { id = "Name",style="width:450px", #class = "form-control" })
#Html.ValidationMessageFor(m => m.QuestionText)
<input class="btn btn-primary btn-default" name="commit" value="register" type="submit" />
</div>
</div>
}