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.


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)

insert into subject(name,isselected)

insert into subject(name,isselected)

      select * from subject

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();

        public ActionResult Index()
            return View(context.Subjects.ToList());

        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)
                    builder.Append( + ", ");
                builder.Remove(builder.ToString().LastIndexOf(","), 1);
                builder.Append(" None of the subjects is selected");
            return builder.ToString();



@model EmpApp.Models.Subject


<br />


@model IEnumerable<EmpApp.Models.Subject>

    ViewBag.Title = "Area of Interest";

<h2>Area of Interest</h2>

@using (Html.BeginForm())
    <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.

1 comment:

  1. This is an amazing blog,it gives very helpful messages to us.Besides that Wisen has established as Best Dot Net Training in Chennai. or learn thru ASP.NET Online Training . Nowadays Dot Net has tons of job opportunities on various vertical industry.