Tuesday, March 15, 2016

Pass A Data Model Between Controller and View

Hi friends,
Today i give you a small lesson in Asp.net MVC.

First of all we get the controller action.

In above action some codepart was missing. So you have to start with your own action. So, you can see at the end of the page i have passed the model to "_ToDoList_LoadActions" view.
It is not enough and you have to catch the model from view as below.

You can see top of the _ToDoList_LoadActions.cshtml file as "@model US.BOX.CreditorPortal.Web.Model.MainActionModel". That's the way to catch the model which are coming from controller to view. But be careful because "MainActionModel" is the type of the data model.

Now you can operate the data model using razor.

* Now i move to other one. That is Passing a data model to controller from view.

As above image we have to encode the data model using json and pass to JavaScript function. Here it is "report()" .

So in JavaScript function, data model is passed to controller as below.

Now data model is captured from controller as below.

I think you can practice this with your own actions. 

Bye.. Happy Coding!

Thursday, February 25, 2016

How to Use RDLC Report in ASP.net MVC

Hi Friend,

Today i am going to post my latest experience in C sharp usage.
Let's start.

now we are going to display some data set that is from anywhere on the view using RDLC report.

What is the RDLC ?

Full meaning of RDLC is Report Definition Language Client-side.
The .rdlc is the format of a report file used by the Microsoft reporting system. Mainly Microsoft Visual Studio creates this rdlc file extension that contain report definitions.

I am sure you can get nice idea about this when you go to the bottom of this page. I recommend first of all read the post to the bottom before do it.

I use asp.net MVC solution. Basic solution structure as follow.

Now i will show the path from button click from the view to displaying the rdlc report on the view.

First we have to add rdlc report to the solution as below.

Now we have to do pass the data set to report and view. But we can't do this in MVC without .aspx file. we have to use an aspx file.

* So i will create new .aspx file as below.

Now we have to design this .aspx file. following is the example.

Ex :-

You can see the path to .rdlc file have been given in the code.

Below is the controller action

Then we have to create above _RemmitReportResult.cshtml file and  we call the .aspx file that we have created before from this _RemmitReportResult.cshtml file as below.

Thats all. Now you have to call the controller action to display the report.

I will show how i did that but you can do it as you want.
I did it using ajax call .

Do it soon.
Happy coading, Bye!

Monday, August 10, 2015

Validate and Put a Mask to Currency Field Using JavaScript

Today i am going to explain how to put a mask to currency field using java script from the client side.
Here i get to use "Norwegian krone".

First of all we get the HTML input field as below.

<input type="text" class="form-control text-right" name="amount">

Now we validate above field using JavaScript. here we catch the input field using its name but you can select id also.

