Showing posts with label Asp.Net MVC4. Show all posts
Showing posts with label Asp.Net MVC4. Show all posts

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

Tuesday, October 16, 2012

How to Creating an Entity Data Model from a Database in Asp.net MVC 4

In this post i have given how to create an Entity Data Model in MVC4 project. Here i will create Entity Data model on "TestKrihsna" database which contains eight tables.
Steps for creating EDM:
1.Right click -->select add -->new item -->select "data" in installed templates frame-->select the Ado.net entity data model.

2.We have two option to create EDM which are 1.using Data Base 2.Empty Model.Here i will select Generate From data base option to create EDM with existing database
3.Here we have to select the which data base  used for application .Then the connection string has generated as per sql server credentials.
4.We can see in the below screen ,it  has check box options to select which tables,views and procedures to include in model.Here i have select all table for my application then click on next button
5.Finally we can see the browser window which has entity data model(model.edmx) in VS

 Why we are using entity data model? .

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"

Bel