mvc calls httppost always - asp.net-mvc

I am creating a login page:
When I go to the login page Login view should be rendered. on submit, http POST should be triggered which should render the home page with the username. My problem is that whatever I do it is the http GET method that is being called on submit. I am new to mvc ans wondering what I am doing wrong. Why is the program on calling http POST method not submit button click
// My login Class(I mean the model looks like this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace a.Models
{
public class Login
{
public string username { get; set; }
public string password { get; set; }
}
}
// Controller looks like this:
namespace b.Controllers
{
public class LoginController : Controller
{
[HttpPost]
public ActionResult Login(Login Ls)
{
return View();
}
[HttpGet]
public ActionResult Login()
{
return View(new Login());
}
}
}
// Login view look like this
#model a.Models.Login
#{
ViewBag.Title = "Login Page";
}
<html>
<head>
<title>Login Here</title>
<link href="#Url.Content("~/Content/cssLogin.css")" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="bg">
<img src="~/Photos/login2.jpg" />
</div>
<form class="form-2">
<p class="float">
#*<label for="login"><i class="icon-user"></i>Username</label>
<input type="text" name="login" placeholder="Username or email">*#
#Html.LabelFor(model => model.username, new { #class = "icon-use" })
#Html.TextBoxFor(model => model.username, new { data_bind = "value: username" })
</p>
<p class="float">
#*<label for="password"><i class="icon-lock"></i>Password</label>
<input type="password" name="password" placeholder="Password" class="showpassword">*#
#Html.LabelFor(model => model.password, new { #class = "icon-lock" })
#Html.PasswordFor(m => m.password, new { data_bind = "value: password", placeholder = "Password" })
</p>
<p class="clearfix">
<input type="submit" value="Log in">
</p>
</form>
</body>
</html>
// Home View looks like this
#model a.Models.Login
#{
ViewBag.Title = "Home";
}
<h2>Home</h2>
<h2>This is University Home page</h2>
#Model.username

You may need to change the code line this:
public ActionResult Login()
{
return View(new Login());
}
And:
[HttpPost]
public ActionResult Login(Login Ls)
{
return View();
}
And nothing else...

Related

Html.BeginForm() is not returning value to controller [duplicate]

I have a really wierd issue with MVC.
My models keeps getting submitted empty.
And it's probably really simple, but I just can't find the issue.
My model looks like this:
public class LoginModel
{
public string Username;
public string Password;
}
My controller like this:
[HttpGet]
public ActionResult Login()
{
return View();
}
[HttpPost]
public ActionResult Login(LoginModel loginTest)
{
if (loginTest.Username != "x" && loginTest.Password != "y")
{
ModelState.AddModelError("a", "Login failed.");
return View(loginTest);
}
else
{
return RedirectToAction("Home", "Welcome");
}
}
And the view is also very simple, like this.
#model LoginSolution.Models.LoginModel
#{
Layout = null;
}
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Login</title>
</head>
<body>
#using (Html.BeginForm("Login", "Home"))
{
<div> <span>Username : </span>
#Html.EditorFor(model => model.Username)
<br />
<span>Password : </span>
#Html.EditorFor(model => model.Password)
<br />
#Html.ValidationSummary()
<br />
<input type="submit" value="Login" name="Login" />
</div>
}
</body>
</html>
This is not a question about security or best practice.
This is just a question regarding why the model keeps returning itself as empty upon submitting.
Your model contains fields, not properties (no getter/setter) so the model binder cannot set the values. Change your model to
public class LoginModel
{
public string Username { get; set; }
public string Password { get; set; }
}

Why is Model Binding not working in my POST action method?

I have a really wierd issue with MVC.
My models keeps getting submitted empty.
And it's probably really simple, but I just can't find the issue.
My model looks like this:
public class LoginModel
{
public string Username;
public string Password;
}
My controller like this:
[HttpGet]
public ActionResult Login()
{
return View();
}
[HttpPost]
public ActionResult Login(LoginModel loginTest)
{
if (loginTest.Username != "x" && loginTest.Password != "y")
{
ModelState.AddModelError("a", "Login failed.");
return View(loginTest);
}
else
{
return RedirectToAction("Home", "Welcome");
}
}
And the view is also very simple, like this.
#model LoginSolution.Models.LoginModel
#{
Layout = null;
}
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Login</title>
</head>
<body>
#using (Html.BeginForm("Login", "Home"))
{
<div> <span>Username : </span>
#Html.EditorFor(model => model.Username)
<br />
<span>Password : </span>
#Html.EditorFor(model => model.Password)
<br />
#Html.ValidationSummary()
<br />
<input type="submit" value="Login" name="Login" />
</div>
}
</body>
</html>
This is not a question about security or best practice.
This is just a question regarding why the model keeps returning itself as empty upon submitting.
Your model contains fields, not properties (no getter/setter) so the model binder cannot set the values. Change your model to
public class LoginModel
{
public string Username { get; set; }
public string Password { get; set; }
}

Data Validation in Partial view mvc 4?

