View to Controller in mvc3

2019-03-01 13:38发布

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) 
    { 
    }

1条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-03-01 14:33

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.

查看更多
登录 后发表回答