X

Subscribe To Our Mailing List

P.S I will never spam you...100% GUARANTEED!

Sunday, March 8, 2015

Clearing client side validation error messages in MVC

Introduction of "Clearing client side validation error messages in MVC"
In this article i am going to explain, how to clear the client side validation messages in MVC. This post will be useful if you are using dataannotation validations in MVC. In forum we were discussing about one scenario -

When you have two buttons in a page "Save" and "Cancel" , on click of "Cancel" button say you are doing AJAX call then you might have observed that client side messages still shown in the page. You might have read my articles - Client side validation using jquery in asp.net mvc and Jquery validation plugin remove rules where i have explained alternate approach for model validations.

Approach for "Clearing client side validation error messages in MVC"
Below is the code for clearing client side validation messages in MVC -

function clearValidationMessages() {
   $('.input-validation-error').addClass('input-validation-valid');
   $('.input-validation-error').removeClass('input-validation-error');
   //removing validation message from  tag created dynamically
   $('.field-validation-error').addClass('field-validation-valid');
   $('.field-validation-error').removeClass('field-validation-error');
   $('.validation-summary-errors').addClass('validation-summary-valid');
   $('.validation-summary-errors').removeClass('validation-summary-errors');
}

Suppose you want to remove validation messages from your partialview/pop up then you can use it like this -

function clearValidationMessages() {
   $('#yourpopupID .input-validation-error').addClass('input-validation-valid');
   $('#yourpopupID .input-validation-error').removeClass('input-validation-error');
   //removing validation message from  tag created dynamically
   $('#yourpopupID .field-validation-error').addClass('field-validation-valid');
   $('#yourpopupID .field-validation-error').removeClass('field-validation-error');
   $('#yourpopupID .validation-summary-errors').addClass('validation-summary-valid');
   $('#yourpopupID .validation-summary-errors').removeClass('validation-summary-errors');
}

Hope it helps to solve your issue as well. Thanks to my colleague shwetha for discussing this issue.

Please comment below.



Tuesday, March 3, 2015

Passing anonymous object to view in ASP.NET MVC

Introduction of "Passing anonymous object to view in ASP.NET MVC"
In this article I am going to discuss about passing anonymous object to my view. In article - Dynamic view model ASP.NET MVC using ExpandoObject i elaborated method to pass dynamic view model using ExpandoObject option. So here in this article i will be reusing models and methods created in previous article.

Practical Approach for "Passing anonymous object to view in ASP.NET MVC"
Now lets dive into practical approach of passing anonymous object to view. As mentioned i will be reusing classes created in the previous article. i.e, Class" and "Student" -
public class Class
{
 public int ClassID { get; set; }
 public string ClassName { get; set; }
}

public class Student
{
 public int StudentID { get; set; }
 public int ClassID { get; set; }
 public string StudentName { get; set; }
}
HomeController after updating looks like this -
public ActionResult Index()
{
 List classes = new List()
 {
  new Class() {ClassID =1, ClassName = "Class1"},
  new Class() {ClassID =2, ClassName = "Class2"},
  new Class() {ClassID =3, ClassName = "Class3"}
 };

 List students = new List()
 {
  new Student() {StudentID = 100, ClassID =1, StudentName = "Student1"},
  new Student() {StudentID = 101, ClassID =1, StudentName = "Student2"},
  new Student() {StudentID = 102, ClassID =1, StudentName = "Student3"},
  new Student() {StudentID = 103, ClassID =2, StudentName = "Student4"},
  new Student() {StudentID = 104, ClassID =2, StudentName = "Student5"},
  new Student() {StudentID = 105, ClassID =3, StudentName = "Student6"}
 };

 IEnumerable studentList1 = students.Where(s => s.ClassID == 1);

 return View(studentList1);
}

As you can see in the above code snippet, I am trying to filter the list students based on Class ID. And my Index view is updated like below -

@model IEnumerable<dynamic>