I am working in MVC 4 and totally new to it. I have two Partial views under shared folder and in index method I am using this code to show these partial views:
#Html.Partial("_Login")
#Html.Partial("_Register")
code of _Login partial view:
<script src="~/Scripts/jquery-1.7.1.min.js"></script>
<link href="~/Content/StyleSheet1.css" rel="stylesheet" />
#using (
Ajax.BeginForm(
"Index", "Partial" ,
new AjaxOptions()
{
UpdateTargetId="LoginClass",
HttpMethod="Post"
}
)
)
{
<div class="loginform">
#Html.Label("Name")
<div>#Html.TextBox("Name")</div>
#Html.Label("Password")
<div>#Html.TextBox("password")</div>
</div>
<div>
<input type="submit" value="submit" />
</div>
}
code of LoginClass:
namespace PartialViews.Models
{
public class LoginClass
{
[Required(ErrorMessage="Required")]
public string Name { get; set; }
[Required]
public string Password { get; set; }
}
}
Controller action for Login:
[HttpGet]
public ActionResult Index()
{
var lc = new LoginClass();
return View(lc);
}
[HttpPost]
public string Index(LoginClass lc)
{
return "Email: " + lc.Name + "</br>" +"Password:" + lc.Password;
}
But validations are not working! What I need to do to make validations work.
Need help, Thanks in advance.
Do something like this:
[HttpPost]
public string Index(LoginClass lc)
{
if(ModelState.IsValid)
{
return "Email: " + lc.Name + "</br>" +"Password:" + lc.Password;
}
else
{
return PartialView("yourview",lc);
}
}
and in view:
<div id="formContainer">
#using (
Ajax.BeginForm(
"Index", "Partial" ,
new AjaxOptions()
{
UpdateTargetId="LoginClass",
HttpMethod="Post",
InsertionMode = "Replace"
}
)
)
{
#Html.ValidationSummary(true)
<div class="loginform">
#Html.Label("Name")
<div>#Html.TextBoxFor(x=>x.Name)</div>
<div>#Html.ValidationMessageFor(x=>x.Name)</div>
#Html.Label("Password")
<div>#Html.TextBox(x=>x.password)</div>
<div>#Html.ValidationMessageFor(x-=>x.password)</div>
</div>
<div>
<input type="submit" value="submit" />
</div>
}
</div>
on top of view set model for view like this:
#model LoginClass
Update 2:
Create login form as partial view:
#using (
Ajax.BeginForm(
"Index", "Partial" ,
new AjaxOptions()
{
UpdateTargetId="formContainer",
HttpMethod="Post",
InsertionMode = "Replace"
}
)
)
{
#Html.ValidationSummary(true)
<div class="loginform">
#Html.Label("Name")
<div>#Html.TextBoxFor(x=>x.Name)</div>
<div>#Html.ValidationMessageFor(x=>x.Name)</div>
#Html.Label("Password")
<div>#Html.TextBox(x=>x.password)</div>
<div>#Html.ValidationMessageFor(x-=>x.password)</div>
</div>
<div>
<input type="submit" value="submit" />
</div>
}
Load it in main view inside container div:
<div id="formContainer">
// Load you login form here as partial view
</div>
Now your action:
[HttpPost]
public string Index(LoginClass lc)
{
if(ModelState.IsValid)
{
// do anything here if valid data
}
else
{
// data not valid
return PartialView("partialviewlogin",lc);
}
}

MVC3 View doesn`t display empty form on validation passed

Very simple situation, here is my model:
public class Comment
{
[Required]
public string Name { get; set; }
[Required]
public string Text { get; set; }
}
Here is my controller:
public class CommentController : Controller
{
//
// GET: /Comment/Create
public ActionResult Create()
{
return View();
}
//
// POST: /Comment/Create
[HttpPost]
public ActionResult Create(FormCollection collection)
{
Comment new_comment = new Comment();
if (TryUpdateModel(new_comment))
{
return View(new Comment()); //problem is there
}
else
{
return View(new_comment);
}
}
}
And here is my standart genered strongly-typed View:
#model test.Models.Comment
#{
ViewBag.Title = "Create";
}
<h2>Create</h2>
<script src="#Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="#Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
#using (Html.BeginForm()) {
#Html.ValidationSummary(true)
<fieldset>
<legend>Comment</legend>
<div class="editor-label">
#Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Name)
#Html.ValidationMessageFor(model => model.Name)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Text)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Text)
#Html.ValidationMessageFor(model => model.Text)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
#Html.ActionLink("Back to List", "Index")
</div>
Problem is: when I enter valid data, TryUpdateModel() returns true, and
return View(new Comment());
should display empty form, because new empty instance of Comment is passed, but it still displays form with values entered earlier.
Please somebody tell me why. How to make it display empty form again?
Clear the modelstate:
if (TryUpdateModel(new_comment))
{
ModelState.Clear();
return View(new Comment()); //no more problem here
}
All HTML helpers first look at the ModelState when rendering their values and only after that in the model.
Or even better and more properly use the Redirect-After-Post pattern (i.e. redirect after a successful POST):
if (TryUpdateModel(new_comment))
{
return RedirectToAction("Create");
}

connecting an action of button in view in asp.net mvc3

I am beginner in asp.net mvc . I am going to connect an action of button in view.
but i cant. i work with web form, in fact i want to click on the button, create action will be called and insert the data.
my code is as following:
The Controller:
namespace BookStore.Controllers
{
public class BookController : Controller
{
public ActionResult Create()
{
return View();
}
[HttpPost]
public ActionResult Create(book bookobj)
{
var dBook=new DBook();
dBook.Insertbook(bookobj);
return RedirectToAction("Index", "Home");
}
}
}
The View:
#model BookStore.Models.DomainObject.book
#{
ViewBag.Title = "Create";
}
<h2>insert data/h2>
#using(Html.BeginForm())
{
#Html.ValidationSummary(true);
<fieldset>
<div>
#Html.LabelFor(model=> model.book_name)
</div>
<div>
#Html.EditorFor(model => model.book_name)
</div>
<div>
#Html.LabelFor(model=>model.book_qty)
</div>
<div>
#Html.EditorFor(model=>model.book_qty)
</div>
<br/>
<div>
<input id="Button_craete_book" type="button" value="insert" />
</div>
</fieldset>
}
Change the button type to "submit":
<input id="Button_craete_book" type="submit" value="insert" />
That will post the form and values to the Edit method in the controller, as marked with the HttpPost attribute.

Resources