Asp.net mvc中的模型绑定单选按钮列表 [英] model binding radio button list in Asp.net mvc

查看:205
本文介绍了Asp.net mvc中的模型绑定单选按钮列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码,我希望每个问题和选中的单选按钮答案都需要保存到数据库中。将数据发布到控制器值时会变为空..请建议我..



如果您有任何解决方案,请按照此处的测试示例项目链接:

dropbox.com/s/v33xcy3c2x72keo/TestQuestions.rar?dl=0



请帮我解决问题

提前致谢。



型号:

This is my code and i want the every question and selected radio button answer need to be save into database. While posting the data to the controller values become null.. Please suggest me..

If you have any solution please follow the test sample project link here:
dropbox.com/s/v33xcy3c2x72keo/TestQuestions.rar?dl=0

Please help me to resolve the solution
Thanks in Advance.

Model :

public class Question
{
    public decimal TestId { get; set; }
    public decimal SkillId { get; set; }
    public decimal ID { get; set; }
    public string QuestionText { get; set; }
    public List<OptionList> Options { set; get; }        
}

public class OptionList
{
    public string Option1 { get; set; }
    public string Option2 { get; set; }
    public string Option3 { get; set; }
    public string Option4 { get; set; }
}



控制器:


Controller :

public ActionResult Successpage()
{
   decimal testid = 1;
   var items = (from p in db.ALBulkTestDetails
                where p.TestID == testid
                select new Question
                {
                    ID = p.QuestionID,
                    QuestionText = p.Question,
                    SelectedAnswer = null,
                    Options = (from a in db.ALBulkTestDetails
                               join b in db.ALBulkTestDetails
                               on new { aa = a.QuestionID, a.SkillID } equals new { aa = b.QuestionID, b.SkillID }
                              where b.QuestionID == p.QuestionID &amp;&amp; b.SkillID == p.SkillID
                               select new OptionList
                               {
                                   Option1 = b.Option1,
                                   Option2 = b.Option2,
                                   Option3 = b.Option3,
                                   Option4 = b.Option4
                               }).ToList()
                }).Take(3).ToList();
   return View(items);
}

推荐答案

Step1 :定义问题和选项模型

Step1: Define Question and Option model
public class Question
{
    public decimal TestId { get; set; }
    public decimal SkillId { get; set; }
    public decimal ID { get; set; }
    public string QuestionText { get; set; }
    public List<OptionList> Options { set; get; }
	public string SelectedAnswer { get; set; }	
}

public class OptionList
{
    public string Option1 { get; set; }
    public string Option2 { get; set; }
    public string Option3 { get; set; }
    public string Option4 { get; set; }
}



Step2 :设计视图页面并添加viewmodel顶视图页。


Step2: Design View page and add your viewmodel top of view page.

@model yourproject.model.Question; // Change as per your requirement
foreach (var temp in Model.Options)
{
	<div>
		@Html.RadioButtonFor(model => Model.SelectedAnswer, "1"})
		@Html.Label(temp.Option1)
	</div>
	<div>
		@Html.RadioButtonFor(model => Model.SelectedAnswer, "2")
		@Html.Label(temp.Option2)
	</div>
	<div>
		@Html.RadioButtonFor(model => Model.SelectedAnswer, "3")
		@Html.Label(temp.Option3)
	</div>
	<div>
		@Html.RadioButtonFor(model => Model.SelectedAnswer, "4")
		@Html.Label(temp.Option4)
	</div>
}



Step3 :在Controller中添加一个动作来获取价值


Step3: Add an action in Controller to get value

[HttpPost]
public AcitonResult Index(Question qModel)
{
    if (ModelState.IsValid)
    {
        //TODO: Save your model and redirect 
	string temp = qModel.SelectedAnswer;
    }

    return View();
}


这篇关于Asp.net mvc中的模型绑定单选按钮列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