Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用razor view TextArea for with dictionary不会更新viewmodel_C#_Asp.net Mvc_Dictionary_Razor - Fatal编程技术网

C# 使用razor view TextArea for with dictionary不会更新viewmodel

C# 使用razor view TextArea for with dictionary不会更新viewmodel,c#,asp.net-mvc,dictionary,razor,C#,Asp.net Mvc,Dictionary,Razor,我正在构建一个需要用户回答调查的应用程序。 我已经建立了调查工作的问题,是多项选择题;然而,我在自由回答问题时也遇到了同样的问题 这是我在视图中的表单: @using (Html.BeginForm("Submit", "Survey", FormMethod.Post, new { role = "form" })) { @Html.AntiForgeryToken() @Html.ValidationSummary("", new { @class = "text-dange

我正在构建一个需要用户回答调查的应用程序。 我已经建立了调查工作的问题,是多项选择题;然而,我在自由回答问题时也遇到了同样的问题

这是我在视图中的表单:

@using (Html.BeginForm("Submit", "Survey", FormMethod.Post, new { role = "form" }))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary("", new { @class = "text-danger" })
    var questionNumber = 0;
    <h4>Matchmaking Questions</h4>
    foreach (var question in Model.Questions)
    {
        var answers = Model.Answers.Where(a => a.QuestionId == question.Id);
        var answerList = new List<Answer>();

        <div class="form-group">
            <h5>
                <i>Q #@(questionNumber + 1)</i>: 
                @if (question.SurveyId == 3)
                {
                    if (User.IsInRole(UserType.HOST_ROLE))
                    {
                        <span>@question.Text</span>
                    }
                    else
                    {
                        <span>@question.Text2</span>
                    }
                }
                else
                {
                    <span>@question.Text</span>
                }
            </h5>
            <!-- If a question has a comment, display it here -->
            @if (!question.Comment.IsNullOrWhiteSpace())
            {
                <h6>@question.Comment</h6>
            }

            @if (answers.ToList().Any())
            {
                answerList = answers.ToList();
                @Html.DropDownListFor(m => m.UserAnswerIds[questionNumber],
                    new SelectList(answerList, "Id", "Text"),
                    "Select your answer", new { @class = "form-control" })
            }
            else
            {
                // TextAnswers has the correct number of count here...
                // But the values are not updated...
                Model.TextAnswers.Add(questionNumber, "");
                @Html.TextAreaFor(m => m.TextAnswers[questionNumber], 5, 300, new { })
            }
        </div>
        questionNumber++;
    }
<div class="form-group">
    <div class="col-md-offset-5 col-md-12">
        <input type="submit" class="btn btn-primary" value="Submit" />
    </div>
</div>
}
我遇到的问题是,当我试图在控制器中使用TextAnswer字典时,由于某种原因,字典似乎是空的。当我尝试调试时,字典在视图中工作,“TextAreaFor”方法似乎没有更新字典的值。但是,在视图呈现期间,字典就在那里,并且为每个文本答案创建了条目:只是这些值不会更新,也不会随视图模型一起传递到控制器中


我完全被难倒了,希望能得到专业人士的一些指导。

从我对
字典
绑定的了解来看,他们通常使用
foreach
循环和
字典
作为迭代基础,例如
@foreach(var-answer-in-TextAnswers){Html.textraeafor(m=>m.TextAnswers[answer.Key],5300,new{}}}您可以考虑使用部分视图来绑定<代码> StutyVIEW模型。答案与问题视图分开(检查类似的构造)。这实际上似乎是一个不同的问题。只有部分答案会发送给控制器。我总共有30个问题要问用户,但当我在控制器中收到SurveyViewModel时,模型的UserAnswerId大小为9????渲染视图时,大小为30…这对我来说毫无意义。。
public class SurveyViewModel
{
    public IEnumerable<Question> Questions { get; set; }
    public IEnumerable<Answer> Answers { get; set; }
    public ApplicationUser User { get; set; }
    public int[] UserAnswerIds { get; set; }
    public Dictionary<int, string> TextAnswers { get; set; }
}
public ActionResult Submit(SurveyViewModel viewModel)
{
    var userAnswerIds = viewModel.UserAnswerIds;
    var userId = User.Identity.GetUserId();
    var surveyComplete = true;
    // Create a new answer based on user's text
    foreach (var textAnswer in viewModel.TextAnswers) 
    // viewModel.TextAnswers has count of 0 here...
    {
        var customAnswer = new Answer()
        {
            QuestionId = textAnswer.Key,
            Text = textAnswer.Value
        };
        try
        {
            customAnswer = _context.Answers.Add(customAnswer);
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }
        // Add the new custom answer to user's list of answers
        userAnswerIds[textAnswer.Key] = customAnswer.Id;
    }

    // Add each answer to UserAnswer
    foreach (var t in userAnswerIds)
    {
        var currentAnswerBlock = t;
        if (_context.Answers.Any(a => a.Id == currentAnswerBlock))
        {
            var ua = new UserAnswer()
            {
                ApplicationUserId = userId,
                AnswerId = t
            };
            try
            {
                _context.UserAnswers.Add(ua);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
        else
        {
            surveyComplete = false;
        }
   }

  // Update user's survey completion status
   try
   {
        _context.Users.Single(u => u.Id == userId).SurveyComplete = surveyComplete;
    }
    catch (Exception e)
    {
        Console.WriteLine(e);
    }
    _context.SaveChanges();

    return RedirectToAction("Index", "Profile");
}