MVC3 @ Html.RadioButtonfor通数据视图控制器 [英] MVC3 @Html.RadioButtonfor Pass data from view to controller

查看:153
本文介绍了MVC3 @ Html.RadioButtonfor通数据视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我想是创建一个反馈表与它有四个答案使用MVC(单选按钮)的问题。

Basically what I am trying is to create a feedback form with a question which has four answers (Radio Button) using MVC.

在View :我能够获得鉴于与单选按钮的问题和答案,每个答案

In View: I am able to get questions and answers in view with radiobutton to each answer.

在控制器:我很困惑在这里

我想送所选问题和放大器;有从View回答到控制器。

I would like to send selected question & there answers from View to controller.

我的看法

@model  IEnumerable SQLOperation.Models.QuestionClass.Tabelfields


@{int i = 0;}


     @foreach (var item in Model)
     {
     using (Html.BeginForm("Question", "home", new {email=item.Email}))
     {   

         @Html.DisplayFor(modelItem => item.QuestionName,item)

         <br /><br />   
         if (item.Option1 != "")
         {
                    @Html.RadioButtonFor(modelItem =>item.SelectedOption, item.Option1, item)                
                    @Html.DisplayFor(modelItem => item.Option1)
                    <br /><br />                
         }

         if (item.Option2 != "")
         {
                    @Html.RadioButtonFor(modelItem => item.SelectedOption, item.Option2)              
                    @Html.DisplayFor(modelItem => item.Option2)
                    <br /><br />
         }

         if (item.Option3 != "")
         {           
                    @Html.RadioButtonFor(modelItem =>item.SelectedOption, item.Option3)              
                    @Html.DisplayFor(modelItem => item.Option3)
                    <br /><br />
         }


         if (item.Option4 != "")
         {
                    @Html.RadioButtonFor(modelItem =>item.SelectedOption, item.Option4)             
                    @Html.DisplayFor(modelItem => item.Option4) 
                    <br /><br />     
         }
         i = (Int16)i + 1;


         if (Model.Count() == i)
         {
                <input name="btnsumbit" type="submit" value="Submit Feedback" 
                style="font-family:Segoe UI Light;font-size:medium;"/>
         }
     }
 }

我的控制器

 [HttpGet]
    public ActionResult Question(string email)
    {
        var tf = new QuestionClass.Tabelfields();

        IList<QuestionClass.Tabelfields> viewmodel = new List<QuestionClass.Tabelfields>();
        var q = QuestionClass.getallQuestion(email).ToList();

        foreach (SQLOperation.Models.Question item in q)
        {
            QuestionClass.Tabelfields viewItem = new QuestionClass.Tabelfields();

            viewItem.Email = item.Email;
            viewItem.QuestionID = item.QuestionID;
            viewItem.QuestionName = item.QuestionName;
            viewItem.Option1 = item.Option1;
            viewItem.Option2 = item.Option2;
            viewItem.Option3 = item.Option3;
            viewItem.Option4 = item.Option4;                
            viewmodel.Add(viewItem);
        }
         return View(viewmodel);
    }

    [HttpPost, ActionName("Question")]
    public void Question(string Email,QuestionClass.Tabelfields tf)
    {

    }

我的模型

public class QuestionClass

{

  public static FeedbackDatabaseDataContext context = new FeedbackDatabaseDataContext();
    public class Tabelfields : Question
    {
        //public decimal QuestionID { get; set; }
        //public string Email { get; set; }
        //public string QuestionName { get; set; }
        //public string Option1 { get; set; }
        //public string Option2 { get; set; }
        //public string Option3 { get; set; }
        //public string Option4 { get; set; }
        public int SelectedOption { get; set; }
    }


    public static List<Question> getallQuestion(string email)
    {
        var list = (from q in context.Questions where q.Email == @email select q);

        return list.ToList();
    }

}

我没有得到这个问题,它的选择与控制相应的电子邮件选项。

I didn't get the question and it's selected option with appropriate Email in controller.

怎样才能得到它的控制器?

How can I get it in controller?

推荐答案

要,开始与包括断点在您的视图,并检查是否正在通过与否的值。

To, start with include a breakpoint in your view and check if the values are being passed or not.

接下来,你的方法签名从

Next, change your method signature from

 public void Question(string Email,QuestionClass.Tabelfields tf)

 public void Question(string email, QuestionClass.Tabelfields tf)

另外,我看你已经使用了一些奇怪的语法(考虑M还新MVC),您可以简单用这种

Also, I see you have used some strange syntax ( considering m also new to MVC ) where you could have simply used this

@Html.DisplayFor(modelItem => item.QuestionName)

而不是这种

@Html.DisplayFor(modelItem => item.QuestionName,item)

和也,我已经看到您资料,我知道你是新来的StackOverflow,所以欢迎计算器!你可能需要阅读&放大器; 的为好。干杯!

and also, I have seen you profile, I know you are new to StackOverflow, so Welcome to StackOverflow ! you might want to read this & this as well. Cheers !

更新:

您正在使用这个用于显示一个单选按钮...

You are using this for displaying a radio button...

@Html.RadioButtonFor(modelItem =>item.SelectedOption, item.Option1, item)

什么item.Option1,我可以看到你注释掉Opion1,2,3,4。或者你还在使用它们?

what is item.Option1, I can see you have commented out Opion1,2,3,4. Or are you still using them ?

实现,这将是这样的一个非常简单的方法。

A very simple way of implementing this would be something like this

@Html.RadioButtonFor(modelItem =>item.SelectedOption, "Option1")
@Html.RadioButtonFor(modelItem =>item.SelectedOption, "Option2")
@Html.RadioButtonFor(modelItem =>item.SelectedOption, "Option3")

或者你也可以使用,

or you can also use,

@Html.RadioButtonFor(modelItem =>item.SelectedOption, item.Option1)
@Html.RadioButtonFor(modelItem =>item.SelectedOption, item.Option2)
@Html.RadioButtonFor(modelItem =>item.SelectedOption, item.Option3)

规定值item.Option1 / 2/3/4是存在的,所有的人都不同。

provided values exist in item.Option1/2/3/4 and are all of them are different.

这篇关于MVC3 @ Html.RadioButtonfor通数据视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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