0

How to pass list of objects to controller, I tried the below steps, list is not passed. Please see the below code & suggest me.

SupplimentDBQConfirmation ajax call to bring the model popup and populate the data via partial view. when i click proceed button i want to pass the table collection to controller, but I got dbqForm.SupplimentalDBQList is zero count. how to overcome this issue ? enter image description here

Model:

public class SupplimentalDBQ
{
    public string DBQName { get; set; }
    public bool Checked { get; set; }
    public string DBQDescription { get; set; }
    public bool FillExamNow { get; set; }
    public string FillExamNowId { get; set; }
    public string DBQNameId { get; set; }
    public string DBQDescriptionId { get; set; }
}

public class DBQ
{
    public DBQ()
    {
        SupplimentalDBQList = new List<SupplimentalDBQ>();
    }

    public string documentId { get; set; }
    ...
    ...
    public List<SupplimentalDBQ> SupplimentalDBQList { get; set; }
} 

Javascript:

function SupplimentDBQConfirmation(section, auditHistory, callback) {
    var ccDbqDtMoMappingId = $("#ccDbqDtMoMappingId").val();
    var DBQType = $("#DBQType").val();
    var documentId = $("#documentId").val();
    var examinerId = $("#examinerId").val();
    var dbqNumber = $("#dbqNumber").val();
    var appointmentId = $("#appointmentId").val();

    var Url = "/DBQ/SupplimentalDBQ?dbqNumber=" + dbqNumber + "&ccDbqDtMoMappingId=" + ccDbqDtMoMappingId + "&DbqType=" + DBQType + "&documentId=" + documentId + "&examinerId=" + examinerId + "&appointmentId=" + appointmentId;
    $.ajax({
        url: Url,
        type: 'post',
        success: function (result, status) {
            debugger;
            //alert(result + ' data ' + result.data);
            $("#SupplimentalDbq_Content").html(result);
            callback(result, status, section, auditHistory);
        },
        error: function (jqXHR, textStatus, errorThrown) {
            $('#getCode').html('Error in SupplimentDBQConfirmation ajax call:' + textStatus + ' - ' + errorThrown);
            $('#myAlertModal').modal({
            });
        }
    });
}

function CreateAppointment() {
    var infoForm = $("#SuppDBQform");
    var CaseHeaderId = $('#CaseHeaderId').val();
    var Url = '/DBQ/CreateSupplimentalDBQ';

    $.ajax({
        url: '@Url.Action("CreateSupplimentalDBQ", "DBQ")',
        type: 'post',
        data: infoForm.serialize() + "&CaseHeaderId=" + CaseHeaderId,
        success: function (result) {
            alert('status ' + ' ' + result);
        },
        error: function (jqXHR, textStatus, errorThrown) {
            $('#getCode').html('Error in CreateAppointment ajax call:' + textStatus + ' - ' + errorThrown);
            $('#myAlertModal').modal({
            });
        }
    });
}

Controller:

public PartialViewResult SupplimentalDBQ(string dbqNumber, int ccDbqDtMoMappingId, string DbqType, string documentId, string examinerId, int appointmentId)
{
    DBQ _dbq = new DBQ();
    _dbq.dbqNumber = dbqNumber; _dbq.ccDbqDtMoMappingId = ccDbqDtMoMappingId; _dbq.DBQType = DbqType; _dbq.documentId = documentId; _dbq.appointmentId = appointmentId;

    var AllDbq = new List<DbqMaster>();
    var routingInformation = RestUtility.CreateRoutingInformation("provider", "dbq", "GetAllDBQ");
    AllDbq = base.PerformGet<List<DbqMaster>>(routingInformation);

    if (DbqType == "DBQHIVRelatedIllnesses")
    {
        var ldbqform = (DBQHIVRelatedIllnesses) MDESession.Current.DBQFORM;
        int i = 1;
        foreach(SupplimentalDBQ sdbq in ldbqform.SupplimentalDBQList)
        {
            sdbq.DBQDescription = AllDbq.Where(x => x.DbqNumber == sdbq.DBQName).FirstOrDefault().Description;
            sdbq.FillExamNowId = "FillExam" + i.ToString();
            sdbq.DBQNameId = "DBQName" + i.ToString();
            sdbq.DBQDescriptionId = "DBQDescription" + i.ToString();
            _dbq.SupplimentalDBQList.Add(sdbq);
            i = i + 1;
        }
    }
    return PartialView("~/Views/Examiner/SupplimentalDBQ.cshtml", _dbq);
}

[HttpPost]
public ActionResult CreateSupplimentalDBQ(DBQ dbqForm, string CaseHeaderId)
{
    foreach(SupplimentalDBQ sdbq in dbqForm.SupplimentalDBQList)
    {
        CaseDetailsController obj = new CaseDetailsController();
        var result = obj.SaveSelectedClaim(Convert.ToInt32(CaseHeaderId), false, sdbq.DBQName, "Added through supplimental dbq", dbqForm.ccDbqDtMoMappingId);
    }

    return Json("Data Saved Successfully");
}

View:

@model VAMDEScheduler.General.DBQ

@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "SuppDBQform" }))
{
    @Html.AntiForgeryToken()
    @Html.TextBoxFor(model => model.documentId)
    @Html.TextBoxFor(model => model.ccDbqDtMoMappingId)
    @Html.TextBoxFor(model => model.appointmentId)
    @Html.TextBoxFor(model => model.DBQType)
    @Html.TextBoxFor(model => model.dbqNumber)

    <table class="table table-striped" border="0">
        <thead>
            <tr>
                <th width="10%">
                    Fill Exam Now
                </th>
                <th width="15%">
                    DBQ Number
                </th>
                <th>
                    DBQ Description
                </th>
            </tr>
        </thead>
        <tbody>
            @foreach (var item in Model.SupplimentalDBQList)
            {
                <tr>
                    <td>
                        @Html.CheckBoxFor(modelItem => item.FillExamNow, new { id = item.FillExamNowId, name = item.FillExamNowId })
                    </td>
                    <td>
                        @Html.TextBoxFor(modelItem => item.DBQName, new { disabled = "disabled", id = item.DBQNameId, name = item.DBQNameId })
                    </td>
                    <td>
                        @Html.TextBoxFor(modelItem => item.DBQDescription, new { disabled = "disabled", id = item.DBQDescriptionId, name = item.DBQDescriptionId })
                    </td>
                </tr>
            }
        </tbody>
    </table>
}
ekad
  • 14,436
  • 26
  • 44
  • 46
  • Why are you posting using ajax? Your generating invalid html (duplicate `id` attributes) and duplicate `name` attributes which will not bind to anything. Refer [this answer](http://stackoverflow.com/questions/30094047/html-table-to-ado-net-datatable/30094943#30094943) for how to generate your view. –  Sep 14 '16 at 06:52
  • And `new { id = item...., name = item.... }` is pointless (your just overwriting the `id` attribute with what it already is and setting `name` does not work fortunately. And your setting `disabled` attributes which meas those form controls will not submit a value –  Sep 14 '16 at 06:55

0 Answers0