在ASP.NET MVC数组中存储单选按钮值

时间:2013-12-03 21:11:58

标签: c# jquery asp.net asp.net-mvc asp.net-mvc-5

我正在进行在线测试,在部分视图中显示问题,如下所示:

http://i52.photobucket.com/albums/g28/conor2099/example.png

问题图像和5个相关的单选按钮在foreach循环中调用。视图中相关的代码部分是:

查看:

 <div class="row">
    <div class="span5 offset5">
        <div class="row">

            @foreach (var item in Model.questions)
            {
                ViewBag.Name = item.question1;     

                <div class="row">
                    <div id="question">
                        <img src="@Url.Content(@item.question1)" style="position:absolute; left:600px;" alt="" />
                    </div>
                    <div id="rdioPos" style="position:absolute; left:600px;">
                        <label class="radio inline"><input type="radio" name=@ViewBag.Name value="1"> 1</label>
                        <label class="radio inline"><input type="radio" name=@ViewBag.Name value="2"> 2</label>
                        <label class="radio inline"><input type="radio" name=@ViewBag.Name value="3"> 3</label>
                        <label class="radio inline"><input type="radio" name=@ViewBag.Name value="4"> 4</label>
                        <label class="radio inline"><input type="radio" name=@ViewBag.Name value="5"> 5</label>              
                    </div>
                </div>

                <script type="text/javascript">


                </script>
            }
           </div>
        </div>
       </div>

 <div class="row">
        <div class="span5" style="position:absolute; top:450px; left:1100px">
            @Ajax.ActionLink("Next", "Next", new { id = @ViewData["scenNo"]},

        new AjaxOptions()
        {
            HttpMethod = "GET", // HttpMethod to use, GET or POST
            UpdateTargetId = "divScenarios", // ID of the HTML element to update
            InsertionMode = InsertionMode.Replace // Replace the existing contents
        }, new { @class = "btn btn-primary" })
       </div>
    </div>
</div>

当单击下一个按钮时,我想读取每组按钮中的值,存储在一个数组中并传递给控制器​​以更新数据库。部分视图也会更新。

以下是来自控制器的方法:

控制器

public PartialViewResult Next(int id)
    {

        id = id + 1;

        if (id > noOfScens)
            id = 1;

        scenario scenario = db.scenarios.Find(id);
        ViewData["scenNo"] = id;
        return PartialView("_Scen", scenario);
    }

以下是场景模型:

型号

namespace TestingApp.Models
{
using System;
using System.Collections.Generic;

public partial class scenario
{
    public scenario()
    {
        this.questions = new HashSet<question>();
    }

    public int scenario_id { get; set; }
    public string scenario1 { get; set; }
    public string scenario_type { get; set; }
    public int scenario_group_id { get; set; }
    public List<int> ansVals { get; set; }

    public virtual ICollection<question> questions { get; set; }
}

}

可以这样做还是我需要重新考虑我的做法?我已经尝试了所有我能想到的东西并且非常困难。任何帮助都将非常感激。

0 个答案:

没有答案