Reputation: 167
My Model Class as follows:
public class Employee
{
public Guid ID { get; set; }
[Required]
[Remote("CheckName","Employee",ErrorMessage="Already Exists...!")]
public String EmployeeName { get; set; }
public String EmployeeAddress { get; set; }
[Required]
public DateTime DateOfBirth { get; set; }
public decimal Salary { get; set; }
public String Department { get; set; }
public DateTime HireDate { get; set; }
}
My Action in Controller as Follows:
[HttpGet]
public JsonResult CheckName(string Name)
{
return Json(Name.Equals("MyString"), JsonRequestBehavior.AllowGet);
}
When i executed this i am getting a following exception
NullReferenceException: Object reference not set to n instance of an object
In the View, form contains a textbox and a submit button. If You are not clear with these things, please let me know to perform a remote validation.
Upvotes: 0
Views: 547
Reputation: 2746
Not sure how you're rendering the Textbox in question, but if you're using:
@Html.TextboxFor(x => x.EmployeeName) // or
@Html.EditorFor(x => x.EmployeeName) // or
@Html.Textbox("EmployeeName")
The field name of the input will be rendered as <input name="EmployeeName" /> and model binding will not be able to resolve it to the Name parameter of the CheckName method, thus attempting to execute
Name.Equals("MyString")
while Name is null and causing the NullReferenceException. If you change the parameter name of the CheckName method to EmployeeName, that should cause model binding to resolve the parameter to the name of the textbox.
This is all contingent on me understanding the situation and making some assumptions.
Upvotes: 3