Full Trust European Hosting

BLOG about Full Trust Hosting and Its Technology - Dedicated to European Windows Hosting Customer

ASP.NET MVC 5 Hosting - HostForLIFE.eu :: How To Create Dropdown Menu?

clock May 4, 2016 00:37 by author Anthony

In this article, I will explain about how to implement Dropdown list in ASP.NET MVC. In traditional ASP.NET it is very easy to implement just by drag and dropping where we want. But in MVC we need some attention to create Drop Downlist.We have Dropdown list Html helper in ASP.NET MVC but we need to know how to bind data to Dropdown list Html helpers.
we can implement Drop down list in ASP.NET MVC in 2 ways.


Free ASP.NET Hosting - Europe

1.Using normal HTML Controls(select tag):

We can implement  dropdownlist using HTML <select/> tag like below
 ExampleCode:


<select id="html_dropdown">
    <option>--Select--</option>
    <option>India</option>
    <option>US</option>
    <option>China</option>
    <option>Russia</option>
    <option>United Kingdom</option>
</select>


2.Using HTML helpers:

Method 1: 


It is very easy to implement dropdownlist using normal html tags. But, if you want to bind data dynamically to the HTML tags it becomes complicated.To overcome and to make it easy to bind data to Html controls Microsoft introduced HTML helpers Methods.Here we are using Dropdown HTML helper to bind object data to Dropdownlist
.


Advantages:
1.It is also easy to implement.
2.Dynamic binding of data is very easy
3.We can easily implement cascading dropdownlists
Note:In method 1 i am sending data from controller using ViewBag. 


ExampleCode:

//Using ViewBag
            List<selectlistitem> item = new List<selectlistitem>();
            item.Add(new SelectListItem { Text = "India", Value = "1" });
            item.Add(new SelectListItem { Text = "China", Value = "2" });
            item.Add(new SelectListItem { Text = "United Sates", Value = "3" });
            item.Add(new SelectListItem { Text = "Srilanka", Value = "4" });
            item.Add(new SelectListItem { Text = "Germany", Value = "5" });
            ViewBag.html_helper_dropdown = item;
</selectlistitem></selectlistitem>

View code:

@using (Html.BeginForm())
{
    @Html.DropDownList("html_helper_dropdown","--select--")
}


Method 2:


In this i am sending data using ViewData to Controller with the same data source


Code:

List<selectlistitem> item = new List<selectlistitem>();
 item.Add(new SelectListItem { Text = "India", Value = "1" });
 item.Add(new SelectListItem { Text = "China", Value = "2" });
 item.Add(new SelectListItem { Text = "United Sates", Value = "3" });
 item.Add(new SelectListItem { Text = "Srilanka", Value = "4" });
 item.Add(new SelectListItem { Text = "Germany", Value = "5" });
ViewData["viewdata_ddl"] = item;
</selectlistitem></selectlistitem>

view code:

<p>Using View data</p>
@using (Html.BeginForm())
{
    @Html.DropDownList("viewbag_dropdown",ViewData["viewdata_ddl"] as List<SelectListItem>, "--select--")
}


Method 3:


Model binding.In this method we are binding the data using Model.This method mainly used for Strongly typed Views.


1.First create a model class with the 2 properties. one for Dropdown values and another for storing the selected value.


Model.cs:

using System.Collections.Generic;
using System.Web.Mvc;
 
namespace Dropdownlist.Models
{
    public class Model
    {
        public string selectedType { get; set; }
        public IEnumerable<selectlistitem> CountryList { get; set; }
    }
}
</selectlistitem>


Then prepare data source in controller and send the data through return View() method


Controller code:

using System;
using System.Collections.Generic;
using System.Web.Mvc;
using Dropdownlist.Models;
 
namespace Dropdownlist.Controllers
{
    public class HomeController : Controller
    {
 
        public ActionResult Index()
        {
            //Using ViewBag
            List<selectlistitem> item = new List<selectlistitem>();
            item.Add(new SelectListItem { Text = "India", Value = "1" });
            item.Add(new SelectListItem { Text = "China", Value = "2" });
            item.Add(new SelectListItem { Text = "United Sates", Value = "3" });
            item.Add(new SelectListItem { Text = "Srilanka", Value = "4" });
            item.Add(new SelectListItem { Text = "Germany", Value = "5" });
            ViewBag.html_helper_dropdown = item;
 
            //using ViewData
            ViewData["viewdata_ddl"] = item;
 
            //using Model binding
            var model = new Model
            {
                CountryList = item
             };
            return View(model);
        }
 
    }
}
</selectlistitem></selectlistitem>

View code as follows:

<p>Using Model </p>
@using (Html.BeginForm())
{
    @Html.DropDownListFor(x=>x.selectedType,Model.CountryList,"--select--")
}


Index.cshtml:

@using Dropdownlist.Models
@model Model
@{
    ViewBag.Title = "Dropdown list demo";
}

<h1>Dropdown list</h1>
<h3>Drop downlist using HTML</h3>
<select id="html_dropdown">
    <option>--Select--</option>
    <option>India</option>
    <option>US</option>
    <option>China</option>
    <option>Russia</option>
    <option>United Kingdom</option>
