首页 > Web开发 > 详细

.NET MVC Form Radio 通过强类型向控制器传值

时间:2015-11-17 16:27:44      阅读:434      评论:0      收藏:0      [点我收藏+]

想要通过强类型传递Radio的值到控制器,在视图中不能使用foreach的方式取得各个Radio项,要使用for的形式,代码如下:

Models:

public class Fo
    {
        public string foName { get; set; }
        public List<Models.a> aList { get; set; }
    }

public class a
    {
        public string name { get; set; }
        public int age { get; set; }
        public int option { get; set; }
        public List<Option> optionList { get; set; }
    }

public class Option
    {
        public string option { get; set; }
        public int score { get; set; }

        public Option()
        {

        }
        public Option(string o, int s)
        {
            this.option = o;
            this.score = s;
        }
    }

Controllers:

public ActionResult Index()
        {
            Models.Fo fo = new Models.Fo();
            fo.foName = "xxx";
            fo.aList = new List<Models.a>();
            for (int i = 0; i < 3; i++)
            {
                Models.a model = new Models.a();
                model.name = "name" + i.ToString();
                model.age = 30 + i;
                model.optionList = new List<Models.Option>();
                model.optionList.Add(new Models.Option("", 10));
                model.optionList.Add(new Models.Option("", 20));
                fo.aList.Add(model);
            }

            return View(fo);
        }

        public string Regist(Models.Fo model)
        {

            return "ok";
        }

Views:

@model MvcApplication1.Models.Fo

@using (Html.BeginForm("Regist", "Home", FormMethod.Post))
{
    <p>@Html.TextBoxFor(m => m.foName)</p>
    for (int i = 0; i < Model.aList.Count; i++)
{
        <p>
            @Html.TextBoxFor(m => m.aList[i].name)
            @for (int l = 0; l < Model.aList[i].optionList.Count; l++ )
            {
                <span>
                    @Html.RadioButtonFor(m => Model.aList[i].option,Model.aList[i].optionList[l].score)
                    @Model.aList[i].optionList[l].option
                </span>
            }
        </p>
    }
    <input type="submit" id="btnsubmit" value="提交" class="submit" />
}

L‘ 2015/11/17 15:52

.NET MVC Form Radio 通过强类型向控制器传值

原文:http://www.cnblogs.com/Velo/p/4971956.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!