Friday, June 11, 2010

EditorFor in MVC

Models->Supplier:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;

namespace FeaturesOfMVC2.Models
{
[MetadataType(typeof(Supplier_Validation))]
public partial class Supplier
{
class Supplier_Validation
{
[ScaffoldColumn(false)]
[DisplayName("Supplier Id :")]
public int SupplierId { get; set; }

[Required(ErrorMessage = "Name Required!")]
[DisplayName("Supplier Name :")]
public string SupplierName { get; set; }

[UIHint("StateDDL")]
[DisplayName("State :")]
public int StateId { get; set; }

[UIHint("CityDDL")]
[DisplayName("City :")]
public int CityId { get; set; }

[UIHint("EmailAddress")]
[DisplayName("Email Address :")]
public string EmailId { get; set; }
}
}
}


Views->Shared->EditorTemplates->StateDDL.ascx

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<%= Html.DropDownList("", new SelectList(ViewData["States"] as IEnumerable, "Id", "Name", Model))%>


Views->Shared->EditorTemplates->CityDDL.ascx

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<%= Html.DropDownList("", new SelectList(ViewData["Cities"] as IEnumerable, "Id", "Name", Model))%>


Views->Supplier->Edit.aspx

<%= Html.EditorFor(model => model.StateID) %>
<%= Html.EditorFor(model => model.CityID) %>

No comments: