Control session variable inside MVC view - asp.net-mvc

I am developing MVC application where people can sign up. After signing up, a page will display confirmation message using bootstrap modal. The modal block on the view is checking a session variable first, if the session is not null, then it will be displayed:
#if (Session["signUpName"] != null)
{
<!-- Modal start -->
<script type="text/javascript">
$(window).load(function(){
$('#signUpModal').modal('show');
});
</script>
<div class="modal fade" id="signUpModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data- dismiss="modal">×</button>
<h4 class="modal-title">Registration Confirmation</h4>
</div>
<div class="modal-body">
<p>Dear #Session["signUpName"]</p>
<p>Thank you ......</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data- dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<!-- Modal end -->
}
My issue is after signing up, I can't get rid of the modal window!! I tried to make the session equals to null inside the if statement with no luck.
Any ideas?

Don't use Session for this. The whole point of MVC is to use Models for your views.
So, create a model class to hold whatever data you need for the view. The moment you start using session and think about clearing it in the view you've gone as far away from MVC as you can get. This isn't webforms or classic asp, you need to think in a different way.
So, you could create your model from Session values. If you need to clear the session, do it in the controller, before you load the view. Keep your view as dumb and as simple as possible as you can.
public class UserModel {
public string Username { get set }
}
in the controller you take care of populating the model
public ActionResult SomeAction()
{
model = new UserModel { Username = Session["someSession"] == null ? "" : Session["someSession"].ToString() }
return View(model);
}
In your view I would not combine razor and Javascript like that. You can assign values to JavaScript variables from a model
so, in JavaScript you could do something like this:
var username = '#Model.Username';
now you can continue with your js code and check if you have a value or not.

Related

MVC configure using Modal bootstrap

I am using a bootstrap modal in the header - layout page for the entire site
e.g. original code from bootstap
<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
Launch Login demo modal
</button>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-body">
Login information
</div>
</div>
</div>
</div>
The above code does work.
I modified the button code to link
Launch login demo modal 2
When user clicks on the link the code works, but now the user has access to the url link and could open in a new window/new tab
When user right clicks on the link and choose "open in a new tab" it would fail because this page does not open.e.g.
http://example.com/account/login#myModal
http://example.com/blog#myModal
Question : Is it possible if user opens in a new tab the url, redirect to
http://example.com/account/login page
a live example fiverr.com (click on join a modal opens) and can also access direct link fiverr.com/join I am using MVC, maybe using a route configurations?
You cat do it with JavaScript. Just get the part of URL after hash and check if it is what you are expected, and then open modal manually:
if(window.location.hash) {
var hash = window.location.hash.substring(1);
if (hash == "myModel"){
$('#myModal').modal('show');
}
}
Make sure, that code is being executed after page is loaded.

Not able to change Bootstrap CSS with ViewBag

I'm sure there are multiple ways of accomplishing this and I'm open to any suggestions but at the moment I'm trying to find a way to dynamically change a Bootstrap alert based on the CSS I'm passing in the ViewBag.
When I try the following it doesn't display the Bootstrap alert at all. Basically what I'm looking to do is have a generic alert at the top of a View. Then I'll look at the ViewBag for a message and if that is not Null I will display it. If it's displayed I would like to look at the CSS so that way I can either pass an error message or a success message. I can see the ViewBag results with the following code but it doesn't show the Bootstrap alert at all.
This is in the Controller
ViewBag.ResultMessage = "Just testing.";
ViewBag.ResultMessageCss = "alert-danger";
This is in my View
#if (ViewBag.ResultMessage != null)
{
<div class="alert alert-dismissible #ViewBag.ResultMessageCss;">
<button type="button" class="close" data-dismiss="alert">&close;</button>
#ViewBag.ResultMessage;
</div>
}
Your code is going to render the below HTML.
<div class="alert alert-dismissible alert-danger;">
<button type="button" class="close" data-dismiss="alert">close</button>
Just testing.;
</div>
The last CSS class name you have is alert-danger; That is not a valid bootstrap class name. It should be alert-danger. So if you remove the unnecessary semicolon, it will properly render the bootstrap message box.
#if (ViewBag.ResultMessage != null)
{
<div class="alert alert-dismissible #ViewBag.ResultMessageCss">
<button type="button" class="close" data-dismiss="alert">close</button>
#ViewBag.ResultMessage
</div>
}

How do I link to a modal dialog from an external link?

