查看到控制器在MVC3 [英] View to Controller in mvc3

查看:134
本文介绍了查看到控制器在MVC3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,从视图将值传递给控制器​​

下面是我的查看

  @model IEnumerable的< SQLOperation.Models.QuestionClass.Tabelfields>@ {
     ViewBag.Title =问题;
 } < H3>问题< / H3 GT&; @ {INT I = 0;} @foreach(以型号VAR项)
 {  使用(Html.BeginForm(问题,家))
  {
      @ Html.DisplayFor(modelItem => item.QuestionName)
      @ Html.HiddenFor(M = GT; item.QuestionID)
         < BR />< BR />
      如果(item.Option1!=)
      {
        @ Html.RadioButtonFor(M = GT; item.SelectedOption,item.Option1,项)
        @ Html.DisplayFor(modelItem => item.Option1)
                    < BR />< BR />
      }      如果(item.Option2!=)
      {
         @ Html.RadioButtonFor(M = GT; item.SelectedOption,item.Option2,项)
         @ Html.DisplayFor(modelItem => item.Option2)
                    < BR />< BR />
      }      如果(item.Option3!=)
      {
        @ Html.RadioButtonFor(M = GT; item.SelectedOption,item.Option3,项)
        @ Html.DisplayFor(modelItem => item.Option3)
                    < BR />< BR />
      }
      如果(item.Option4!=)
      {
       @ Html.RadioButtonFor(M = GT; item.SelectedOption,item.Option4,项)
                    @ Html.DisplayFor(modelItem => item.Option4)
                < BR />< BR />
      }
      I =(Int16的)I + 1;
      如果(Model.Count()==ⅰ)
      {
                <输入名称=btnsumbit类型=提交值=提交反馈
                风格=FONT-FAMILY:UI濑越轻; FONT-SIZE:中等;/>
      }
  }
 }

