0

これらは私のPOCOクラスです:

public class PollOptions
    {
        public int Id { get; set; }
        public virtual Polls Polls { get; set; }
        public string Answer { get; set; }
        public int Votes { get; set; }
    }
public class Polls
    {
        public int Id { get; set; }
        public string Question { get; set; }
        public bool Active { get; set; }
        public virtual ICollection<PollOptions> PollOptions { get; set; }
    }

そして、この方法でモデルを Index ビューに渡しました。

public ActionResult Index()
        {
           var poll = db.Polls.Include("PollOptions").Where(x => x.Active).ToList();
           return View(model);
        }

インデックスビューでは、ユーザーに投票してもらいたいのですが、以下は投票のセクションです:

@section Polling{
   @foreach (var question in Model.Polls)
    {
        <h3>@question.Question</h3>
       <ul class="pollUnordedList">
       @foreach(var answer in question.PollOptions)
        {
            <li><input type="radio" name="opt" id="@answer.Id" value="1"/><span>@answer.Answer</span></li>
        }
        </ul>
    }
    @using (Html.BeginForm(actionName: "Radio", controllerName: "Home",routeValues: new { id = 2, value = 5 }))
    {
        <p><input type="submit" class="btn btn-primary" value="ارسال نظر" /></p>
    }
}

私の行動方法:

public void Radio(int Id=0,int value=0)
       {
           PollOptions opt = db.PollOptions.Find(Id);
           db.Entry(opt).State = System.Data.EntityState.Modified;
           opt.Votes += value;
           db.SaveChanges();
       }

ルート値として渡すための radioButton の値と ID を取得するにはどうすればよいですか。たとえば、上記のルートで Id と値の値は 2,5 です

4

2 に答える 2

1

jQuery AJAX を使用して問題を解決しました:

@using (Html.BeginForm(actionName: "Radio", controllerName: "Home")) {
        foreach (var question in Model.Polls)
        {
            <h3>@question.Question</h3>
            <ul class="pollUnordedList">
                @foreach (var answer in question.PollOptions)
                {
                    <li>
                        @*@Html.RadioButton("options",false,new {id=@answer.Id})<span>@answer.Answer</span>*@
                        @Html.RadioButton("options",answer.Id)<span>@answer.Answer</span>
                    </li>
                }
            </ul>
        }
         <p>
            <input type="submit" id="btnVote" class="btn btn-primary" value="ارسال نظر" />
        </p>
    }

そして私のアクションメソッド:

[HttpPost]
       public ActionResult Radio(int options)
       {
           var option = db.PollOptions.Find(options);
           if (option != null) option.Votes++;
           db.SaveChanges();
           return RedirectToAction("Index");
       }

Daniel Liuzzi に感謝します。ラジオ ボタンが選択されている場合、フォームが投稿されたときに送信されるラジオ ボタンの値であるため、foreach は必要ありません。

于 2013-03-04T19:17:36.457 に答える
0

すべてのラジオ ボタンに同じ名前を使用します。

@using (Html.BeginForm(actionName: "Radio", controllerName: "Home")) {
    foreach (var question in Model.Polls) {
        <h3>@question.Question</h3>
        foreach (var answer in question.PollOptions) {
            <label>
                <input type="radio" name="answers" value="@answer.Id" />
                @answer.Answer
            </label>
        }
    }

    <p><input type="submit" class="btn btn-primary" value="ارسال نظر" /></p>
}

次に、アクション メソッドで、選択した回答を配列として受け取ります。

[HttpPost]
public ActionResult Radio(int[] answers)
{
    // TODO: Save your answers
}

編集

値が常に1に等しいというあなたのコメントによると、それを渡す意味はありません。選択したすべてのラジオ ボタンの値を 1と仮定するだけです。アクション メソッドは次のようになります。

[HttpPost]
public ActionResult Radio(int[] answers)
{
    foreach (var id in answers)
    {
        var option = db.PollOptions.Find(id);
        if (option != null) option.Votes++;
        db.SaveChanges();
    }
}
于 2013-03-04T17:50:37.453 に答える