My website has a payment form in a modal. All I want is (for some specific customers) to pass them a link and when they click on it to move to the website with the modal already be open.
Is that possible?
You must facilitate the feature yourself. Lets say you have normal bootstrap modal like this :
<div class="modal fade" id="payment">
<div class="modal-dialog">
<div class="modal-content">
...
</div>
</div>
</div>
and you want to invoke the modal on page load in a link sent to some users :
http://www.example.com#payment
Then add this code snippet to your page :
$(document).ready(function() {
var modals = ['#payment', '#anotherModal'];
if (window.location.hash && ~modals.indexOf(window.location.hash)) {
$(window.location.hash).modal();
}
})
This is of course by far the most simple way to do it.

Login MVC redirect to another page

I have a log-in page. When the user hits the log-in button the button will calla another action method. that action method will have two parameter. the user provided userid and password. then it do some validation and redirect to another action method according to the outcome. I am struggling with there. i guess this is pretty simple. i am new to MVC. Any help would be appriciated.
View
<div class="container-fluid">
<div class="starter-template">
<h1>Policy Assessment Tool</h1>
<p class="lead">Are You Following Right?</p>
<button type="button" class="btn btn-primary btn-lg" onclick="location.href='#Url.Action("Create","UserDatas")'">Register for a An Account</button>
<h3><strong>OR</strong></h3>
</div>
<div class="row">
<form class="form-signin" role="form" method="post">
<div class="col-lg-4"></div>
<div class="col-lg-4">
<h2 class="form-signin-heading">Please Sign-In to Continue</h2>
<div class="input-group">
<input type="text" name="input_domain_id" class="form-control text-center" placeholder="Domain ID" autofocus required>
<input type="password" name="input_domain_password" class="form-control text-center" placeholder="Domain Password" required>
<div class="col-lg-12">
<p><button class="btn btn-lg btn-primary btn-block" type="submit" onclick="location.href='#Url.Action("Verify_Login", "Ldap_Login_Verify")'">Login</button></p>
</div>
</div><!-- /input-group -->
</div><!-- /.col-lg-4 -->
<div class="col-lg-4"></div>
</form>
</div><!-- /.row -->
</div>
Controller
[HttpPost]
public ActionResult Verify_Login(string input_domain_id, string input_domain_password)
//Log-in Logic
return View("Success")
Instead of using form tag use Html.BeginForm HtmlHelper and pass Model from Controller to View for the best practice. Here is link for you to get Getting Started With MVC5.
Based on the mark up above you need not worry about it, When the form is posted the values will get bound to the parameters of the action method, As the parameter names are similar to the form field names.
The above concept is called model binding. The below two links should give you a good idea on the same.
http://www.codeproject.com/Articles/710776/Introduction-to-ASP-NET-MVC-Model-Binding-An-Absol
http://dotnetslackers.com/articles/aspnet/Understanding-ASP-NET-MVC-Model-Binding.aspx
I have tried to compile a simple example below.
Lets assume there is a view like below.
#model string
#{
ViewBag.Title = "Injection";
}
<h2>Injection</h2>
#using(Html.BeginForm())
{
<div id="formDetails">
#Html.TextBox("UserName")
</div>
<div>
<input type="submit" id="btnTest" value="Click Me" />
</div>
}
The Action method for the same would be like
[HttpPost]
public ActionResult Injection(string UserName)
{
return View("Injection");
}
So when i click on the submit button, The form is posted , Since the ViewName is injection, and there is a corresponding action method called injection it will automatically hit that action method. Since the parameter name of the method and field is the same, What ever value is there in the username textbox will get automatically bound to the method parameter.

Rendering Grails content in Twitter Bootstrap modal

I am trying to render the outcome of an action into a modal (twitter bootstrap). Unfortunately I do not get this to work.
Before I was generating a link within an each iterator:
<g:link action="perform" id="${exerciseInstance.id}">
<h2>${fieldValue(bean: exerciseInstance, field: "title")}: (${exerciseInstance.questions.size()} Questions)</h2>
</g:link>
Instead of rendering a complete new site I rather want the quiz to be presented in a modal. Therefore I tried a simple twitter bootstrap modal example:
<a data-toggle="modal" href="#myModal" class="btn btn-primary btn-large">${fieldValue(bean: exerciseInstance, field: "title")}</a>
<div id="myModal" class="modal hide fade" style="display: none; ">
<div class="modal-header">
<h3>Test</h3>
</div>
<div class="modal-body">
--> This is where the content should go <--
</div>
<div class="modal-footer">
Close
</div>
</div>
What is the best way to achieve this?
Asking for the "best way" on SO is a dangerous game. There probably isn't a best. Just different approaches. I'll give you one that I use utilizing jQuery's $.load() function.
$("#myModal .modal-body").load(url);
It really is that simple. Obviously, adjust your load() function if you need to pass in parameters, provide a callback function, etc. Your controller's action would just render a template containing the HTML you want in your modal-body. This isn't really even Grails specific. This approach would work with any server side tech.

Resources