Wednesday, 21 October 2015
Cares has to be taken while working MVC
1: Partial View always in shared folder
2: Always check for null condition in the partial view
3: $ is not defined means the Jquery and Javascript namespaces are not included
4: bind the proper model to the view. Inproper model wont give the error.
5: Verify with alert("") is your selected value is called in jquery
Updating an MVC Partial View with Ajax
https://cmatskas.com/update-an-mvc-partial-view-with-ajax/
MVC : Calling the Partial View on Selected Index changed
@model MVCDemo.Models.StudentDetails
@{
ViewBag.Title = "Index";
}
<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>
<script type="text/javascript">
$(document).ready(function () {
$(document).ready(function () {
$("#myDropDown").change(function () {
var selectedID = $(this).val();
alert(selectedID);
var categoryId = $("#myDropDown").val();
$("#dvCategoryResults").load('@(Url.Action("SelectStudent", "GetStudents", null, Request.Url.Scheme))?selectedStudent=' + selectedID);
});
});
});
</script>
<style>
table, td, th {
border: 1px solid green;
border-collapse: collapse;
}
th {
border: 1px solid black;
background-color: green;
color: white;
}
</style>
<br><br><br><br><br><br>
@Html.DropDownListFor(M => M.Studentmodel, new SelectList(Model.Studentmodel, "ID", "Name",0),"Please Select", new { @id = "myDropDown" })
<br>
<div id="dvCategoryResults">
@Html.Partial("~/Views/Shared/_PartialStudent.cshtml", Model);
</div>
@model MVCDemo.Models.StudentDetails
@{
ViewBag.Title = "Index";
}
<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>
<script type="text/javascript">
$(document).ready(function () {
$(document).ready(function () {
$("#myDropDown").change(function () {
var selectedID = $(this).val();
alert(selectedID);
var categoryId = $("#myDropDown").val();
$("#dvCategoryResults").load('@(Url.Action("SelectStudent", "GetStudents", null, Request.Url.Scheme))?selectedStudent=' + selectedID);
});
});
});
</script>
<style>
table, td, th {
border: 1px solid green;
border-collapse: collapse;
}
th {
border: 1px solid black;
background-color: green;
color: white;
}
</style>
<br><br><br><br><br><br>
@Html.DropDownListFor(M => M.Studentmodel, new SelectList(Model.Studentmodel, "ID", "Name",0),"Please Select", new { @id = "myDropDown" })
<br>
<div id="dvCategoryResults">
@Html.Partial("~/Views/Shared/_PartialStudent.cshtml", Model);
</div>
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);
}
}
}
Partial View
@model MVCDemo.Models.StudentDetails
@if (Model.Studentmodel != null && Model.Studentmodel.Count() > 0)
{
var grid = new WebGrid(Model.Studentmodel, canSort: false);
<div>
@grid.GetHtml(columns:
grid.Columns
(
grid.Column("ID", "Stud ID"),
grid.Column("Name", "Stud Name"),
grid.Column("Class", "Class"),
grid.Column("Section", "Section"),
grid.Column("Address", "Address")
), mode: WebGridPagerModes.Numeric)
</div>
}
Partial View
@model MVCDemo.Models.StudentDetails
@if (Model.Studentmodel != null && Model.Studentmodel.Count() > 0)
{
var grid = new WebGrid(Model.Studentmodel, canSort: false);
<div>
@grid.GetHtml(columns:
grid.Columns
(
grid.Column("ID", "Stud ID"),
grid.Column("Name", "Stud Name"),
grid.Column("Class", "Class"),
grid.Column("Section", "Section"),
grid.Column("Address", "Address")
), mode: WebGridPagerModes.Numeric)
</div>
}
MVC : how-to-add-a-please-select-item-to-a-dropdown-box-in-asp-net-mvc
http://stackoverflow.com/questions/2648366/how-to-add-a-please-select-item-to-a-dropdown-box-in-asp-net-mvc
MVC : Bind the list to the dropdown
http://www.c-sharpcorner.com/UploadFile/4d9083/binding-dropdownlist-in-mvc-in-various-ways-in-mvc-with-data/
MVC : Bind the collection to Grid
http://www.aspdotnet-pools.com/2014/07/how-to-bind-data-to-webgrid-in-aspnet.html
Subscribe to:
Posts (Atom)