Reputation: 885
Hi guys im having problems when posting, my model appears to be null on http post which is weird, there is no problems during the get method, everything populates as it should be but during post model is null. please help.
View
@model ComplianceCheck.Models.ViewModel.ArrivalModel
@for (int itemindex = 0, id = itemindex + 1; itemindex < Model.questionnaires.Count; itemindex++)
{
<tr>
<td>
@id
</td>
<td>
@Html.DisplayFor(modelItem => Model.questionnaires[itemindex].Description)
</td>
<td>
YES
@Html.RadioButtonFor(modelItem => Model.questionnaires[itemindex].Answer, true, new { id = "rbYes", @checked = "checked" })
NO
@Html.RadioButtonFor(modelItem => Model.questionnaires[itemindex].Answer, false, new { id = "rbNo" })
</td>
<td>
@Html.DisplayFor(modelItem => Model.questionnaires[itemindex].WigsID)
</td>
<td>
@Html.TextBoxFor(modelItem => Model.questionnaires[itemindex].Remarks, new { @style = "width:250px;" })
</td>
</tr>
Model
public class ArrivalModel
{
public ArrivalSearchModel searchModel = new ArrivalSearchModel();
public List<ArrivalDataModel> data = new List<ArrivalDataModel>();
public ArrivalSingleDataModel singledata = new ArrivalSingleDataModel();
public List<ArrivalQuestionnaires> questionnaires = new List<ArrivalQuestionnaires>();
}
public class ArrivalQuestionnaires
{
public int QuestionID { get; set; }
public string WigsID { get; set; }
public string Area { get; set; }
public string Description { get; set; }
public bool Answer { get; set; }
public string Remarks { get; set; }
}
Controller: some codes deleted to lessen question length
//Get works fine..
GET: /Questionnaires/ (Create)
public ActionResult QuestionnairesList(string txtDate, string Flightno, string Checker)
{
ArrivalModel model = new ArrivalModel();
List<ArrivalQuestionnaires> arrivalQuestionnaires = new List<ArrivalQuestionnaires>();
foreach (Question questionnaires in ArrivalService.GetAllQuestionnaires("Arrival"))
{
ArrivalQuestionnaires toAdd = new ArrivalQuestionnaires
{
QuestionID = questionnaires.QuestionID,
WigsID = questionnaires.Wig.WigsCode,
Area = questionnaires.Area,
Description = questionnaires.Description,
};
arrivalQuestionnaires.Add(toAdd);
}
model.questionnaires = arrivalQuestionnaires;
}
return View(model);
}
}
Here is the problem, httppost fires but model is null
[HttpPost]
public ActionResult QuestionnairesList(ArrivalModel model)
{}
Upvotes: 0
Views: 1297
Reputation:
Your model has no getters/setters
public class ArrivalModel
{
// Add constructor if you want to initialize properties
public ArrivalModel()
{
searchModel = new ArrivalSearchModel();
data = new List<ArrivalDataModel>();
singledata = new ArrivalSingleDataModel();
questionnaires = new List<ArrivalQuestionnaires>();
}
public ArrivalSearchModel searchModel { get; set; }
public List<ArrivalDataModel> { get; set; }
public ArrivalSingleDataModel singledata { get; set; }
public List<ArrivalQuestionnaires> questionnaires { get; set; }
}
Upvotes: 4
Reputation: 15387
For this, create editorTemplate for ArrivalQuestionnaires
and then bind to data in ArrivalModel
.
More information about editor Template
Upvotes: 0
Reputation: 1291
The problem is that your model is full of collections, try to read this article for the binding of the model
http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx/
Upvotes: 0