【问题标题】:How to edit grouped rows in ASP.NET MVC如何在 ASP.NET MVC 中编辑分组行
【发布时间】:2019-01-21 22:18:08
【问题描述】:

我正在尝试编辑编辑中显示的选定行中的所有行,但也按以下格式分组:

注意:[] 表示复选框,在数据库中为真或假


A.您在服用以下药物时是否有任何这些症状?

            Yes No   Sometimes   Not Sure  After 21 days   Indifferent

(i) 对乙酰氨基酚 [] [] [] [] [] [] ...


(ii) 奎宁 [] [] [] [] [] [] ...

B.下列药物用冷水泡眼睛会不会闪烁?

            Yes No   Sometimes   Not Sure  After 21 days   Indifferent

(i) 维生素 C [] [] [] [] [] [] ...


(ii) 奎宁 [] [] [] [] [] [] ,,,


我的 POCO 设计是这样的:

public class QuestionCategory
{
  public int QuestionCategoryID {get; set;}
  public string Name {get; set;}
  ...
}

public class Question
{
  public int ID {get; set;}
  public string Name {get; set;}
  public int QuestionCategoryID  {get; set;}
  ...
}

在视图中

我可以按 QuestionCategoryID 对问题进行分组,现在的问题是如何列出所有问题并一键编辑。


所以我决定为我想要实现的目标制作一个线框图。

【问题讨论】:

  • QuestionCategoryQuestion是哪一个?
  • 这些是表(1 个主表和 QuestionCategory 是一个查找表),我想在编辑模式下显示所有问题,但它必须按类别分组。这有意义还是我应该画一个草图?
  • 如果您可以将您的 POCO 与上面的示例相关联,将会很有帮助。但在编辑方面,只要您使用表单并修改模型的值,您就可以轻松地将其发布到控制器中的操作中。
  • 请检查问题,为了清楚起见,我已经更新了问题。

标签: c# asp.net list entity-framework razor


【解决方案1】:

请参阅下面的解决方案。我为问题、子问题和选项创建了 3 个表格。这些选项与父问题相关联(假设所有问题的选项都相同)。问题和子问题显示在文本框中(以使其可编辑)。 您可以对其进行修改以适应您的需要。希望这会有所帮助。

这是我的看法:

 @model List<QuestionsTest.Models.QuestionModel>    
    <form method="post">
      <table class="table">
        @for (int i = 0; i < Model.Count; i++)
        {
          <tr>
            <td colspan="2">
              @Html.HiddenFor(model => Model[i].Id, new { @class = "form-control" })
              <b>  @Html.TextBoxFor(model => Model[i].Question, new { @class = "form- 
              control" })</b>
            </td>
          </tr>

          for (int j = 0; j < Model[i].SubQuestions.Count; j++)
          {
            <tr>
              <td>
                @Html.HiddenFor(model => Model[i].SubQuestions[j].Id, new { @class = 
                "form-control" })
                @Html.HiddenFor(model => Model[i].SubQuestions[j].ParentQuestionId, new { 
                @class = "form-control" })
                @Html.TextBoxFor(model => Model[i].SubQuestions[j].SubQuestion1, new { 
                @class = "form-control" })
              </td>
              <td>
                @for (int k = 0; k < Model[i].Options.Count; k++)
                {
                  @Html.RadioButtonFor(model => Model[i].Options[k].QuestionOption, 
                  Model[i].Options[k].QuestionOption)@Model[i].Options[k].QuestionOption
                }

              </td>
            </tr>
          }

        }
      </table>
      <input class="btn-block btn-success" type="submit" value="Update" />

这是我的控制器:

public ActionResult Questions()
{
  var questions = _laraTestEntities.Questions.ToList();
  var questionModel = new List<QuestionModel>();

  questions.ForEach(q =>
  {

    var subQuestions = _laraTestEntities.SubQuestions.Where(s => s.ParentQuestionId == q.Id).ToList();
    var options = _laraTestEntities.Options.ToList();

    var model = new QuestionModel
    {
      Id = q.Id,
      Question = q.Question1,
      SubQuestions = subQuestions,
      Options = options
    };

    questionModel.Add(model);
  });
  return View(questionModel);
}

[HttpPost]
public ActionResult Questions(List<QuestionModel> Model)
{
  Model.ForEach(q =>
  {
    var questionDetail = _laraTestEntities.Questions.Find(q.Id);
    if (questionDetail != null)
    {
      questionDetail.Question1 = q.Question;
      q.SubQuestions.ForEach(s =>
      {
        var subQuestionDetail = _laraTestEntities.SubQuestions.Find(s.Id);
        if (subQuestionDetail != null)
        {
          subQuestionDetail.SubQuestion1 = s.SubQuestion1;
        }
      });

      _laraTestEntities.SaveChanges();

    }
  });

  return View(Model);
}

型号:

     public class QuestionModel
  {
    public int Id { get; set; }
    public string Question { get; set; }

    public List<SubQuestion> SubQuestions { get; set; }

    public List<Option> Options { get; set; }
  }

public class SubQuestion
  {
    public int Id { get; set; }
    public int ParentQuestionId { get; set; }
    public string SubQuestion1 { get; set; }

    public virtual QuestionCategory QuestionCategory { get; set; }
  }

 public class Option
  {
    public int Id { get; set; }
    public string OptionTitle { get; set; }
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多