2

I have a model as follows:

public class SearchModel
{
    public List<CheckBoxResponse> Status { get; set; }
}

public class CheckBoxResponse
{
    public string ItemText { get; set; }
    public bool Selected { get; set; }
}

The view is as follows:

<div id="divSearchParams">
    <div class="col-xs-12 col-sm-12 col-md-5 col-lg-5">
            <div class="form-group">
                <label>Status</label> <br />
                @for (var i = 0; i < Model.Status.Count; i++)
                {                       
                    <input type="checkbox" asp-for="@Model.Status[i].Selected" />
                    <label asp-for="@Model.Status[i].Selected">@Model.Status[i].ItemText</label>
                    <input type="hidden" asp-for="@Model.Status[i].ItemText" />
                }
            </div>
        </div>
</div>

My AJAX call :

$(document).on("click", "#search", function () {

    var model = $('#divSearchParams').find("input,select,textarea").serialize();
       
    $.ajax({
    url: targetUrl,
    type: 'POST',
    data: { model: model },
    cache: true,
    async: true,
    }).done(function (result) {
   
    }).fail(function (error) {
        
    }).always(function () {
    
    }
});   
});

So on serialise, the model value is "Status%5B0%5D.ItemText=Open&Status%5B1%5D.Selected=true"

However when I check the value of model in controller, I see Status value inside model is null.

public async Task<ActionResult> Students(SearchModel model)
{

}

What am I missing?

2
  • 1
    If you post that model that serializes to this, that isn't actually JSON. That is query string parameters. Thus, it won't be able to parse this as JSON. You could try appending this query string to the targetURL with: targetUrl + "?" + model and see if that works, but it probably isn't what you want here? Commented Sep 22, 2021 at 19:37
  • 1
    @The_Outsider: The JQuery 'serialize()' method returns a string. Therefore, if you change the Students() parameter type from SearchModel to string you will see what you are sending, at least. Commented Sep 22, 2021 at 22:28

1 Answer 1

2

Ajax post data with contentType application/x-www-form-urlencoded; charset=UTF-8 by default, you need change:

data: { model: model },

To:

data: model,

Be sure your backend controller is not ApiController, otherwise you need specific [FromForm] attribute on SearchModel parameter.

public class HomeController : Controller
{
    [HttpPost]
    public async Task<ActionResult> Students(SearchModel model)
    {
        //do your stuff...
    }
}

Result display:

enter image description here

Reference:

https://stackoverflow.com/a/65572281/11398810

Update:

If your action contains a second string parameter:

[HttpPost]
public async Task<ActionResult> Students(SearchModel model,string Id)
{
        //....
}

Add an input which has name="Id" in the View:

<input name="Id" />

If your action contains a second model type parameter:

Model:

public class TestViewModel
{
    public int Id{ get; set; }
}

Controller:

[HttpPost]
public async Task<ActionResult> Students(SearchModel model, TestViewModel Item)
{
    //...
}

Add an input which has name="Item.Id" in the View:

<input name="Item.Id" />

Update2:

Controller:

[HttpPost]
public async Task<ActionResult> Students(SearchModel model, string Id,bool isPartial)
{
    return View();
}

View:

$(document).on("click", "#search", function () {
    var id = "aaa";
    var isPartial = false;
    var model = $('#divSearchParams').find("input,select,textarea").serialize();
    $.ajax({
        url: "/Home/Students?Id=" + id + "&isPartial=" + isPartial,
        type: 'POST',
        data: model,
        cache: true,
        async: true,
    }).done(function (result) {

    }).fail(function (error) {

    }).always(function () {

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

5 Comments

This looks fine but what if there is a second parameter? How do I go about passing the second parameter in this situation?
Can you edit the post and put in an example for multiple parameters? Say a model and an id needs to be posted to the server? Ex : public async Task<ActionResult> Students(SearchModel model, string Id) {
Check my updated answer please.
I understand what you are saying. But I want something like data: { model: model, isPartial: true, Id: Id },in the AJAX function, not included as part of the form as these are not model parameters.
Hi @The_Outsider, you cannot combine { model: model, isPartial: true, Id: Id }, with .serialize data to post to backend, if the other parameter does not in form, you can append them to the url like: "/Home/Students?Id=" + id + "&isPartial=" + isPartial. Update my answer again.

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.