在HTTP Post方法上获取ViewModel值null



我的查看页面,它包含许多动态控件我知道这个问题已被问到并回答了很多次,但没有任何解决方案对我有帮助。

我有一个以下视图模型,该模型由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]    [varateantiforgerytoken]    公共行动ressult submit_questionaire(vm_questionaire vm_question(    {        if(ModelState.ISVALID(        {            console.write(newtonsoft.json.jsonconvert.serializeObject(vm_question((;        }        返回视图("索引"(;    }

查看

<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>

第一个获取值的对象,但列表获取为null。

@html.displayfor((不会发布值。您需要将 @html.hiddenfor((与 @html.displayfor((一起使用。请参阅:html.display,不在ASP.NET MVC 3

中发布值

如果您不传递任何类型的数据,则将您的数据作为隐藏字段,之后您将在各自的控制器中收到数据。

谢谢。

相关内容

  • 没有找到相关文章