European ASP.NET MVC 4 and MVC 5 Hosting

BLOG about ASP.NET MVC 3, ASP.NET MVC 4, and ASP.NET MVC 5 Hosting and Its Technology - Dedicated to European Windows Hosting Customer

ASP.NET MVC 6 Hosting - HostForLIFE.eu :: Moving Your Data From ListBox to other ListBox with JQuery in ASP.NET MVC

clock October 30, 2015 00:48 by author Peter

Now, we will discussed about Moving Your Data From ListBox to other ListBox with JQuery in ASP.NET MVC. A list box is a graphical control element that allows the user to select one or more items from a list contained within a static, multiple line text box. First step, write the following code:
namespace Mvc2.Controllers
{
public class MovieController : Controller
{
    public ActionResult MoveDateinListBox()
  {
      return View();
  }
  }
}

In View
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
MoveDateinListBox
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h5>
    MVC:How to Move data between two ListBoxes using JQuery</h5>
<script language="javascript" type="text/javascript" src="http://code.jquery.com/jquery-1.4.1.min.js"></script>
<script language="javascript" type="text/javascript">
    $(function () {
        $("#MoveToRight,#MoveToLeft").click(function (event) {
            var id = $(event.target).attr("id");
            var selectFrom = id == "MoveToRight" ? "#SelectLeftItem" : "#SelectRightItem";
            var moveTo = id == "MoveToRight" ? "#SelectRightItem" : "#SelectLeftItem";
            var selectedItems = $(selectFrom + " option:selected").toArray();
            $(moveTo).append(selectedItems);
            selectedItems.remove;
        });
    });
</script>
<form method="get" action="" runat="server">
 <select id="SelectLeftItem" multiple="multiple" style="height: 100px">
    <option value="1">ASP.NET</option>
    <option value="2">C#.NET</option>
    <option value="3">SQL Server</option>
    <option value="4">VB.NET</option>
</select>
<input id="MoveToRight" type="button" value=" >> " style="font-weight: bold" />
<input id="MoveToLeft" type="button" value=" << " style="font-weight: bold" />
<select id="SelectRightItem" multiple="multiple" style="height: 100px">
    <option value="1">MVC</option>
    <option value="2">WCF</option>
    <option value="3">WPF</option>
    <option value="4">Silverlight</option>
</select>
</form>
</asp:Content>


And here is the output:

HostForLIFE.eu ASP.NET MVC 6 Hosting
HostForLIFE.eu is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes. We have customers from around the globe, spread across every continent. We serve the hosting needs of the business and professional, government and nonprofit, entertainment and personal use market segments.



ASP.NET MVC 6 Hosting France - HostForLIFE.eu :: How To Clear Thumbnails After It Has Uploaded

clock October 24, 2015 00:23 by author Rebecca

Sometimes, we need to clear the thumbnails from dropzone.js after the file is uploaded. In this article, I will tell you how to do it.

You need to call addedfile function once the file is uploaded. After that, you have to to generate remove button for the thumbnail.

Look at this example code:

Step 1: File Upload response from the server