我的控制器

  [HTTPGET]
    公众的ActionResult问题(字符串email)
    {
        变种TF =新QuestionClass.Tabelfields();        IList的< QuestionClass.Tabelfields>视图模型=新的List< QuestionClass.Tabelfields>();
        变种Q = QuestionClass.getallQuestion(电子邮件).ToList();        的foreach(在Q SQLOperation.Models.Question项)
        {
            QuestionClass.Tabelfields viewItem =新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);
        }
         返回视图(视图模型);
    }    [HttpPost,ActionName(问题)
    公共无效问题(IEnumerable的< QuestionClass.Tabelfields>的项目)
    {    }

我的型号

 公共类QuestionClass
{
    公共静态FeedbackDatabaseDataContext背景=新FeedbackDatabaseDataContext();    公共类Tabelfields:问题
    {
        //公共小数QuestionID {搞定;组; }
        //公共字符串电子邮件{获得;组; }
        //公共字符串QuestionName {搞定;组; }
        //公共字符串选项1 {搞定;组; }
        //公共字符串选项2 {搞定;组; }
        //公共字符串2选项{搞定;组; }
        //公共字符串选项4 {搞定;组; }
        公共字符串SelectedOption {搞定;组; }
    }
    公共静态列表<问题> getallQuestion(字符串email)
    {
        VAR名单=(从context.Questions q其中q.Email == @Email选择Q);        返回list.ToList();
    }
 }

但是我得到了在控制器的项目 NULL。

  [HttpPost,ActionName(问题)
    公共无效问题(IEnumerable的< QuestionClass.Tabelfields>的项目)
    {
    }

如果我改变我查看&放然而,控制器下面,我从数据库中获取控制器最后一个值

查看:

  @foreach(以型号VAR项)
{
  使用(Html.BeginForm(问题,家,新的{电子邮件= item.Email,Q = item.QuestionID}))
  {
      @ Html.DisplayFor(modelItem => item.QuestionName)
      @ Html.HiddenFor(M = GT; item.QuestionID)
       。       。       。
       。
  }
}

控制器:

<$ P为$ P> [HttpPost,ActionName(问题)
    公共无效的问题(字符串email,INT Q)
    {
    }

我收到电子邮件和Q值

让我怎么能得到所有值即QuestionId,电子邮件,Questionname和它的控制器中选择合适的值(单选)?

即。在继控制器:

  [HttpPost,ActionName(问题)
    公共无效问题(IEnumerable的&LT; QuestionClass.Tabelfields&GT;的项目)
    {
    }


解决方案

您需要索引的HTML *对于项目本身;

  @ Html.RadioButtonFor(M = GT; M [​​] .SelectedOption,item.Option3,项)

为了让事情simplier,我可能会摆脱的foreach &安培;和单独 I 声明,并使用以下;

  @for(INT I = 0; I&LT; Model.Count;我++)
{
    @ Html.HiddenFor(M = GT; M [​​] .QuestionID)
    @ Html.RadioButtonFor(M = GT; M [​​] .SelectedOption,型号[I] .Option3,型号[I])
}

等。

这样的索引将导致HTML与索引完整呈现:

 &LT;输入类型=隐藏名称= [0]'QuestionId/&GT;
&LT;输入类型=隐藏名称= [1]QuestionId/&GT;
&LT;输入类型=隐藏名称= [2]QuestionId/&GT;

而不是你在做什么目前,它最终呈现如此;

 &LT;输入类型=隐藏的名字='QuestionId/&GT;
&LT;输入类型=隐藏的名字='QuestionId/&GT;
&LT;输入类型=隐藏的名字='QuestionId/&GT;

如果没有索引,每个表单字段被赋予相同的名称,所以你控制器会只有一个返回去思考。

I have a problem on passing values from view to controller

Here is my view:

 @model  IEnumerable<SQLOperation.Models.QuestionClass.Tabelfields>

@{
     ViewBag.Title = "Question";
 }

 <h3> Question</h3>

 @{int i = 0;}

 @foreach (var item  in Model)
 {

  using (Html.BeginForm("Question", "Home"))
  {
      @Html.DisplayFor(modelItem => item.QuestionName)
      @Html.HiddenFor(m => item.QuestionID)      
         <br /><br />   
      if (item.Option1 != "")
      {
        @Html.RadioButtonFor(m => item.SelectedOption, item.Option1, item)                
        @Html.DisplayFor(modelItem => item.Option1)
                    <br /><br />                
      }

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

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


      if (item.Option4 != "")
      {
       @Html.RadioButtonFor(m => item.SelectedOption, item.Option4, item)             
                    @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;"/>
      }
  }
 }

My controller :

[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(IEnumerable<QuestionClass.Tabelfields> items)
    {

    }

My Model:

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 string 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();
    }
 }

however I get NULL in "items" in controller.

[HttpPost, ActionName("Question")] 
    public void Question(IEnumerable<QuestionClass.Tabelfields> items) 
    { 
    }

Whereas if I change my View & Controller to below , I get last value from database in controller

View:

@foreach (var item in Model)
{
  using (Html.BeginForm("Question", "home", new { email=item.Email,  q=item.QuestionID}))
  {
      @Html.DisplayFor(modelItem => item.QuestionName)
      @Html.HiddenFor(m => item.QuestionID)
       .

       .

       .
       . 
  }
}

Controller:

[HttpPost, ActionName("Question")] 
    public void Question(string email,int q) 
    {   
    }

I get values in email and q

so how can I get all values i.e. QuestionId,Email,Questionname and it's appropriate selected value (radiobutton) in controller ?

i.e. in Following Controller:

[HttpPost, ActionName("Question")] 
    public void Question(IEnumerable<QuestionClass.Tabelfields> items) 
    { 
    }

解决方案

You need to index the Html.*For items as such;

@Html.RadioButtonFor(m => m[i].SelectedOption, item.Option3, item)

To make things simplier, i'd probably get rid of the foreach & and separate i declaration and use the following;

@for(int i=0; i < Model.Count; i++)
{
    @Html.HiddenFor(m => m[i].QuestionID) 
    @Html.RadioButtonFor(m => m[i].SelectedOption, Model[i].Option3, Model[i])
}

etc.

Indexing like this will cause the html to be rendered with the indexing intact:

<input type='hidden' name=[0].'QuestionId' />
<input type='hidden' name=[1].'QuestionId' />
<input type='hidden' name=[2].'QuestionId' />

Rather than what you're doing currently, which ends up rendering as so;

<input type='hidden' name='QuestionId' />
<input type='hidden' name='QuestionId' />
<input type='hidden' name='QuestionId' />

Without the indexing, each form field is given the same name, so you're controller is going to think only one was returned.

这篇关于查看到控制器在MVC3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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