1

I just wonder when we pass data to controller method as parameters, why we need to use exactly the same name, any underlying reason?

For example, the following ajax call:

$.ajax({
            // Get Faculty PartialView
            url: "/Home/FacultiesToPVDemo",
            type: 'Get',
            data: { courseName: selectedCourseName },
            success: function (data) {
                $("#facultyDetailTable").empty().append(data);
            },
            error: function () {
                alert("something seems wrong");
            }
        });

will invoke the following method in the controller:

 public ActionResult FacultiesToPVDemo(string courseName)
    {
        IEnumerable<Course> allCourses = _repository.GetCourses();
        var selectedCourseId = (from c in allCourses where c.CourseName == courseName select c.CourseId).FirstOrDefault();

        IEnumerable<Faculty> allFaculties = _repository.GetFaculties();
        var facultiesForCourse = allFaculties.Where(f => f.AllotedCourses.Any(c => c.CourseId == selectedCourseId)).ToList();

        return PartialView("FacultyPV", facultiesForCourse);
    }

In the ajax if I use a different data property name other than "courseName", it wouldn't work, even if there is only one parameter to match. why is that?

strisunshine
  • 389
  • 2
  • 10
  • 2
    it's the model binding that looks to match the parameter names...you could still get the value from the Request – dotjoe Aug 04 '15 at 14:29
  • alternatively, you could write a custom ModelBinder that evaluates the Request object to make the call work :) – Icepickle Aug 04 '15 at 14:31
  • 2
    @Icepickle: Oh don't suggest *that*. I know you're trying to be funny, but someone's liable to go off and do something that silly. If you think a custom model binder is the answer, most likely you're asking the wrong question. – Chris Pratt Aug 04 '15 at 14:33
  • @dotjoe how the parameter is related to model? I think it just serves as a "parameter" for other data to fill in – strisunshine Aug 04 '15 at 14:33

2 Answers2

1

In exactly the same way that you cannot declare a method as:

public void Test(string test = null)

.. and expect this to work:

obj.Test(notTest: null)

If you're not passing your data over with the correct name, it won't bind. Just think of it in the same way, you are calling a method, albeit abstracted by ASP.NET / IIS through a routed HTTP request.

This allows you to have actions that have the same name, but different parameters should you need to accept them and is a benefit rather than a burden.

We may be able to help explain further or suggest alternatives if you give us a scenario where you would like things to be named differently. Things such as custom model binders, action filters (or anything overly complex) or accessing the form values directly (as a keyed array) are always an option depending on your actual requirements.

Rudi Visser
  • 21,350
  • 5
  • 71
  • 97
  • Ok so what I get is the naming matching is required for the binding to work . It was just defined so, but to me "courseName" is just a parameter in that function to find data in models – strisunshine Aug 04 '15 at 14:52
  • Well, in a logical mind - `courseName` is a course name. If you want to accept something else, add an overload (with a default value, `null`?) or a different method entirely that deals with your separate parameter. The appropriate method will be called depending on what parameters you actually pass over. – Rudi Visser Aug 04 '15 at 14:58
  • Okay, I mean sometimes as humans we may spell a different way, like "coursename" then it wouldn't match-) – strisunshine Aug 04 '15 at 15:03
  • It's static and defined in your javascript code, I don't see why this matters. – Rudi Visser Aug 04 '15 at 15:05
  • Never mind, thanks for your help :) – strisunshine Aug 04 '15 at 15:07
1

The MVC ModelBinder needs the names to match in order for it to know how to relate the incoming request data to the correct parameters.

Oliver
  • 35,233
  • 12
  • 66
  • 78
  • Thanks. What I get is that is just how it works. If there are more than one parameter and we use different names, the ModelBinder would be rather confused, right -: – strisunshine Aug 04 '15 at 14:56