Showing posts with label MVC. Show all posts
Showing posts with label MVC. Show all posts

Wednesday, July 10, 2013

New features in Asp.net MVC 5

Erliar i have given Asp.net MVC4 related information with examples. Now the new version has came which is MVC 5 with new features.The below link will give the complete tutorial of MVC 5 with examples

Wednesday, May 8, 2013

cascading dropdownlists in asp.net MVC4

Title:
Cascading DropDownList in Asp.net MVC

Description:
AS we discussed in about cascading drop down in asp.net ,this articles will also describe the same functionality using model view controller frame work.Before going to read this article ,we should have some basic knowledge on Jquery and MVC patters.

Example:
What we have discussed in previous articles is cascading dropdownlist in asp.net,Required Validations Asp.net MVC4 ,Bind data to dropdownlist in MVC,jquery reference mvc.Now i would like explain how to develop cascading dropdownlist in MVC4.For you need to do the step by step process as per below code

View:
@{
ViewBag.Title = "Cacading Dropdownlist in Asp.net MVC4";
}
@using (Html.BeginForm())
{
<fieldset>
<legend></legend>
@Html.Label("CodeBook")
@Html.DropDownList("CodeBook", ViewBag.CodeBookRes as SelectList, "Select a CodeBook", new { id = "CodeBook" })
@Html.Label("Versions")
<select id="Versions"  name="Versions"></select>
</fieldset>
}

@Scripts.Render("~/bundles/jquery")
<script type="text/jscript">
$(function () {
$('#CodeBook').change(function () {
$.getJSON('/Cascading/VersionResList/' + $('#CodeBook').val(), function (data) {
var Listitems = '<option>please Select CodeBook</option>';
$.each(data, function (i, Versions) {
Listitems += "<option value='" + Versions.Value + "'>" + Versions.Text + "</option>";
});
$('#Versions').html(Listitems);
});
});
});
</script>
MVC4CacadingController:
This controller has two methods one is return the list and other one return the JSON result.The json result will call when make selection of Book dropdown
using CascadingDropdownlistMVC4.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace CascadingDropdownlistMVC4.Controllers
{
public class CascadingController : Controller
{
//
// GET: /Cascading/
public ActionResult Index()
{
List CodeBooks = new List();
CodeBooks.Add(new SelectListItem { Text = "Asp.netMVC", Value = "Asp.netMVC" });
CodeBooks.Add(new SelectListItem { Text = "Sharepoint", Value = "Sharepoint" });
CodeBooks.Add(new SelectListItem { Text = "SqlServer", Value = "SqlServer" });
CodeBooks.Add(new SelectListItem { Text = "Asp.net", Value = "Asp.net" });
CodeBooks.Add(new SelectListItem { Text = "C#.net", Value = "C#.net" });
ViewBag.CodeBookRes = new SelectList(CodeBooks, "Value", "Text");
return View();
}

public JsonResult VersionResList(string Id)
{
var VersionRes = from vs in VersioList.GetVersion()
where vs.Book == Id
select vs;
return Json(new SelectList(VersionRes.ToArray(), "Book", "Version"), JsonRequestBehavior.AllowGet);
}
}
}
Set Proprties:In this class i have created variable and set the propeties
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace CascadingDropdownlistMVC4.Models
{
public class SetProperties
{
public string CodeBooks { get; set; }
public string Versions { get; set; }
}

public class VersionList
{
public string Book { get; set; }
public string Version { get; set; }

public static IQueryable GetVersion()
{
return new List
{
new VersionList { Book= "Asp.netMVC", Version= "MVC3" },
new VersionList { Book= "Asp.netMVC", Version= "MVC4" },
new VersionList { Book= "SqlServer", Version = "2008R2" },
new VersionList { Book= "SqlServer", Version = "2012" },
new VersionList { Book= "Sharepoint", Version= "2010" },
new VersionList { Book= "Sharepoint", Version = "2013" },
new VersionList { Book= "Asp.net", Version = "4.0" },
new VersionList { Book= "Asp.net", Version = "4.5" },
new VersionList { Book= "c#.net", Version = "4.0" },
new VersionList { Book= "C#.net", Version = "4.5" },
}.AsQueryable();
}
}
}

Thursday, December 27, 2012

Use of Routing in Asp.net MVC


The basic thing we have to know when we working with MVC .i:e "How to deliver or processing a request in Asp.net MVC".we have dynamic object type  route ,which is used to manage requests.This can deliver request to web forms called web forms routing,wcf services or any other .But in MVC the it will directly send the request directly to controller.To exaplain this process i have choosen a basic Internet application .


