0

i am unable to get the model data in my controller on post method, please help me my code is below.

model:
public class CardQuestionData
{
    public int cardId { get; set; }
    public string cardNotation { get; set; }
    public string cardDesc { get; set; }

    public List<SectionQuestionData> sectionData { get; set; }
}

public class SectionQuestionData
{
    public int sectionId { get; set; }
    public string sectionDesc { get; set; }
    public int sectionSerNo { get; set; }
    public List<QuestionData> questionData1 { get; set; }
}

public class QuestionData
{
    public int questionId { get; set; }
    public string questionDesc { get; set; }
    public int questionSerNo { get; set; }
    public int selectAns { get; set; }
    public IEnumerable<ControlData> controlData { get; set; }
}

public class ControlData
{ 
    public string controlType { get; set; }
    public string controlAnswMap { get; set; }
    public int controlQuesMap { get; set; }
    public int controlId { get; set; }
    public string controlDesc { get; set; }
    public int controlMinValue { get; set; }
    public int controlMaxValue { get; set; }
    public int controlSerNo { get; set; }
    public int controlValue { get; set; }
    public string versionNo { get; set; }
    public string attr1 { get; set; }
    public string attr2 { get; set; }
    public string attr3 { get; set; }
    public string attr4 { get; set; }
    public string attr5 { get; set; }
    public string attr6 { get; set; }
    public string attr7 { get; set; }
    public string attr8 { get; set; }
    public string attr9 { get; set; }
    public string attr10 { get; set; }
    public string answer { get; set; }
    public string statusFalg { get; set; }
    public DateTime initiatedDate { get; set; }
    public DateTime updateDate { get; set; }
    public DateTime submitDate { get; set; }
}


View:
@model  List<MSIL.Service.FeedbackCard.Models.CardQuestionData>

@using(Html.BeginForm("CardQuestions", "CardData", FormMethod.Post))
{
<div id="example" class="form-group">

<section class="well">
    <div>
        @{ foreach (var xx in Model)
            {
                @Html.HiddenFor(model => xx.cardId)
                for (int i = 0; i < xx.sectionData.Count(); i++)
                {
                    @Html.HiddenFor(model => xx.sectionData[i].sectionId)
                    @Html.DisplayFor(model => xx.sectionData[i].sectionDesc)

                    for (int j = 0; j <    xx.sectionData[i].questionData1.Count; j++)
                    {
                        @Html.HiddenFor(model => xx.sectionData[i].questionData1[j].questionId)
                        @Html.DisplayFor(model => xx.sectionData[i].questionData1[j].questionId)
                        @Html.DisplayFor(model => xx.sectionData[i].questionData1[j].questionDesc)

                        foreach (var ans in xx.sectionData[i].questionData1[j].controlData)
                        {
                            <div>
                                @Html.HiddenFor(model => ans.controlId)
                                @Html.RadioButtonFor(model => xx.sectionData[i].questionData1[j].selectAns, ans.controlId, new { id = ans.controlId })
                            </div>
                        }
                    }
                }
            }
        }


        <div class="clearfix"></div>

        <div class="buttons-wrap">
            <button class="k-button k-state-default" type="submit"   value="Previous" name="butCardData">Previous</button>
            <button class="k-button k-state-default" type="submit" value="Next" name="butCardData">Next</button>
        </div>
    </div>
</section>
</div>
}

Controller :
public ActionResult CardQuestions(CardQuestionData objQuestionsList, string butCardData, FormCollection frmx)
        {
}
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
Nirmal
  • 1
  • 1
  • 1
    Hi Nirmal, Please take a look at our how to ask guide: http://stackoverflow.com/help/how-to-ask . As of now, I am unable to find out exactly what you are struggling with. – dubes Mar 09 '16 at 10:35
  • You cannot use `foreach` loops to generate form controls for collections. In your case you need nested `for` loops - one for the `Model` and an inner `for` loop for `sectionData` –  Mar 09 '16 at 10:38
  • And once thats fixed, the parameter in the POST method needs to be `List` - i.e. it must match the model in the view –  Mar 09 '16 at 10:41
  • Hi Stephen, thanks for your help i have change my code and now i can generate controls but still unable to find null model in post method please find my code below. – Nirmal Mar 14 '16 at 07:09
  • Hi stephen i have done all in code based on your answer to similar type of problem but the i have facing problem to fetch data fro db in the model you have mention in that article, if i go with hard code every thing works fine. – Nirmal Mar 14 '16 at 07:53

2 Answers2

0

You are sending a list of object in your view but on the time of post you are expecting a single object :

@model  List<MSIL.Service.FeedbackCard.Models.CardQuestionData>


public ActionResult CardQuestions(CardQuestionData objQuestionsList, string butCardData, FormCollection frmx)
    {

}

I can't understand what you exactly need to submit if you want to submit a list then please use list on action result.

Varun Vasishtha
  • 461
  • 2
  • 9
0

As far i looked in your code it look ok no problem with this. i think you forget to put attribute in that

Please put [HttpPost] attribute on a controller method.

Ankur Shah
  • 412
  • 6
  • 16