0

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.

0

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.