$("[name='amount']").on('keydown', function (e) {
    // tab, esc, enter
    if ($.inArray(e.keyCode, [9, 27, 13]) !== -1 ||
        // Ctrl+A
        (e.keyCode === 65 && e.ctrlKey === true) ||
        // home, end, left, right, down, up
        (e.keyCode >= 35 && e.keyCode <= 40)) {
    // backspace & del
    if ($.inArray(e.keyCode, [8, 46]) !== -1) {

    var a = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "`"];
    var n = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"];

    var value = $(this).val();
    var clean = value.replace(/\./g, '').replace(/,/g, '').replace(/^0+/, '');
    var charCode = String.fromCharCode(e.keyCode);

    var p = $.inArray(charCode, a);
    if (e.keyCode > 47 && e.keyCode < 58) {
        p = String.fromCharCode(e.keyCode);
    } else if (p !== -1) {
        p = n[p];

    if (p !== -1) {
        value = clean + p;
        if (value.length == 2) value = '0' + value;
        if (value.length == 1) value = '00' + value;

        var formatted = '';
        for (var i = 0; i < value.length; i++) {
            var sep = '';
            if (i == 2) sep = ',';
            if (i > 3 && (i + 1) % 3 == 0) sep = '.';
            formatted = value.substring(value.length - 1 - i, value.length - i) + sep + formatted;

If you want to do something with backspace and delete buttons you can do that. Here i comment it.

So. get it your front end validation purposes and put a mask for currency filed.
Bye..Happy coding!

Tuesday, July 28, 2015

Validate Input: Number Field Using JQuery

Now, i am going to validate number field using JQuery.

First of all we get the HTML part as below .

Number : <input type="text" name="number" id="id" />&nbsp;<span id="error"></span>

JQuery for the validation the field.

$(document).ready(function () { 
  $("#id").keypress(function (e) {    
     if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {      
        $("#error").html("Numbers Only").show().fadeOut("slow");
               return false;

Then if you press the charachter like 'a' or '/' or something that is not in the number set (0-9), it will display the error message.

Bye..Happy coding!

Tuesday, July 14, 2015

Log Off and Solve Back Button Issue

In this post i explain how to log off and solve back button issue after logout in browser ASP .Net MVC web application.

* View

<a href="@Url.Action("LogOut","Account")" class="cc-font-min">Log out</a>

* Controller

public ActionResult LogOut()
    return RedirectToAction("Index", "Home");

Here, after click the logout link it will call to above LogOut Action and then session is destroyed.

Then you will log out. But using browser back button we can come to back steps and then we can continue even we logged out.

So to solve this problem we can use "[Authorize] " at the top of the every actions or class like below examples.

public class CaseViewController : Controller

public ActionResult Index(int task)

Try it when you are implement logout function.
Bye.. Happy Coding!

Monday, July 13, 2015

File Uploading in ASP .Net MVC

In this post i am going to explain that how to upload a file to the server in asp .net mvc web application.

first of all we get the view part to explain. Actually explaining is not set of words. it is a human readable coding. It is very easy.

<form action="../FileUpload/UploadItemView" method="post" enctype="multipart/form-data">
    <input type="file" accept=".xml,.txt" name="files"/>
    <input type="file" accept=".xml,.txt" name="files"/>   
    <input type="submit" id="submitForm" onclick="return validateSubmit()"/>

Above is a HTML form. After select the files we can upload them using submit button. if we want to validate before upload we can use Jquery or JavaScript function like "validateSubmit()".
Now  we move to controller with uploaded files. :)

public ActionResult UploadItemView(IEnumerable<HttpPostedFileBase> files)
    _creditorId = Crypto.Decrypt(Convert.ToString(Session["UserId"]));
    string targetPath = ConfigurationManager.AppSettings["UploadPath"];

    if (!Directory.Exists(targetPath))

    var tempUploadFileDtoList=new List<UploadFileDTO>();
    _serviceObject=new USCPService();
    foreach (var file in files)
        if (file != null)
            var fileName = Path.GetFileName(file.FileName);
            if (fileName != null)
                var path = Path.Combine(targetPath, fileName);
                var tempUploadFileDto = new UploadFileDTO {FilePath = fileName};
    return View();

Using above action, we can upload files.

Bye.. Happy Coding!

Sunday, July 12, 2015

Add a List to the Session in ASP .Net MVC

In this post i am going to explain how to add a list to session in asp.net mvc web application.
Actually it is a RAZOR syntax in .cshtml file.

@if (@Model.UserModel.NewAnnouncements.Count > 0)
    List<int> idList = new List<int>();
    var numberOfAnnouncement = 1;
    foreach (var newAnnouncement in @Model.UserModel.NewAnnouncements)
        var tempList = (List<int>)Session["ListOfId"];
        if (tempList == null || tempList.All(id => id != newAnnouncement.ID))
            <div class="modal-header">
                <button type="button" class="close cc-modal-close">
                    <span aria-hidden="true">&times;</span>
                <h2 class="modal-title">@newAnnouncement.Header</h2>
            <div class="modal-body">                           
            Session["ListOfId"] = idList;

So, above code may have unwanted things for you. please ignore them.

Good bye.. Happy coding!
UnicornSLI . Powered by Blogger.