当前位置: 首页>>代码示例>>C#>>正文


C# Question.AddResponse方法代码示例

本文整理汇总了C#中Question.AddResponse方法的典型用法代码示例。如果您正苦于以下问题:C# Question.AddResponse方法的具体用法?C# Question.AddResponse怎么用?C# Question.AddResponse使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Question的用法示例。


在下文中一共展示了Question.AddResponse方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Edit


//.........这里部分代码省略.........
                            break;
                        } else if (foundResp.IsActive == responsesParameter.Remove) //Hide response has changed
                        {
                            originalHasChanges = true;
                            break;
                        }
                    }
                }
            }
            if (newCategoryId != 0 && newCategoryHasAnswers)
            {
                var newCategory = Repository.OfType<Category>().GetNullableById(newCategoryId);
                if (newCategory != null)
                {
                    if (newCategory.IsActive && question.IsActive) //If the new Category isn't active it shouldn't have answers
                    {
                        newHasChanges = true;
                    }
                }
            }

            //Mapper.Map(question, questionToEdit);
            question.Responses.Clear();

            foreach (var responsesParameter in viewModel.Responses)
            {
                if (responsesParameter.ResponseId != 0)
                {
                    var foundResp = questionToEdit.Responses.Where(a => a.Id == responsesParameter.ResponseId).Single();
                    foundResp.Value = responsesParameter.Value;
                    foundResp.Score = responsesParameter.Score.GetValueOrDefault(0);
                    foundResp.IsActive = !responsesParameter.Remove;
                    foundResp.Order = responsesParameter.Order;
                    question.AddResponse(foundResp);
                }
                else
                {
                    var responseToAdd = new Response
                    {
                        Order = responsesParameter.Order,
                        IsActive = true,
                        Score = responsesParameter.Score.GetValueOrDefault(0),
                        Value = responsesParameter.Value
                    };

                    question.AddResponse(responseToAdd);
                }
            }
            question.Survey = questionToEdit.Survey;

            ModelState.Clear();
            question.TransferValidationMessagesTo(ModelState);

            foreach (var responsesParameter in cleanedResponse)
            {
                if (!responsesParameter.Score.HasValue)
                {
                    ModelState.AddModelError("Question", "All responses need a score");
                }
                if (question.IsOpenEnded && !responsesParameter.Remove)
                {
                    switch ((QuestionType)question.OpenEndedQuestionType)
                    {
                        case QuestionType.WholeNumber:
                            int number;
                            if (!int.TryParse(responsesParameter.Value, out number))
开发者ID:ucdavis,项目名称:NuSurvey,代码行数:67,代码来源:QuestionController.cs

示例2: Create

        public ActionResult Create(int id, int? categoryId, Question question, ResponsesParameter[] response)
        {
            var isNewVersion = false;
            var survey = Repository.OfType<Survey>().GetNullableById(id);
            if (survey == null)
            {
                Message = "Survey Not Found";
                return this.RedirectToAction<ErrorController>(a => a.Index());
            }
            var viewModel = QuestionViewModel.Create(Repository, survey);
            if (categoryId != null) //This category Id is just used for defaults and navigation
            {
                var category = Repository.OfType<Category>().GetNullableById(categoryId.Value);
                viewModel.Category = category;
            }
            viewModel.Question = question;

            if (question.Category != null && Repository.OfType<Answer>().Queryable.Where(a => a.Category.Id == question.Category.Id).Any())
            {
                //The category picked already has answers, so if it is valid and we create a new question, we want to create a new version of the category first.
                isNewVersion = true;
            }

            // Remove responses that do not have a Choice or that have the remove checked. This is the create, so they will never be added
            var cleanedResponse = new List<ResponsesParameter>();
            foreach (var responsesParameter in response.OrderBy(a => a.Order))
            {
                if (!string.IsNullOrWhiteSpace(responsesParameter.Value) && !responsesParameter.Remove)
                {
                    cleanedResponse.Add(responsesParameter);
                }
            }
            viewModel.Responses = cleanedResponse;

            var questionToCreate = new Question(survey);
            Mapper.Map(question, questionToCreate);

            foreach (var responsesParameter in viewModel.Responses)
            {
                var responseToAdd = new Response
                {
                    Order = responsesParameter.Order,
                    IsActive = true,
                    Score = responsesParameter.Score.GetValueOrDefault(0),
                    Value = responsesParameter.Value
                };

                questionToCreate.AddResponse(responseToAdd);
            }

            ModelState.Clear();
            questionToCreate.TransferValidationMessagesTo(ModelState);

            foreach (var responsesParameter in cleanedResponse)
            {
                if (!responsesParameter.Score.HasValue)
                {
                    ModelState.AddModelError("Question", "All responses need a score");
                }
                else
                {
                    if(questionToCreate.IsOpenEnded)
                    {
                        switch ((QuestionType)questionToCreate.OpenEndedQuestionType)
                        {
                            case QuestionType.WholeNumber:
                                int number;
                                if (!int.TryParse(responsesParameter.Value, out number))
                                {
                                    ModelState.AddModelError("Question", "Choices must be whole numbers");
                                }
                                break;
                            case QuestionType.Decimal:
                                float floatNumber;
                                if (!float.TryParse(responsesParameter.Value, out floatNumber))
                                {
                                    ModelState.AddModelError("Question", "Choices must be numbers (decimal ok)");
                                }
                                break;
                            case QuestionType.Time:
                                float floatTime;
                                if (!responsesParameter.Value.TimeTryParse(out floatTime))
                                {
                                    ModelState.AddModelError("Question", "Choices must be Time (hh:mm)");
                                }
                                break;
                            case QuestionType.TimeAmPm:
                                float floatTimeAmPm;
                                if (!responsesParameter.Value.TimeTryParseAmPm(out floatTimeAmPm))
                                {
                                    ModelState.AddModelError("Question", "Choices must be Time (hh:mm AM/PM)");
                                }
                                break;
                            case QuestionType.TimeRange:
                                float timeRangeNumber;
                                if (!float.TryParse(responsesParameter.Value, out timeRangeNumber))
                                {
                                    ModelState.AddModelError("Question", "Choices must be numbers (decimal ok)");
                                }
                                break;
//.........这里部分代码省略.........
开发者ID:ucdavis,项目名称:NuSurvey,代码行数:101,代码来源:QuestionController.cs

示例3: ArchiveCategory

        public Category ArchiveCategory(IRepository repository, int id, Question questionToEdit)
        {
            var oldVersion = repository.OfType<Category>().Queryable.Where(a => a.Id == id).Single();
            var newVersion = new Category(oldVersion.Survey);

            newVersion.Rank = oldVersion.Rank;
            newVersion.LastUpdate = DateTime.Now;
            newVersion.CreateDate = newVersion.LastUpdate;

            foreach (var categoryGoal in oldVersion.CategoryGoals)
            {
                var categoryGoalToDuplicate = new CategoryGoal();
                Mapper.Map(categoryGoal, categoryGoalToDuplicate);
                newVersion.AddCategoryGoal(categoryGoalToDuplicate);
            }
            foreach (var question in oldVersion.Questions)
            {
                if (question.Id == questionToEdit.Id)
                {
                    //Ignore
                }
                else
                {
                    var questionToDuplicate = new Question(oldVersion.Survey);
                    questionToDuplicate.Order = question.Order;
                    questionToDuplicate.PrimaryPhoto = question.PrimaryPhoto;
                    foreach (var response in question.Responses)
                    {
                        var newResponse = new Response(); //If I don't do this, the old responses are *moved* here, not copied
                        Mapper.Map(response, newResponse);
                        questionToDuplicate.AddResponse(newResponse);
                    }
                    foreach (var photo in question.Photos) //This should work, but it needs to be tested.
                    {
                        var newPhoto = repository.OfType<Photo>().Queryable.Single(a => a.Id == photo.Id);
                        questionToDuplicate.Photos.Add(newPhoto);
                    }
                    Mapper.Map(question, questionToDuplicate);
                    newVersion.AddQuestions(questionToDuplicate);
                }

            }

            newVersion.IsActive = oldVersion.IsActive;
            newVersion.Name = oldVersion.Name;
            newVersion.Affirmation = oldVersion.Affirmation;
            newVersion.Encouragement = oldVersion.Encouragement;
            newVersion.DoNotUseForCalculations = oldVersion.DoNotUseForCalculations;
            newVersion.PreviousVersion = oldVersion;

            //*******************  SAVE
            repository.OfType<Category>().EnsurePersistent(newVersion);
            //*******************  SAVE

            oldVersion = repository.OfType<Category>().GetNullableById(id);
            oldVersion.IsCurrentVersion = false;

            //*******************  SAVE
            repository.OfType<Category>().EnsurePersistent(oldVersion);
            //*******************  SAVE

            return newVersion;
        }
开发者ID:ucdavis,项目名称:NuSurvey,代码行数:63,代码来源:ArchiveService.cs


注:本文中的Question.AddResponse方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。