MVC4从视图到控制器获取表数据

时间:2022-10-18 09:33:15

How can I get the data which is bottom left hand side of the image from view to controller when I press to submit button ?

当我按下提交按钮时,如何从视图到控制器获取图像左下角的数据?

Link for image

链接图像

 public ActionResult EnterInformation1()
        {
           // the ??? is the data from the view;
           TempData['abc'] = ???
     
           return RedirectToAction("Paper");
        }

I did look up reference before I ask.thanks

在我问之前,我确实查了一下参考文献。谢谢

1 个解决方案

#1


1  

If you want to send data from view to controller all you need is to do next step: 1) Send data from controller to view. For example you want to display the content of a model wich is List

如果要将数据从视图发送到控制器,您只需要执行下一步:1)将数据从控制器发送到视图。例如,您想要显示List的模型的内容

public ActionResult ShowList()
{
   List<string> lst = new List<string>() {"1", "2", "3", ,"4"};
   return View("MyListView", lst);
}

2) Display this data on the View using html helpers with postfix "for" (for example, textBoxFor, hiddenFor, EditorFor, DisplayFor...) and place them inside form tag

2)使用带有后缀“for”的html助手(例如,textBoxFor,hiddenFor,EditorFor,DisplayFor ...)在View上显示此数据,并将它们放在表单标签中

    @model List<string> 

   <form action="GetDataFromView" method="post">
   <table>
      <tr>
         <th>some header</th>
      <tr>
       @foreach (var item in Model) {
       <tr>
           <td>
               @Html.DisplayFor(m=> item)
           </td>
       </tr>
       }
    </table>
    </form>

3) Write Method in controller wich will get these data

3)控制器中的写入方法将获得这些数据

public ActionResult GetDataFromView(List<string> model)
{
  //do what you want with data sended from view to controller
  // which stored in parameter model
  return View("someView");
}

#1


1  

If you want to send data from view to controller all you need is to do next step: 1) Send data from controller to view. For example you want to display the content of a model wich is List

如果要将数据从视图发送到控制器,您只需要执行下一步:1)将数据从控制器发送到视图。例如,您想要显示List的模型的内容

public ActionResult ShowList()
{
   List<string> lst = new List<string>() {"1", "2", "3", ,"4"};
   return View("MyListView", lst);
}

2) Display this data on the View using html helpers with postfix "for" (for example, textBoxFor, hiddenFor, EditorFor, DisplayFor...) and place them inside form tag

2)使用带有后缀“for”的html助手(例如,textBoxFor,hiddenFor,EditorFor,DisplayFor ...)在View上显示此数据,并将它们放在表单标签中

    @model List<string> 

   <form action="GetDataFromView" method="post">
   <table>
      <tr>
         <th>some header</th>
      <tr>
       @foreach (var item in Model) {
       <tr>
           <td>
               @Html.DisplayFor(m=> item)
           </td>
       </tr>
       }
    </table>
    </form>

3) Write Method in controller wich will get these data

3)控制器中的写入方法将获得这些数据

public ActionResult GetDataFromView(List<string> model)
{
  //do what you want with data sended from view to controller
  // which stored in parameter model
  return View("someView");
}