Tag: Razor

Remove Unused View Engines

By default, ASP.NET MVC loads both the Razor and Web Forms View Engines. This can cause performance issues. Because ASP.NET will look for Web Forms views first, and switch to look for Razor views if Web Forms views cannot be found.

This can be resolved easily by adding the following 2 lines to the Application_Start method in Global.asax.cs.

ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new RazorViewEngine());

CascadingDropDown in ASP.NET MVC

1. Download CascadingDropDown jQuery Plugin

2. Put file jquery.cascadingDropDown.js in ~\Scripts

3. Add the following line of code in ~\App_Start\BundleConfig.cs

    bundles.Add(new ScriptBundle("~/bundles/jqueryplugin").Include(
        "~/Scripts/jquery.cascadingDropDown.js"));

4. Create a View Model for the Cascading DropDown Select Lists and the selected values

public class CascadingDropDownViewModel
{
    public SelectList AreaSelectList { get; set; }

    public SelectList DistrictSelectList { get; set; }

    [Display(Name = "Area")]
    public Area Area { get; set; }

    [Display(Name = "District")]
    public District District { get; set; }
}

5. Create a Controller for populating the View Model

public ActionResult CascadingDropDown()
{
    CascadingDropDownViewModel model = new CascadingDropDownViewModel();
    model.AreaSelectList = new SelectList(_entities.Areas, "AreaId", "Name");
    model.DistrictSelectList = new SelectList(Enumerable.Empty(), "DistrictId", "Name");

    return View(model);
}

6. Create a View to show the Cascading DropDown

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    @Html.LabelFor(m => m.Area) :
    @Html.DropDownListFor(m => m.Area, Model.AreaSelectList, "-- Please Select --")
    @Html.ValidationMessageFor(m => m.Area)


    @Html.LabelFor(m => m.District) :
    @Html.DropDownListFor(m => m.District, Model.DistrictSelectList)
    @Html.ValidationMessageFor(m => m.District)


    <button type="submit">Submit</button>
}

@section scripts{
<script type="text/javascript">// <![CDATA[
        $(document).ready(function () {
            $("#District").CascadingDropDown("#Area", "/Ajax/DistrictCascadingDropDown",
            {
                promptText: "-- Please Select --",
                postData: function () {
                    return { areaId: $("#Area").val() };
                }
            });
        });

// ]]></script>
}

7. Create a dedicated Controller AjaxController and an Action method DistrictCascadingDropDown

public ActionResult DistrictCascadingDropDown(int areaId)
{
	var query = from d in _entities.Districts
				where d.AreaId == areaId
				select d;

	SelectList selectList = new SelectList(query, "DistrictId", "Name");

	return Json(selectList);
}

References

DropDownList in ASP.NET MVC

View Model

public class DropDownListViewModel
{
    public SelectList DistrictSelectList { get; set; }

    [Display(Name = "District")]
    public District District { get; set; }
}

Controller

public ActionResult DropDownList()
{
    DropDownListViewModel model = new DropDownListViewModel();
    model.DistrictSelectList = new SelectList(_entities.Districts, "DistrictId", "Name");

    return View(model);
}

View

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    @Html.LabelFor(m => m.District) <text>:</text>
    @Html.DropDownListFor(m => m.District, Model.DistrictSelectList, "-- Please Select --")
    @Html.ValidationMessageFor(m => m.District)
    <button type="submit">Submit</button>
}

References