2016-07-09 6 views
-1

の質問を送ってください。答えはラジオボタン で質問を表示して送信してください。 私はクイズフォームを持っています。このようないくつかのクラスを作った:リストを送って、asp.net mvc

public class Question 
{ 

    public int Qid { get; set; } 
    public string Questionstext { get; set; } 
    public int selected { get; set; } 
    public List<Qitem> lst { get; set; } 

} 
public class Qitem 
{ 
    public int id { get; set; } 
    public string txt { get; set; } 

} 

、私はビューに質問のリストを送信します。

答えが「選択中に入れなければなりませんどのように私はそれを表示し、解答を提出することができ、
List<Question> llst = new List<Question> 
     { 
      new Question 
      { 
       Qid = 1, 
       Questionstext = "is it true?", 
       lst = new List<Qitem> 
       { 
        new Qitem 
        { 
         txt = "yes", 
         id = 1 
        }, 
        new Qitem 
        { 
         id = 2, 
         txt = "no" 
        } 
       } 
      }, 
      new Question 
      { 
       Qid = 1, 
       Questionstext = "is it true 2?", 
       lst = new List<Qitem> 
       { 
        new Qitem 
        { 
         txt = "yes2", 
         id = 3 
        }, 
        new Qitem 
        { 
         id = 4, 
         txt = "yes3" 
        } 
       } 
      } 
     }; 

     return View(llst); 

'質問のプロパティ。 私の問題はビューと特にラジオボタンです。

答えて

2

あなたのビューには、(Indexは、このビューを生成するために使用されるGETメソッドと同じ名前であると仮定した場合)に戻って掲載します

@model List<Question> 
@using (Html.BeginForm()) 
{ 
    for (int i = 0; i < Model.Count; i++) 
    { 
     @Html.HiddenFor(m => m[i].Qid) 
     <h2>@Model[i].Questionstext</h2> 
     foreach (var answer in Model[i].lst) 
     { 
      <label> 
       @Html.RadioButtonFor(m => m[i].selected, answer.id, new { id = "" }) 
       <span>@answer.txt </span> 
      </label> 
     } 
    } 
    <input type="submit" value="Save" /> 
} 

でなければなりません。

public ActionResult Index(List<Question> model) 

サイドノート:あなたはまた、我々は、複数のリストを送信する場合、我々はまた、リストを格納し、ビューで使用するViewbagまたはTempDataをを使用することができ、同様に掲示するQuestionstext値をしたい@Html.HiddenFor(m => m[i].Questionstext)

+0

が含まれている場合ビューへの別のモデル –

+0

@HimaanSingh、 'TempData'はビューにデータを送るのには適していませんし、OPがやっているように強く型付けされたモデルでは' ViewBag'を提案するのは実用的です –

関連する問題