@{
 ViewBag.Title = "Home Page";
}


<p><b>Student Details</b></p>

<table>
 <tr>
  <th>StudentID</th>
  <th>StudentName</th>
 </tr>
 @foreach (DynamicModelusingAnonymousobjects.Models.Student s in Model)
 {
  <tr>
   <td>@s.StudentID</td>
   <td>@s.StudentName</td>
  </tr>
 }
</table> 

So here in the above code snippet i am trying to bind the students in Class ID = 1. Below is the screenshot of output from my application -


Passing anonymous object to view in ASP.NET MVC
Passing anonymous object to view in ASP.NET MVC

Hope this article is helpful.


Sunday, March 1, 2015

Dynamic view model ASP.NET MVC using ExpandoObject


Introduction of Dynamic view model ASP.NET MVC using ExpandoObject

In this "Dynamic view model ASP.NET MVC using ExpandoObject" article I am going to explain about passing dynamic model to our view in mvc. Here I am trying to use "ExpandoObjects". This has been introduced in .NET 4.0 version. An ExpandoObject object helps us to add or remove members dynamically at runtime.

Note: This article is just to give a information on how we can use dynamic models in mvc using ExpandoObject. But I am strictly against this approach as we can handle these stuffs with different approach as well (without using "ExpandoObject", i will discuss those approaches in the coming weeks though).

Now lets dive in to the practical approach of using dynamic view model using ExpandoObject.

Lets create new classes - "Class" and "Student" to demonstrate this -
public class Class
{
 public int ClassID { get; set; }
 public string ClassName { get; set; }
}

public class Student
{
 public int StudentID { get; set; }
 public int ClassID { get; set; }
 public string StudentName { get; set; }
}
So now lets update the existing Homecontroller to use "ExpandoObject". So below is the updated code -

public ActionResult Index()
{
 dynamic mymodelDetails = new ExpandoObject();
 
 List classes = new List()
 {
  new Class() {ClassID =1, ClassName = "Class1"},
  new Class() {ClassID =2, ClassName = "Class2"},
  new Class() {ClassID =3, ClassName = "Class3"}
 };

 List students = new List()
 {
  new Student() {StudentID = 100, ClassID =1, StudentName = "Student1"},
  new Student() {StudentID = 101, ClassID =1, StudentName = "Student2"},
  new Student() {StudentID = 102, ClassID =1, StudentName = "Student3"},
  new Student() {StudentID = 103, ClassID =2, StudentName = "Student4"},
  new Student() {StudentID = 104, ClassID =2, StudentName = "Student5"},
  new Student() {StudentID = 105, ClassID =3, StudentName = "Student6"}
 };

 //mymodelDetails.Classes = new { Classes = classes }.ToExpando();
 //mymodelDetails.Students = new { Students = students }.ToExpando();

 mymodelDetails.Classes = classes;
 mymodelDetails.Students = students;

 return View(mymodelDetails);
}

So as you can see in the above code snippet, data are populated for these classes and two properties "Classes" and "Students" are added to my dynamic expandoobject - "mymodelDetails".

So these "ExpandoObject" properties are used in the existing Index.cshtml as below -

@model dynamic
@{
    ViewBag.Title = "Home Page";
}

Class Details
<table> <tr> <th>ClassID</th> <th>ClassName</th> </tr> @foreach (PassingDynamicModel.Models.Class c in Model.Classes) { <tr> <td>@c.ClassID</td> <td>@c.ClassName</td> </tr> } </table> <br/> <br />
Student Details
<table> <tr> <th>StudentID</th> <th>StudentName</th> </tr> @foreach (PassingDynamicModel.Models.Student s in Model.Students) { <tr> <td>@s.StudentID</td> <td>@s.StudentName</td> </tr> } </table>

After Executing this we get output like below -

Dynamic view model ASP.NET MVC using ExpandoObject

Hope this article is useful.