Thursday, 22 October 2015

MVC : Form Submission



View :
@model MVCDemo.Models.StudentDetail

<script type="text/javascript" src="js/script.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>




@{
    ViewBag.Title = "CreateStudent";
}

<h2>CreateStudent</h2>


@using (Html.BeginForm("CreateStudent11", "GetStudents", FormMethod.Post))
{
    @Html.AntiForgeryToken()
   
    <div class="form-horizontal">
        <h4>StudentDetail</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Class, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Class, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Class, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Section, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Section, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Section, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Address, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Address, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Address, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" id="Submit" class="btn btn-default" onclick="location.href='@Url.Action("GetStudents", "CreateStudent11")'"/>
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}



Model :

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

namespace MVCDemo.Models
{
    public class StudentDetails
    {
        public List<StudentDetail> Studentmodel { get; set; }

        public List<StudentDetail> GetStudentList()
        {
            List<StudentDetail> objStudent = new List<StudentDetail>();
            objStudent.Add(new StudentDetail { Id = 1, Name = "Ajay", Class = "1", Section = "2", Address = "Pune" });
            objStudent.Add(new StudentDetail { Id = 1, Name = "Aarav", Class = "1", Section = "2", Address = "Pune" });
            objStudent.Add(new StudentDetail { Id = 1, Name = "Rakhi", Class = "1", Section = "2", Address = "Pune" });
            return objStudent;
        }

        public List<StudentDetail> GetStudentList(int? id)
        {
            List<StudentDetail> objStudent = new List<StudentDetail>();
            objStudent.Add(new StudentDetail { Id = 1, Name = "Ajay", Class = "1", Section = "2", Address = "Pune" });
            if (id == null)
                return objStudent;
            else
            return objStudent.Where(P=>P.Id== id).ToList();
        }


    }
    public class StudentDetail
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Class { get; set; }
        public string Section { get; set; }
        public string Address { get; set; }

     
    }
}



Controller :

using MVCDemo.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Student_Application.Controllers
{
    public class GetStudentsController : Controller
    {
        // GET: GetStudents
        public ActionResult Index()
        {
            StudentDetails _objuserloginmodel = new StudentDetails();
            List<StudentDetail> _objStudent = new List<StudentDetail>();
            _objStudent = _objuserloginmodel.GetStudentList();
            _objuserloginmodel.Studentmodel = _objStudent;
            return View(_objuserloginmodel);
        }

        [HttpGet]
        public ActionResult SelectStudent(int? selectedStudent)
        {
            StudentDetails _objuserloginmodel = new StudentDetails();
            List<StudentDetail> _objStudent = new List<StudentDetail>();
            _objStudent = _objuserloginmodel.GetStudentList(selectedStudent);
            _objuserloginmodel.Studentmodel = _objStudent;

            return PartialView("_PartialStudent", _objuserloginmodel);

        }

       
        public ActionResult CreateStudent(StudentDetail objuserloginmodel)
        {
           
            if (!ModelState.IsValid)
            {
                return View("CreateStudent", objuserloginmodel);
            }
            else
            {
                StudentDetail _objuserloginmodel = new StudentDetail();
                return View("CreateStudent", _objuserloginmodel);
            }

        }

        [HttpPost]
        public ActionResult CreateStudent11(StudentDetail objuserloginmodel)
        {
            StudentDetails _objuserloginmodel = new StudentDetails();
            List<StudentDetail> _objStudent = new List<StudentDetail>();
            _objStudent = _objuserloginmodel.GetStudentList();
            _objuserloginmodel.Studentmodel = _objStudent;
            return View("Index",_objuserloginmodel);
        }

    }

    public class SubmitButtonSelector : ActionNameSelectorAttribute
    {
        public string Name { get; set; }
        public override bool IsValidName(ControllerContext controllerContext, string actionName, System.Reflection.MethodInfo methodInfo)
        {
            // Try to find out if the name exists in the data sent from form
            var value = controllerContext.Controller.ValueProvider.GetValue(Name);
            if (value != null)
            {
                return true;
            }
            return false;

        }
    }
}

No comments:

Post a Comment