2

I have an ajax method to retrieve a partial view based on a filter criteria using GET. However, it seems that I'm not able to pass in more than one parameter unless their all primitive types. No errors are thrown, just are passed in as null.

$.ajax({
    cache: false,
    type: "GET",
    dataType: 'json',
    contentType: 'application/json, charset=utf-8',
    url: '/DataManagement/GetDataPartialView',
    data: { configFilter : filter, Name: "SomeValue"},
    success: function (data) {
       $('#divManagement').html(data);
    },
    error: function (xhr, ajaxOptions, thrownError) {
        alert('Failed...');
    }
});

Controller

[HttpGet]
public ActionResult GetDataPartialView(ConfigFilter configFilter, string Name)
{
    ....
    return PartialView("_DataPartialView.tmpl", model);
}

Model

public class ConfigFilter
{
    public string ConnectionString { get; set; }
    public string UserId { get; set; }
    public string AppKey { get; set; }
}

if I pass either, it works... But I need to pass in both parameters.

1 Answer 1

4

You need to change the data object your passing to relate to the models/parameters in your method

$.ajax({
    cache: false,
    type: "GET",
    dataType: 'html', // your returing a view
    // contentType: 'application/json, charset=utf-8', remove
    url: '@Url.Action("GetDataPartialView", "DataManagement")', // don't hard code
    data: { ConnectionString: 'xxx', UserId: 'xxx', AppKey: 'xxx', Name: 'xxx' },
    success: function (data) {
       $('#divManagement').html(data);
    },
    error: function (xhr, ajaxOptions, thrownError) {
        alert('Failed...');
    }
});

Note aslo that your method is returning html, not json

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

2 Comments

Is that way no need to change the controller's overload? public ActionResult GetDataPartialView(ConfigFilter configFilter, string Name) GetDataPartialView need ConfigFilter object instead of primitive types.
@toha, No there is no need to change the method. The DefaultModelBinder reads the posted data and matches anything in the method parameters, including properties in a model.

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.