MVC Dropdown List using Enumerator(enum)

In this post I will explain how to create a drop down list with Enumerator using MVC Rozar view. Many ways we have to do that but I will explain one of the easiest ways. If you follow this way we easily set select index of dropdown in Edit page also.
The view is
@{
var EnumInterval = from EnumDropDown().Interval n in Enum.GetValues(typeof(EnumDropDown.Interval))
select new SelectListItem
{
Value = n.ToString(),
Text = EnumDropDown.GetEnumDescription(n),
};
}
The Helper class is look like below

using System.Reflection;
using System.ComponentModel;
namespace EnumHtmlHelper.Helper
{
public static class EnumDropDown
{
public static string GetEnumDescription(TEnum value)
{
FieldInfo fi = value.GetType().GetField(value.ToString());
DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.
GetCustomAttributes(typeof(DescriptionAttribute), false);
if ((attributes != null) && (attributes.Length > 0))
return attributes[0].Description;
else
return value.ToString();
}
public enum Interval
{
[Description("Daily")]
D = 1,
[Description("Quick")]
Q = 2,
[Description("Weekly")]
W = 3
}
}
}
The View source look like bellow

Method Overriding in C#.NET


        Creating a method in derived class with same signature as a method in base class is called as method overriding.
        Same signature means methods must have same name, same number of arguments and same type of arguments.
        Method overriding is possible only in derived classes, but not within the same class.
 
When derived class needs a method with same signature as in base class, but wants to execute different code than provided by base class then method overriding will be used.
         To allow the derived class to override a method of the base class, C# provides two options,
virtual methods and abstract methods.

Page.PreviousPage Property(How to get value of previous page control value

            
            When you use the Server.Transfer method or use cross-page posting to transfer processing from one ASP.NET page to another Page, the originating page contains request information that might be required for the destination page. You can use the Previous Page property to access that information.


The following example is in two parts. The first is an ASP.NET page that uses the Transfer method, exposed in the page model as Server.Transfer("path"). The second part is the target page, which uses the PreviousPage property to get the title of the first page.

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

    protected void Page_Load(object sender, EventArgs e)
    {
        // If second is an even number, the server is available
        // Replace this line with a valid check for the server.
        bool IsServerAvailable = (DateTime.Now.Second % 2 == 0);

        if (!IsServerAvailable)
            Server.Transfer("Notify.aspx", true);
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Switch Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <h2>Database Server is Available</h2>

    <p>This page appears if the database server
        is available.</p>

    <p>Enter a pretend Server Name:
        <asp:TextBox ID="serverNameText"
        runat="server">MyDatabaseServer</asp:TextBox>
    </p>

    <p><asp:Button ID="SubmitButton" runat="server"
        Text="Is server available?" /></p>
    </div>
    </form>
</body>
</html>


Notify.aspx page 

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

    protected void Page_Load(object sender, EventArgs e)
    {
        // Find the server name on the previous page
        TextBox txt =
                (TextBox)Page.PreviousPage.FindControl("serverNameText");
        if (txt != null)
            prevServerName.Text = Server.HtmlEncode(txt.Text);
        else
            prevServerName.Text = "[Name Not available]";
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Page A</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <h2>Database Server is Not Available</h2>

    <p>This page appears if the named database server is not
    available, but the URL displays as the main target page.</p>

    <p>Server Name (From Page.PreviousPage):
        <asp:Label ID="prevServerName" runat="server" /></p>

    <p>Refresh the page to see if the server is now available.</p>
    </div>
    </form>
</body>
</html>


A circular reference was detected while serializing an object of type 'System.Data.Entity.DynamicProxies

In this post I will explain Circular Reference Serialization Error in MVC Entity frame work. In my latest project I need to get data from database for purpose of Autocomplete.So I need data as JSON format.
          When I request the “http://localhost:xxxx/Student/GetStudentJson”
Circular reference

How to get MVC Razor view Controls ID from Java script

In this post I will explain How to get MVC Razor view controller id from JavaScript .In real time some time we need to assign  value to controller or we need to read (get) value from controller.

Example The view have Following Controller
@model MvcApplication1.Models.User
@{
    ViewBag.Title = "create";
}
<h2>
    create</h2>
@using (Html.BeginForm())
{

    @Html.ValidationSummary(true)
    <fieldset>
        <legend>User</legend>
        <div class="editor-label">
            @Html.LabelFor(model => model.UserName)
        </div>