【问题标题】:View Model not getting returned on post查看模型未在帖子中返回
【发布时间】:2016-02-18 18:03:27
【问题描述】:

无论我在 post 方法中做什么,模型都将 Answers 设为 null。

index.cshtml

@using WebPortal.Classes
@using WebPortal.Models
@model WebPortal.Models.QuestionsAnswersViewModel 

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Index</h2>
@using (Html.BeginForm("index", "QuestionFormResponses", FormMethod.Post))
{
    @Html.AntiForgeryToken()

if (Model != null)
{
    string category = "";
    string column = "";

    foreach (Answer answer in Model.Answers)
    {
        if (answer.Question.QuestionCategory.Category != category)
        {
            category = answer.Question.QuestionCategory.Category;
            <h1>@category</h1>
        }

        if (answer.QuestionCategoryColumn.ColumnName != column)
        {
            column = answer.QuestionCategoryColumn.ColumnName;
            <h4>@column</h4>
        }

        Html.HiddenFor(x => x.Answers);

         <p>@answer.Question.QuestionText: </p>
         <input type="text" name="@answer.Id" value="@answer.QuestionAnswer"/>

    }
           <input type="submit" value="Submit" class="btn btn-default"/>
}

查看模型

   namespace WebPortal.Models
   {
       public class QuestionsAnswersViewModel
       {
           public List<Answer> Answers { get; set; }
        }
   }

发布方法

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Index(QuestionsAnswersViewModel m)
    {

答案模型

namespace WebPortal.Models
{
public class Answer
{
    public int Id { get; set; }
    public int QuestionId { get; set; }
    public virtual Question Question { get; set; }
    public int QuestionFormResponseId { get; set; }
    public virtual QuestionFormResponse QuestionFormResponse { get; set; }
    public int QuestionCategoryColumnId { get; set; }
    public virtual QuestionCategoryColumn QuestionCategoryColumn { get; set; }
    public object QuestionAnswer { get; set; }

}
}

我在帖子的第一行放置一个断点,然后查看 m 中的内容。 m 始终将 Answers 设置为 null。即使视图模型在经过前端时已满,前端也可以显示其中的内容。

Headers for the post

Params for the post

【问题讨论】:

  • 首先,您的输入元素缺少namevalue 属性的引号。另外,请附上您的 Answer 型号代码。
  • @timothyclifford 添加了答案模型。我会看看引号。我认为我不需要它们,因为我使用的是变量,但我也可以研究一下。
  • 你的整个动作方法在哪里?
  • 已经-1了。 @DanielA.White 你是指post前调用的index方法吗?
  • 向我们展示浏览器发出的实际 POST。

标签: c# html .net asp.net-mvc


【解决方案1】:

编辑:

将我的前端代码更改为以下代码。前端看起来很糟糕,但现在回发了

@using (Html.BeginForm("index", "QuestionFormResponses", FormMethod.Post))
{
    @Html.AntiForgeryToken()

if (Model != null)
{

    for (int i = 0; i < Model.Answers.Count; i++)
    {

        <table>
            <tr><th>
                @Html.DisplayFor(a => a.Answers[i].QuestionCategoryColumn.QuestionCategory.Category)
            </th></tr>
            <tr>
                <td>@Html.DisplayFor(a => a.Answers[i].QuestionCategoryColumn.ColumnName)</td>
                <td>@Html.DisplayFor(a => a.Answers[i].Question.QuestionText)</td>
                <td>@Html.TextBoxFor(a => a.Answers[i].QuestionAnswer)</td>
            </tr>
        </table>
    }

          <input type="submit" value="Submit" class="btn btn-default"/>
 }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-09
    • 1970-01-01
    • 1970-01-01
    • 2012-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多