0

I have this form with 3 fields that i want to pass to a controller action:

<form role="form" class="form-inline" name="frmUserData" id="frmUserData">
    <div class="form-group">
        <label for="txtlitm">Item Code:</label>
        <input type="text" class="form-control" id="txtlitm" name="txtlitm">
    </div>
    <div class="form-group">
        <label for="txtmcu">Branch Plant (filter):</label>
        <input type="text" class="form-control" id="txtmcu" name="txtmcu">
    </div>
    <div class="form-group">
        <label for="txtlocn">Location (filter):</label>
        <input type="text" class="form-control" id="txtlocn" name='txtlocn'>
    </div>
    <button type="button" class="btn btn-default" id="btnGO">Search</button>
</form>

My javascript: Version 1:

$.ajax({
    type: "POST",
    data: JSON.stringify($('#frmUserData').serializeArray()),
    contentType: "application/json; charset=utf-8",
    url: action,
    dataType: 'html',
    success: function(data) {
        $(dataContainer).html(data);
        $(what).hide();
    }

Version 2:

$.ajax({
    type: "POST",
    data: '{"frmData":' + JSON.stringify($('#frmUserData').serializeArray()) + '}',
    contentType: "application/json; charset=utf-8",
    url: action,
    dataType: 'html',
    success: function(data) {
        $(dataContainer).html(data);
        $(what).hide();
    }

My MVC Action:

public PartialViewResult GetAvailability(FormModel frmData)
{
    var vm = Bll.GetAvailabilityWithHolds(frmData.txtlitm, frmData.txtmcu, frmData.txtlocn);
    return PartialView(new GetAvailabilityModel{Availability = vm});
}

My Form class:

public class FormModel
{
    public string txtlitm { get; set; }
    public string txtmcu { get; set; }
    public string txtlocn { get; set; }
}

No matter what i do, i get null values in controller action.

What am i doing wrong?

1 Answer 1

2

have you tried this? You don't need to use Json, you can go with standard form-encoding

$.ajax({
type: "POST",
data: $('#frmUserData').serialize(),
url: action,
dataType: 'html',
success: function(data) {
    $(dataContainer).html(data);
    $(what).hide();
}

I can't test any of these fixes, but what about trying this to fix your original solution:

Attempt 1

$.ajax({
    type: "POST",
    data: '{"frmData":' + JSON.stringify($('#frmUserData').serializeArray()[0]) + '}',
    contentType: "application/json; charset=utf-8",
    url: action,
    dataType: 'html',
    success: function(data) {
        $(dataContainer).html(data);
       $(what).hide();
}

Attempt 2

$.ajax({
    type: "POST",
    data: JSON.stringify($('#frmUserData').serializeArray()[0]),
    contentType: "application/json; charset=utf-8",
    url: action,
    dataType: 'html',
    success: function(data) {
        $(dataContainer).html(data);
        $(what).hide();
}
Sign up to request clarification or add additional context in comments.

2 Comments

It works with your solution.But what if i wanted to use json. For this project i can live with just serialize.
I can't test without a setting up a sample project, but have a go at the alternate solutions in the answer

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.