Wednesday, 1 January 2014

Call a Editor Template - ASP.NET MVC


           In this article we are going to see how to call a Editor template from a view,Let we take an example of Area of interest survey form which have a sequence of subjects, where user have to select the subjects got interest. when they click submit the values are submitted to the page.

EditorFor Model() Method will call the view which have the same name of model type present in the Editor Template Folder.using ADO.Net Entity Framework imports the DataModel and save it as Subject for model name.

Output

After Submit


Step 1 : Create a Controller name InfoController.
Step 2 : Create a View with strongly typed IEnumerable<Subject> Index.cshtml, which is present under the folder of the View -> Info
Step 3 : Create a View name it as Subject and save under the folder Editor Templates as Subject.cshtml
Step 4 : Create a Get and Post requests.

Sql Script:
create table subject
(
id int identity(1,1),
name varchar(100),
isselected bit
)

insert into subject(name,isselected)
values('Computer Science',0)

insert into subject(name,isselected)
values('Micro Biology',1)

insert into subject(name,isselected)
values('Electronics',1)

insert into subject(name,isselected)
values('Civil',0)

insert into subject(name,isselected)
values('Aeronotics',0)

      select * from subject

C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EmpApp.Models;
using System.Text;

namespace EmpApp.Controllers
{
    public class StudiesController : Controller
    {

        EmployeeContext context = new Models.EmployeeContext();

        [HttpGet]
        public ActionResult Index()
        {                      
            return View(context.Subjects.ToList());
        }

        [HttpPost]
        public string Index(IEnumerable<Subject> subjects)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("Result : ");

            if (subjects.Count(x => x.isselected!=null) > 0)
            {
                builder.Append(" Selected Subjects are ");
               
                foreach (Subject sub in subjects)
                {
                    if(sub.isselected.Value)
                    builder.Append(sub.name + ", ");
                }
                builder.Remove(builder.ToString().LastIndexOf(","), 1);
            }
            else
            {
                builder.Append(" None of the subjects is selected");
            }
            return builder.ToString();
        }

    }
}



View
Subject.cshtml

@model EmpApp.Models.Subject

@Html.HiddenFor(m=>m.id)
@Html.HiddenFor(m=>m.name)
@Html.HiddenFor(m=>m.isselected)

@Html.CheckBoxFor(m=>m.isselected.Value)
@Html.DisplayFor(m=>m.name)
<br />



Index.cshtml

@model IEnumerable<EmpApp.Models.Subject>

@{
    ViewBag.Title = "Area of Interest";
}

<h2>Area of Interest</h2>

@using (Html.BeginForm())
{
   
    @Html.EditorForModel()
    <br />
    <input type="submit" value="Submit" />
}





From this article you can learn how to call the Editor Template from the View and how to create a Editor template.

No comments:

Post a Comment