TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Guest User
Tech Writer
2.1k
473.3k
How to make your controller functional to model and view?
Jan 24 2020 3:13 PM
Hi Team
I want to make my controller functional to my model and view, that the application does correcttly it validates user inputs only. I want my create button, to create new user if not exist to my database and cant seem to make this logic right and need some help and guidance.
Controller:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public
ActionResult Create(CreateModel objSubmit)
{
ViewBag.Msg =
"Details submitted successfully"
;
return
View(objSubmit);
}
// This is for login, and its hits this method each time.
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public
ActionResult Login(Login login)
{
if
(ModelState.IsValid)
{
bool
success = WebSecurity.Login(login.username, login.password,
false
);
var UserID = GetUserID_By_UserName(login.username);
var LoginType = GetRoleBy_UserID(Convert.ToString(UserID));
if
(success ==
true
)
{
if
(
string
.IsNullOrEmpty(Convert.ToString(LoginType)))
{
ModelState.AddModelError(
"Error"
,
"Rights to User are not Provide Contact to Admin"
);
return
View(login);
}
else
{
Session[
"Name"
] = login.username;
Session[
"UserID"
] = UserID;
Session[
"LoginType"
] = LoginType;
if
(Roles.IsUserInRole(login.username,
"Admin"
))
{
return
RedirectToAction(
"AdminDashboard"
,
"Dashboard"
);
}
else
{
return
RedirectToAction(
"UserDashboard"
,
"Dashboard"
);
}
}
}
else
{
ModelState.AddModelError(
"Error"
,
"Please enter valid Username and Password"
);
return
View(login);
}
}
else
{
ModelState.AddModelError(
"Error"
,
"Please enter Username and Password"
);
return
View(login);
}
}
Model:
namespace
eNtsaPortalWebsiteProject.Models
{
public
class
CreateModel
{
[Required]
[StringLength(100, ErrorMessage =
"The {0} must be at least {2} characters long."
, MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name =
"Password"
)]
public
string
password {
get
;
set
; }
[Required]
public
string
username {
get
;
set
; }
}
}
// View for login
<div
class
=
"col-md-12 col-md-offset-10 col-xs-12"
>
<div
class
=
"loginPage panel-info"
>
<span
class
=
""
><i
class
=
"glyphicon glyphicon-user"
>Username:</i></span>
<div
class
=
"form-group text-center"
>
@Html.TextBoxFor(model => model.username,
new
{ @
class
=
"form-control text-center"
, autocomplete =
"off"
})
@Html.ValidationMessageFor(model => model.username)
</div>
<div
class
=
"form-group"
>
<span
class
=
""
><i
class
=
"glyphicon glyphicon-user"
>Password:</i></span>
@Html.PasswordFor(model => model.password,
new
{ @
class
=
"form-control text-center"
, autocomplete =
"off"
})
@Html.ValidationMessageFor(model => model.password)
</div>
</div>
<div
class
=
"form-group"
>
<input id=
"BtnLogin"
type=
"submit"
class
=
"btn btn-success btn-pressure"
name=
"BtnLogin"
value=
"Login"
/>
<input id=
"BntCreate"
type =
"submit"
class
=
"btn btn-info btn-pressure"
name=
"BtnCreate"
value=
"Create"
/>
</div>
</div>
View
for
creating user:
<div
class
=
"col-md-12 col-md-offset-10 col-xs-12"
>
<div
class
=
"glyphicon-registration-mark"
>
<div
class
=
""
><i
class
=
"glyphicon glyphicon-user"
>Username:</i></div>
<div
class
=
"form-group text-center"
>
@Html.TextBoxFor(model=>model.username,
new
{@
class
=
"form-control text-center"
, automplete=
"off"
})
@Html.ValidationMessageFor(model=>model.username)
</div>
<div
class
=
"form-group"
>
<span
class
=
""
><i
class
=
"glyphicon glyphicon-user"
>Password:</i></span>
@Html.PasswordFor(model=>model.password,
new
{@
class
=
"form-control text-center"
, autocomplete=
"off"
})
@Html.ValidationMessageFor(model=>model.password)
</div>
</div>
</div>
<div
class
=
"form-group"
>
<div
class
=
"col-md-offset-2 col-md-10"
>
<input id=
"BtnSubmit"
type=
"submit"
class
=
"btn btn-success btn-pressure"
name=
"BtnSubmit"
value=
"Submit"
/>
</div>
</div>
}
</div>
Reply
Answers (
0
)
code of GPS location
Validate dropdown in bootstrap multistep form