In that you will go the App_Start folder ,then select the route config file.The routing file has default collection of mapRoute.Here we can also create our custom mapRoute as per requirement

Sunday, October 14, 2012

Required and compare Validations in Asp.net MVC4

I have created a registration form  for my application,then placed some validation for user inputs based on requirement.In asp.net we have to use validation controls to implement validations.In Asp.Net MVC System.ComponentModel.DataAnnotations name space has been included to get the validation class to perform validations on it.The below code has done for required validation for user name and compare validation for password field

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
 
namespace TestApp.Models
{
public class RegisterModel
{
public int Id { get; set; }
[Required]
public string UserName { get; set; }
[StringLength(100,ErrorMessage="Password must have 5 characters at least",MinimumLength=5)]
[Required] 
[DataType(DataType.Password)]
public string Password { get; set; }
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
}
}

There is one more validation  i have done i:e String-length validation on user name.This can make the password minimum length to "5"

Friday, July 13, 2012

Bind data to dropdownlist in Asp.net MVC4

In previous articles we have seen how to bind data to dropdownlist in gridview in asp.net,How to create entity data model in MVC,Give the reference to css/java script file in ASP.NET MVC,.In this post i will give an example for binding data to drop downlist with static data and dynamic data(From DataBase) in MVC.To assign the items to drop downlist statically,First we have to put the items into SelectListItem array  in controller.Then we can bind those items to drop down in View
Model:

using System.Web;
using System.Web.Mvc;
using System.Configuration;

namespace Dropdown.Models
{
public class Dropdwon
{
public SelectListItem[] sli { set; get; }
}
}
Controller:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Dropdown.Models;

namespace Dropdown.Controllers
{
public class DropdownController : Controller
{
// GET: /Dropdown/
public ActionResult Index()
{
var sli = new[]{
new SelectListItem {Text = "India"},
new SelectListItem {Text = "US" },
new SelectListItem {Text = "UK" },
new SelectListItem {Text = "Australia" }
};
ViewData["ddlcountryname"] = sli;
return View(sli);
}

}
}
Index.cshtml
@{

    ViewBag.Title = "This is my test";

}

<h2>Bind data to dropdown list in mvc4</h2>

@Html.DropDownList("ddlcountryname")

Using Entity model To bind data:
I have used entity model to bind the data to dropdown from database.Whenever we create a connection we can get  model with our data base.Then we can write LINQ query to get the desired data.Finally we need to convert the data to SelectList from query data

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Dropdown.Models;

namespace Dropdown.Controllers
{
public class CityController : Controller
{
private Test_KrishnaEntities db = new Test_KrishnaEntities();
 
// GET: /City/

public ActionResult Index()
{
List x = new List(); 
var city = from contact in db.Contacts orderby contact.Name
                         select contact.Name;
x.AddRange(city.Distinct());
ViewData["ddlcountryname"] =new  SelectList(x);
return View();
}
}

}

Monday, June 25, 2012

How to set Reference for javascript/css file in ASP.NET MVC

When i work with MVC i don't know how to add the js and css files to application.I just give the reference for the files as per asp.net.But it is not a right method to give the reference in MVC.While adding a java script ,css or images are any file which we refer using the source tag "src" we need to use the following syntax in MVC using Asp.net
For java script:

<script type="text/javascript" src="@Url.Content("~/Scripts/jquery-1.7.2.min.js")"></script>
<script type="text/javascript" src="@Url.Content("~/Scripts/jquery.datepicker.addevents.js")"></script>

For Css files:
<link href="@Url.Content("~/csslib/reset.css" rel="stylesheet")" type="text/css" />
<link href="@Url.Content("~/csslib/styles.css" rel="stylesheet")" type="text/css" />

Monday, February 27, 2012

New Features in ASP.NET MVC 4

Here i will given what are the new featured are introduces in ASP.NET MVC 4 beta version.
It includes ASP.NET web API,A new framework to creating HTTP services.By using this framework we can reach clients including browsers and mobile devices. ASP.NET Web API is also an ideal platform for building Restful services.

The ASP.NET Web API includes following features:

1.Modern HTTP programming model
2.Full support for routes
3.Content negotiation
4.Model binding and validation
5.Filters
6.Improved testability of HTTP details
7.Improved Inversion of Control (IoC) via Dependency Resolver
8.Code-based configuration
9.Self-host
10.Introduced New HTML5 project template and mobile templates
download ASP.NET MVC 4

Bel