在Http Post方法上获取viewmodel值为null,

时间:2019-05-07 10:09:38

标签: c# asp.net-mvc asp.net-mvc-5 viewmodel

My View page, it contains many dynamic controls 我知道这个问题已经被回答了十多次,但是没有一种解决方案对我有帮助。

我有一个以下ViewModel,它由QuestionBatch数据模型,listResponseTag和listQuestion数据模型的列表组成。

查看模型

 public class VM_Questionaire
    {
        public QuestionBatch ThequestionBatch { get; set; }
        public List<Question> listQuestion { get; set; }
        public List<ResponseTag> listResponseTag { get; set; }
    }

控制器



[HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult submit_Questionaire(VM_Questionaire vm_question)
    {
        if (ModelState.IsValid)
        {
            Console.Write(Newtonsoft.Json.JsonConvert.SerializeObject(vm_question));
        }
        return View("Index");
    }

查看

<pre>

    @model Fonz_Survey.Models.VM_Questionaire

@{
    ViewBag.Title = "Questionaire";
}


@using (Html.BeginForm("submit_Questionaire", "Questions", FormMethod.Post))
{
    @Html.AntiForgeryToken()
    <h2>Questionaire</h2>
    <div class="card">
        <div class="card-header">
            <div class="jumbotron-fluid">
                <div class="container">
                    <div class="row">
                        <div class="col-lg-3 col-md-6 col-sm-6">
                            <h5 class="text-muted text-monospace">CODE</h5>
                        </div>
                        <div class="col-lg-3 col-md-6 col-sm-6 text-danger text-monospace">
                            @Html.DisplayFor(model => model.ThequestionBatch.Code, new { @class = "text-danger text-monospace" })
                            @Html.HiddenFor(model => model.ThequestionBatch.Code)
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-3 col-md-6 col-sm-6">
                            <h5 class="text-muted text-monospace">Question Batch</h5>
                        </div>
                        <div class="col-lg-3 col-md-6 col-sm-6 text-primary text-monospace">
                            @Html.DisplayFor(model => model.ThequestionBatch.BatchName, new { @class = "text-primary text-monospace" })
                            @Html.HiddenFor(model => model.ThequestionBatch.BatchName)
                        </div>
                        <div class="col-lg-3 col-md-6 col-sm-6">
                            <h5 class="text-muted text-monospace">No of Questions</h5>
                        </div>
                        <div class="col-lg-3 col-md-6 col-sm-6">
                            <label class="text-primary text-monospace">@ViewBag.totalQstns</label>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-3 col-md-6 col-sm-6">
                            <h5 class="text-muted text-monospace">Description</h5>
                        </div>
                        <div class="col-lg-3 col-md-6 col-sm-6 text-primary text-monospace text-wrap">
                            @Html.DisplayFor(model => model.ThequestionBatch.Description, new { @class = "text-primary text-monospace text-wrap" })
                            @Html.HiddenFor(model => model.ThequestionBatch.Description)
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="card-body">
            @{
                //int rowIndex = 0;
            }

            @if (Model != null && Model.listQuestion != null)
            {
                for(var rowIndex=0; rowIndex< Model.listQuestion.Count; rowIndex++)
                    //foreach (Question question in Model.listQuestion)
                    {
                       // rowIndex++;
                    <div class="row mb-4">
                        <div class="col-12">
                            <div class="card">
                                <div class="card-header bg-gray text-light">
                                    <div class="d-inline-block">
                                        <label class="text-lg-left font-weight-bold">@rowIndex.</label>
                                        <label class="text-lg-left font-weight-bold">@Model.listQuestion[rowIndex].QuestionEN</label>
                                        <br />
                                        <label class="text-lg-left font-weight-bold">@Model.listQuestion[rowIndex].QuestionAR</label>
                                    </div>
                                </div>
                                <div class="card-body font-weight-bolder">
                                    @switch (@Model.listQuestion[rowIndex].QType)
                                    {
                                        case 1:
                                            // to do Text Boxes
                                            <p class="text-info"><u>Please enter your message below;</u></p>
                                            <div class="form-row">
                                                @*<input type="text" placeholder="@question.QuestionEN" name="Q_@question.Id" id="Q_@question.Id" class="form-control" />*@
                                                @Html.EditorFor(model=> @Model.listQuestion[rowIndex].QuestionEN)
                                            </div>
                                            break;
                                        case 2:
                                            // to do Radio
                                            <p class="text-info"><u>Please select any one of the option below;</u></p>
                                            foreach (ChoiceTag ct in Model.listQuestion[rowIndex].ChoiceTags)
                                            {
                                                <div class="form-check">
                                                    <label class="form-check-label" for="C_@ct.Id">
                                                        <input type="radio" class="form-check-input" id="C_@ct.Id" name="@Model.listQuestion[rowIndex].Id" value="@ct.AnswerEN">@ct.AnswerEN | @ct.AnswerAR
                                                    </label>
                                                </div>
                                            }
                                            break;
                                        case 3:
                                            // to do Radio
                                            <p class="text-info"><u>Please select options below;</u></p>
                                            foreach (ChoiceTag ct in Model.listQuestion[rowIndex].ChoiceTags)
                                            {
                                                <div class="form-check">
                                                    <label class="form-check-label" for="C_@ct.Id">
                                                        <input type="checkbox" class="form-check-input" id="C_@ct.Id" name="@Model.listQuestion[rowIndex].Id" value="@ct.AnswerEN">@ct.AnswerEN | @ct.AnswerAR
                                                    </label>
                                                </div>
                                            }
                                            break;
                                    }
                                </div>
                            </div>
                        </div>
                    </div>
                }

                <div class="form-group">
                    <div class="col-12 text-center">
                        <input type="submit" value="Submit" class="btn btn-success " />
                    </div>
                </div>
            }

        </div>
        <div class="card-footer">
            <label class="text-danger">@ViewBag.Message</label>
        </div>
    </div>
                }

</prev>

First object getting value, but the list getting null.

2 个答案:

答案 0 :(得分:0)

@ Html.DisplayFor()将不会发布值。您需要使用@ Html.HiddenFor()和@ Html.DisplayFor()。请参阅:Html.DisplayFor not posting values to controller in ASP.NET MVC 3

答案 1 :(得分:0)

如果您不传递任何类型的数据,则将数据设置为隐藏字段,然后您将在各自的控制器处接收到数据。

谢谢。