Dropzone.options.dropzoneForm = {
maxFiles: 2,
init: function () {
this.on("maxfilesexceeded", function (data) {
var res = eval('(' + data.xhr.responseText + ')');

});
this.on("addedfile", function (file) {

Step 2: Create the remove/clear button

var removeButton = Dropzone.createElement("
<button>Remove file</button>
");

Step 3: Capturing the Dropzone.js instance as closure

var _this = this;

Step 4: Listen to the click event

removeButton.addEventListener("click", function (e) {

Make sure the button click doesn't submit the form:

e.preventDefault();
e.stopPropagation();

Step 5: Remove the file preview

_this.removeFile(file);

If you want to the delete the file on the server as well, you can do the AJAX request here:

});

Step 6: Add the button to the file preview element

file.previewElement.appendChild(removeButton);
});
}
};

That's it! Simple right?

HostForLIFE.eu ASP.NET MVC 6 Hosting
HostForLIFE.eu is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes. We have customers from around the globe, spread across every continent. We serve the hosting needs of the business and professional, government and nonprofit, entertainment and personal use market segments.

 



ASP.NET MVC 6 Hosting Italy - HostForLIFE.eu :: How to Use Dapper.NET ORM in ASP.NET MVC ?

clock October 22, 2015 23:36 by author Peter

In this tutorial, let me explain you how to use Dapper.NET ORM in ASP.NET MVC 6. Dapper is a simple object mapper for .NET. Dapper is a file you can drop in to your project that will extend your IDbConnection interface. A key feature of dapper is performance. the subsequent metrics show how long it takes to execute five hundred select statements against a db and map the data returned to objects.

Now,  Install dapper using Nuget Package Manager
PM> Install-Package Dapper

After that, Create a project in ASP.NET MVC and then Add a folder named Dapper inside it as you can see on the following picture:

Next step: Create User and Address classes
public class Address
{
public int AddressID { get; set; }
public int UserID { get; set; }
public string AddressType { get; set; }
public string StreetAddress { get; set; }
public string City { get; set; }
public string State { get; set; }
public string ZipCode { get; set; }
}

public class User
{
public User()
{
this.Address = new List<Address>();
}

public int UserID { get; set; }

public string FirstName { get; set; }

public string LastName { get; set; }

public string Email { get; set; }

public List<Address> Address { get; set; }
}

Now Create IUserRepository.cs interface  and UserRepository.cs classes for data access.
public interface IUserRepository
{
List < User > GetAll();
User Find(int id);
User Add(User user);
User Update(User user);
void Remove(int id);
User GetUserInformatiom(int id);
}
public class UserRepository : IUserRepository
{
private IDbConnection _db = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
public List<User> GetAll()
{
return this._db.Query<User>("SELECT * FROM Users").ToList();
}

public User Find(int id)
{
return this._db.Query<User>("SELECT * FROM Users WHERE UserID = @UserID", new { id }).SingleOrDefault();
}

public User Add(User user)
{
var sqlQuery = "INSERT INTO Users (FirstName, LastName, Email) VALUES(@FirstName, @LastName, @Email); " + "SELECT CAST(SCOPE_IDENTITY() as int)";
var userId = this._db.Query<int>(sqlQuery, user).Single();
user.UserID = userId;
return user;
}
public User Update(User user)
{
var sqlQuery =
    "UPDATE Users " +
    "SET FirstName = @FirstName, " +
    "    LastName  = @LastName, " +
    "    Email     = @Email " +
    "WHERE UserID = @UserID";
this._db.Execute(sqlQuery, user);
return user;
}

public void Remove(int id)
{
throw new NotImplementedException();
}

public User GetUserInformatiom(int id)
{
using (var multipleResults = this._db.QueryMultiple("GetUserByID", new { Id = id }, commandType: CommandType.StoredProcedure))
{
    var user = multipleResults.Read<User>().SingleOrDefault();

    var addresses = multipleResults.Read<Address>().ToList();
    if (user != null && addresses != null)
    {
        user.Address.AddRange(addresses);
    }

    return user;
}
}
}


Now use the above repository in the HomeController.cs
Create an instance for UserRepository class
private IUserRepository _repository = new UserRepository();

For Get All User write the following code:
public ActionResult Index()
{
return View(_repository.GetAll());
}

HostForLIFE.eu ASP.NET MVC 6 Hosting
HostForLIFE.eu is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes. We have customers from around the globe, spread across every continent. We serve the hosting needs of the business and professional, government and nonprofit, entertainment and personal use market segments.

 



ASP.NET MVC 6 Hosting France - HostForLIFE.eu :: How to Avoid XSS (Cross Site-Scripting) in MVC Project

clock October 16, 2015 11:09 by author Rebecca

Cross-site scripting (XSS) is a type of computer security vulnerability typically found in web applications. XSS enables attackers to inject client-side script into web pages viewed by other users. Cross-Site Scripting (XSS) attacks are a type of injection, in which malicious scripts are injected into otherwise trusted web sites. In this article, I will show you how to avoid XSS while allowing only the HTML that you want to accept. In example, only <b> and <u> tags.

Firstly, let's filter the user input, and accept only <b></b> and <u></u> tags.

Step 1: Disables input validation

Step 2: Encodes all the input that is coming from the user

Step 3: Replace the encoded html with the HTML elements that you want to allow

Here is the full code snippet:

[HttpPost]
// Input validation is disabled, so the users can submit HTML
[ValidateInput(false)]
public ActionResult Create(Comment comment)
{
    StringBuilder sbComments = new StringBuilder();
   
    // Encode the text that is coming from comments textbox
    sbComments.Append(HttpUtility.HtmlEncode(comment.Comments));
   
    // Only decode bold and underline tags
    sbComments.Replace("&lt;b&gt;", "<b>");
    sbComments.Replace("&lt;/b&gt;", "</b>");
    sbComments.Replace("&lt;u&gt;", "<u>");
    sbComments.Replace("&lt;/u&gt;", "</u>");
    comment.Comments = sbComments.ToString();

    // HTML encode the text that is coming from name textbox
    string strEncodedName = HttpUtility.HtmlEncode(comment.Name);
    comment.Name = strEncodedName;

    if (ModelState.IsValid)
    {
        db.Comments.AddObject(comment);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    return View(comment);
}

This is just one example. Only filtering the user input can't guarantee XSS elimination. XSS can happen in different ways and forms.

Hope you did it!

HostForLIFE.eu ASP.NET MVC 6 Hosting
HostForLIFE.eu is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes. We have customers from around the globe, spread across every continent. We serve the hosting needs of the business and professional, government and nonprofit, entertainment and personal use market segments.



ASP.NET MVC 6 Hosting - HostForLIFE.eu :: How to Use Different Actions to Show A Single View

clock October 2, 2015 12:39 by author Rebecca

In this article, you will learn how use different actions to show a single view in ASP.NET MVC.  It’s common situation in project development where you don’t want to create view for each and every action of certain controller. In this situation, we have to use concept of shared view. The solution is very simple, you just have to keep the view in shared folder. Like below:

Now, The question is why need to keep in shared folder? The reason is when you run any controller, by default it check its own directory or shared directory. Every controller will look in shared directory, if it not available in it’s own directory. Let’s have a look on below code, let's create very simple controller class:

sing System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
 
namespace MVC3.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/
        public ActionResult Action1()
        {
          
            ViewBag.Controller = "Action1";
            return View("_Common");
        }
        public ActionResult Action2()
        {
          
            ViewBag.Controller = "Action2";
            return View("_Common");
        }
 
    }

Both Action1() and Action2() is calling _Common view and as the view is placed in shared folder, both can able to access. Before calling to view we are assigning action name in ViewBag. From view we can detect which action has invoked it. Here is code for view:

<%@ Page
Language="C#"
Inherits="System.Web.Mvc.ViewPage<MVC3.Models.customer>"
%>
<!DOCTYPE html>
<html>
<head runat="server">
    <title>_Common</title>
</head>
<body>
    <div>
        <% var ActionName = ViewBag.ActionName; %>
        <% if (ActionName == "Action1")
           {%>
          
              This view is called from Action 1
           <%}
           else
           {%>
          
              This view is Called from Action 2
           <%} %>
    </div>
</body>
</html>

 
Here is the output:

Now, the other question my show “Is it possible to call one view from different controller”? Yes, you can call. In below example, you will see how to do that.

You can call one view from different controller. This is your first controller Home1:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
 
namespace MVC3.Controllers
{
    public class Home1Controller : Controller
    {
        public ActionResult Action()
        {
          
            ViewBag.Controller = "Home1";
            return View("_Common");
        }
    }
}

And here is the output.



And for the Home2 controller:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
 
namespace MVC3.Controllers
{
    public class Home2Controller : Controller
    {
        public ActionResult Action()
        {
          
            ViewBag.Controller = "Home2";
            return View("_Common");
        }
 
    }
}

Here is the output:

 

It’s clear that both Action() (they are in different controller) are calling same view.

HostForLIFE.eu ASP.NET MVC 6 Hosting
HostForLIFE.eu is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes. We have customers from around the globe, spread across every continent. We serve the hosting needs of the business and professional, government and nonprofit, entertainment and personal use market segments.




About HostForLIFE.eu

HostForLIFE.eu is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes.

We have offered the latest Windows 2016 Hosting, ASP.NET Core 2.2.1 Hosting, ASP.NET MVC 6 Hosting and SQL 2017 Hosting.


Tag cloud

Sign in