亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 ASP.NET MVC 中編輯分組行

如何在 ASP.NET MVC 中編輯分組行

C#
青春有我 2021-11-28 18:43:08
我正在嘗試編輯編輯中顯示的選定行中的所有行,但也按以下格式分組:注意:[] 代表復選框,在數據庫中為 true 或 falseA. 您在服用以下藥物時是否有這些癥狀?            Yes No   Sometimes   Not Sure  After 21 days   Indifferent(i) 撲熱息痛 [] [] [] [] [] [] ...(ii) Quninne [] [] [] [] [] [] ...B. 用冷水和以下藥物一起使用時,您的眼睛會閃爍嗎?            Yes No   Sometimes   Not Sure  After 21 days   Indifferent(i) 維生素 C [] [] [] [] [] [] ...(ii) Quninne [] [] [] [] [] [] ,,,我的 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 對問題進行分組,現在的問題是如何列出所有問題并單擊一個按鈕進行編輯。所以我決定制作一個我想要實現的線框。
查看完整描述

1 回答

?
慕森王

TA貢獻1777條經驗 獲得超3個贊

請參閱下面的我的解決方案。我為問題、子問題和選項創建了 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; }

  }


查看完整回答
反對 回復 2021-11-28
  • 1 回答
  • 0 關注
  • 190 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號