</select>
<br/>
<h3>Using Html helpers </h3>
<p>Method 1:</p><br/>
<p>Here the data was sent from Controller through ViewBag</p>
@using (Html.BeginForm())
{
    @Html.DropDownList("html_helper_dropdown","--select--")
}
<br/>
<p>Method 2:</p>
<p>Using View data</p>
@using (Html.BeginForm())
{
    @Html.DropDownList("viewbag_dropdown",ViewData["viewdata_ddl"] as List<SelectListItem>, "--select--")
}
<p>Method 3:</p>
<p>Using Model </p>
@using (Html.BeginForm())
{
    @Html.DropDownListFor(x=>x.selectedType,Model.CountryList,"--select--")
}

Note: We can also bind the dropdownlist data using ENUM's and arrays also.But, above are the most reliable and used by many developers (in our company also developers uses this methods only.).
Finally i got output like this.

 

 

 

HostForLIFE.eu ASP.NET MVC 5 Hosting
HostForLIFE.eu revolutionized hosting with Plesk Control Panel, a Web-based interface that provides customers with 24x7 access to their server and site configuration tools. Plesk completes requests in seconds. It is included free with each hosting account. Renowned for its comprehensive functionality - beyond other hosting control panels - and ease of use, Plesk Control Panel is available only to HostForLIFE's customers. They
offer a highly redundant, carrier-class architecture, designed around the needs of shared hosting customers.

 

http://aspnetmvceuropeanhosting.hostforlife.eu/image.axd?picture=2015%2f10%2fhostforlifebanner.png



ASP.NET MVC 5 Hosting - HostForLIFE.eu :: How to Fix Error “Could not load file or assembly ‘Microsoft.Web.Infrastructure’”

clock September 9, 2014 08:17 by author Peter

When deploying an ASP.NET MVC 5 application, I wanted to deploy it on Windows Azure to test it. Thanks to Visual Studio, the deployment was really easy but, as soon as I tried to browse the website, I received the following error message:

Could not load file or assembly ‘Microsoft.Web.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35′ or one of its dependencies. The system cannot find the file specified.

This is also one of a few component libraries that are needed for deploying an MVC application:

  • System.Web.Helpers.dll (required by the web.config)
  • System.Web.Mvc.dll
  • System.Web.Razor.dll
  • System.Web.WebPages.dll
  • System.Web.WebPages.Razor.dll
  • Microsoft.Web.Infrastructure.dll

The system libraries are installed with .NET 4, however, 'Microsoft.Web.Infrastructure.dll' is only installed when Visual Studio is installed on the machine.  Therefore, short of needing to install MVC and Visual Studio on a production environment, we need to deploy the libraries with out application - and we'd like to do so automatically.

As one possible fix, please try the following:
1. Run the following command in the Package Manager Console. (If you are using Visual Studio, this can be reached via menu options “Tools –> Library Package Manager –> Package Manager Console:)

PM> Install-Package Microsoft.Web.Infrastructure
You will see the following messages if it is successfully installed.
     Successfully installed 'Microsoft.Web.Infrastructure 1.0.0.0'.
   Successfully added 'Microsoft.Web.Infrastructure 1.0.0.0' to Web.

2. You will notice that Microsoft.Web.Infrastructure.dll has now been added as a Reference (can be seen in the references folder of your project in in Solution Explorer)
3. If you look at the properties of this reference you will notice that “Copy Local” has been set to “True” by default.
4. Now when you “Publish ” your project, Microsoft.Web.Infrastructure.dll will be deployed.



HostForLIFE.eu offers €1.29/month Affordable and High Performance Windows & ASP.NET Shared Hosting Plan

clock May 20, 2014 11:53 by author Peter

European Windows and ASP.NET hosting specialist, HostForLIFE.eu, has officially launched the new Windows & ASP.NET Shared Hosting Plan offered from as low as €1.29/month only. This LITE Windows & ASP.NET Hosting packages combine generous or 1 website, 1 GB disk space, 10 GB bandwidth, Support UTF-8 Domains, Dedicated Pool, etc. As the market for hosted solutions continues to grow, the new hosting range is designed to exceed the growing technical demands of businesses and IT professionals.

HostForLIFE.eu  is confident that their new LITE shared hosting plans will surely appeal to the personal across the world, besides the website owners and companies owning websites. The new web hosting plans will meet the requirement of high performance web hosting where one can easily update the content of a website on a regular basis. This plan is designed more for the web hobbiest needing affordable, high availability, hosting and easy backend management of windows and ASP.NET with powerful Plesk control panel.

Every day thousands of people decide to set up a website for business or personal use. New business owners and the average consumer don’t always have access to unlimited budgets. HostForLIFE.eu understand the importance of reliable hosting but are not always prepared to pay the exorbitant prices that reliable hosts charge.

For additional information about LITE Shared Hosting Plan offered by HostForLIFE.eu, please visit http://hostforlife.eu

About HostForLIFE.eu:

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

HostForLIFE.eu is awarded Top No#1 SPOTLIGHT Recommended Hosting Partner by Microsoft (see www.microsoft.com/web/hosting/HostingProvider/Details/953). Their service is ranked the highest top #1 spot in several European countries, such as: Germany, Italy, Netherlands, France, Belgium, United Kingdom, Sweden, Finland, Switzerland and other European countries. Besides this award, They have also won several awards from reputable organizations in the hosting industry and the detail can be found on their official website.



About HostForLIFE

HostForLIFE 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 2019 Hosting, ASP.NET 5 Hosting, ASP.NET MVC 6 Hosting and SQL 2019 Hosting.


Tag cloud

Sign in