【问题标题】:Getting viewmodel value null on Http Post method,在 Http Post 方法上获取 viewmodel 值 null,
【发布时间】:2023-03-06 16:58:01
【问题描述】:

My View page, it contains many dynamic controls 我知道这个问题已经被问过并回答了十几次,但没有一个解决方案能帮到我。

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

查看模型

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

控制器

[HttpPost] [验证AntiForgeryToken] 公共 ActionResult submit_Questionaire(VM_Questionaire vm_question) { 如果(模型状态。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>

First object getting value, but the list getting null.

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

    • 问题是正在创建一些动态输入控件,如何在 Http 帖子中获取该输入控件值。你能检查一下这行吗(Model.listQuestion 中的 Fonz_Survey.Question 问题)\n
    • 我是 MVC 新手。
    • @abhi 根据我的经验,输入 - 名称和 id 应该与模型类型名称完全匹配。您是否尝试过使用@Html.TextBoxFor()?
    • Html.TextBoxFor(model=> @question.QuestionEN ,new { htmlAttributes = new { class= "form-control" } })
    • 在浏览器中,检查输入元素并检查名称和 ID。它应该与模型匹配。
    【解决方案2】:

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

    谢谢。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-04
      • 1970-01-01
      相关资源
      最近更新 更多