Programming Time Articles for your daily pleasure!
form validation

I created new project and I did setup routes and other necessary functions, now it was turn for form authentication.
I follow up all necessary steps, required to validate the user like creating cookie etc like below:

Did build project, succeeded without any error,  now its time to test it. I used simple method to test the if request is authenticated or not using following simple way:

What was the problem?

But condition was false and It didn’t happen to take user to dashboard page, I double checked the code and everything was fine. After wasting an hour eventually I went to web.config
where I found I forgot to add authentication method, I didn’t tell system how I manage to validate the user.

Solution (whew)

So solution was very simple, we just need to define authenticate method in web.config and it should work.

Again did build the solution and yeah now its working perfectly fine 🙂

code

Viewstate might be very useful during Asp.net application, it can hold the value until you are on the page means when you redirected to another page all saved value will be removed.

Its very easy to use viewstate value on code behind, but if you will try to get on client side it might not be simple using jquery, so today I will show you How to use viewstate value on client side in asp.net

How we can easily get value from viewstate at client side

The way I am using is very simple, I am not going to get value using hidden field, you might know, each viewstate key will be created as new hidden field starting with underscore ( _ ). You don’t need to bother about it.

I used server side code on client side in order to get value from Viewstate, its very simple to use.
Example are as follows.

You can see, I first initialize Total key with value 10 in Veiwstate. After that I am alerting on client side. You can see how easy it is.

If you have any question feel free to ask.

In this post I have described the problem and solution of the Dropdown not displaying selected value in case of list of model

Sometimes during programming we have some cases where we need to use list of model. There can be many examples like we need to save multiple user bank accounts or credit cards or we can take example of # of cars during policy creation process etc. So there can be many scenarios according to our business requirements.

How I can fix this?

You might noticed one thing that all input and other fields like textarea works very well they does show correct value what the user actually fill out but there is one problem like I said above all input and other fields works instead one field that is dropdown, you heard correct, dropdown.

I noticed all fields were displaying correct values what the user previously entered but the problem with dropdown was its was not displaying the selected value rather just behaving like user didn’t ever select the value but the interesting thing I noticed in the code behind, I take a look at the model object at the httppost method, I was getting the correct value but it was just not displaying on the page.

After researching this issue, eventually I came with this solution that was instead creating Selectlist from controller I have to create in on the cshtml page. This is the only trick which worked for me.

I don’t know why this issue was caused but this trick did his work, following is the code.

Solution

MVC

Hello guys, today we’ll learn about How we can auto select menu using Jquery.

The most common functionality in almost all websites is that to show selected menu which was previously clicked. There are many ways to do this, either we can achieve this using server side language or we can use jQuery or javascript(Almost all browsers supports javascript by default). So its up to us, how we want to integrate this.

I will use jQuery  and almost each website use this for having image slider, hierarchical dropdown etc which not possible without using jQuery.

So let’s start doing code, before coding let’s think, how we can do this.

The Idea, I am using is very simple, what I will do, I will get the page name from the browser URL and compare with each menu. isn’t simple?

menu
menu

On page load I will call one function that will get page name from URL and one by one it will compare that page name with each menu and if menu matches with page name then we can add class that is responsible to add styles to make menu look like selected, following is the code that will auto select the menu item.