MVC:添加住进一家集单选按钮的值,并保存到数据库 [英] mvc: Adding values of radio buttons checked into a collection and saving to database

查看:127
本文介绍了MVC:添加住进一家集单选按钮的值,并保存到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本框和一些问题与单选按钮的形式。我希望这种形式的用户可以检查每一个单选按钮。它是强制性的。我想借此检查是否是或否连同每个问题的名称或ID的值,并使用实体框架写入数据库。
如果我知道如何收集这些信息到一个集合,我可以做写入数据库。
我怎样才能把它们添加到收藏吗?

 公共类HomeController的:控制器
            {
                公众的ActionResult指数()
                {                }                [HttpPost]
                公众的ActionResult ProcessToDb()
                {                    返回查看();
                }
            }        @using(@ Html.BeginForm(ProcessToDb,家,FormMethod.Post))
        {
             < H2>我测试与LT; / H>            < D​​IV>邮箱地址:LT; / DIV>
            < D​​IV>
                 @ Html.TextBox(EmailAddress的,空,新的{@class =表格控})
            < / DIV>            < D​​IV>
                <标签=问题1>的你还好吗<?/标签>
                <输入类型=电台NAME =1组值=是>是
                <输入类型=电台NAME =1组值=否>无
            < / DIV>
            < D​​IV>
                <标签=问题2>是正确答案LT&;?/标签>
                <输入类型=电台NAME =组2值=是>是
                <输入类型=电台NAME =组2值=否>无
            < / DIV>
            < D​​IV>
                <标签=问题3>你追不上<?/标签>
                <输入类型=电台NAME =组3值=是>是
                <输入类型=电台NAME =组3值=否>无
            < / DIV>            < D​​IV>
                <输入类型=提交值=发送> <输入类型=复位>
            < / DIV>
        }


解决方案

您需要启动要显示/编辑,例如什么是视图模型重新presenting

 公共类AnswerVM
{
  公众诠释QuestionID {搞定;组; }
  公共字符串QuestionText {搞定;组; }
  公共BOOL答案{搞定;组; }
}

然后在GET方法

 公众的ActionResult指数()
{
  清单< AnswerVM>模型=新AnswerVM();
  //从数据库中,但是出于测试目的填充集合
  model.Add(新AnswerVM(){QuestionID = 1,QuestionText =你没事吧?});
  model.Add(新AnswerVM(){QuestionID = 2,QuestionText =答案是正确的呢?});
  返回查看(模型);
}

然后在视图

  @model名单< yourAssembly.AnswerVM>
@using(Html.BeginForm())
{
  的for(int i = 0; I< Model.Count;我++)
  {
    @ Html.HiddenFor(M = GT; M [​​] .QuestionID)
    @ Html.DisplayFor(M = GT; M [​​] .QuestionText)
    <标签>
      @ Html.RadioButtonFor(M = GT; M [​​]。答,真)
      <跨度>是< / SPAN>
    < /标签>
    <标签>
      @ Html.RadioButtonFor(M = GT; M [​​]。答,FALSE)
      <跨度&GT否LT; / SPAN>
    < /标签>
  }
  <输入类型=提交/>
}

和POST方法

 公众的ActionResult指数(名单< AnswerVM>模型)
{
  的foreach(模型AnswerVM的答案)
  {
    //访问QuestionID和应答属性并保存到数据库中
  }
  //重定向
}

I have a form with a textbox and some questions with radio buttons. I want users of this form to check each of the radio buttons. It is mandatory. I want to take the values checked whether Yes or No together with the name or Id of each question and write to the database using entity framework. I can do the writing to the database If I know how to collect this information into a collection. How can I add these to a collection please?

         public class HomeController : Controller
            {
                public ActionResult Index()
                {

                }

                [HttpPost]
                public ActionResult ProcessToDb()
                {

                    return View();
                } 
            }

        @using (@Html.BeginForm("ProcessToDb", "Home", FormMethod.Post))
        { 
             <h2>My Test</h2>

            <div>Email Address</div>
            <div>
                 @Html.TextBox("EmailAddress", null, new { @class = "form-control" })
            </div>

            <div>
                <label for="question1">Are you ok?</label>
                <input type="radio" name="group1"  value="Yes">Yes
                <input type="radio" name="group1" value="No">No
            </div>
            <div>
                <label for="question2">Is the answer correct?</label>
                <input type="radio" name="group2" value="Yes">Yes
                <input type="radio" name="group2" value="No">No
            </div>
            <div>
                <label for="question3"> Did you overtake him ?</label>
                <input type="radio" name="group3" value="Yes">Yes
                <input type="radio" name="group3" value="No">No
            </div>

            <div>
                <input type="submit" value="Send"> <input type="reset">
            </div>
        }

解决方案

You need to start with a view model representing what you want to display/edit, for example

public class AnswerVM
{
  public int QuestionID { get; set; }
  public string QuestionText { get; set; }
  public bool Answer { get; set; }
}

Then in the GET method

public ActionResult Index()
{
  List<AnswerVM> model = new AnswerVM();
  // populate the collection from the database but for testing purposes
  model.Add(new AnswerVM() { QuestionID = 1, QuestionText = "Are you ok?" });
  model.Add(new AnswerVM() { QuestionID = 2, QuestionText = "Is the answer correct?" });
  return View(model);
}

Then in the view

@model List<yourAssembly.AnswerVM>
@using (Html.BeginForm())
{
  for(int i = 0; i < Model.Count; i++)
  {
    @Html.HiddenFor(m => m[i].QuestionID)
    @Html.DisplayFor(m => m[i].QuestionText)
    <label>
      @Html.RadioButtonFor(m => m[i].Answer, true)
      <span>Yes</span>
    </label>
    <label>
      @Html.RadioButtonFor(m => m[i].Answer, false)
      <span>No</span>
    </label>
  }
  <input type="submit" />
}

And the POST method

public ActionResult Index(List<AnswerVM> model)
{
  foreach (AnswerVM answer in model)
  {
    // access the QuestionID and Answer properties and save to the database
  }
  // redirect
}

这篇关于MVC:添加住进一家集单选按钮的值,并保存到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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