简体   繁体   English

我无法在我的控制器MVC中获得文本框的值

[英]I can not get the value of a textbox in my controller MVC

* Hello i have long time trying it and i dont get any results, my problem is the next: On my controller i can not get the value of the textbox, but if I put the form and the textbox out of the loop i get the value.* *你好我有很长时间尝试它,我没有得到任何结果,我的问题是下一个:在我的控制器上我不能得到文本框的值,但如果我把表单和文本框放出循环我得到了值。*

** this is my view:** **这是我的观点:**

@for (int r = 0; r <= Model.Count-1;r++)
{
    i = i + 1;

    <tr>

         @using (Html.BeginForm("Result","Ruta", new { ruta=Model [r].descrip ,ficha =Model [r].ficha }, FormMethod.Post))
         {
             if (i == 1)
             {
            @Html.TextBox("FechaInicio")

             }

        <td>
            @Html.DisplayFor(modelItem => Model[r].ruta)


        </td>
        <td>
            @Html.DisplayFor(modelItem => Model[r].descrip)
        </td>
        <td>
            @Html.DisplayFor(modelItem => Model[r].ficha)

        </td>

        <td>
        <input type="Submit" value="Consultar" style=" height :20px;  font-size:12px; "/>





        </td>

         }
    </tr>

My Controller: 我的控制器:

 [HttpPost]
        public ActionResult Result(string ficha, string ruta)
        {

            Utility.Fecha1 = Request.Form ["FechaInicio"];
           if (string.IsNullOrEmpty(ficha) || string.IsNullOrEmpty(ruta) || string                      .IsNullOrEmpty ( Utility .Fecha1) )
            {
                return RedirectToAction("FichaConduces", "Ruta", new { ficha = ficha, ruta = ruta,fecha=Utility .Fecha1 });



            }
            else
            {
                return View("index");
            }

}

Why don't you do the simple way? 你为什么不这么简单?

Create a view based on view model, and create textboxs for view model's field in view then you can easily get values you want from the form. 基于视图模型创建视图,并在视图中为视图模型的字段创建文本框,然后您可以轻松地从表单中获取所需的值。

And, when you create textbox it should be @Html.TextBoxFor(....) 而且,当你创建文本框时,它应该是@Html.TextBoxFor(....)

For example ViewModel 例如ViewModel

public class MyViewModel
{
   public string MyField1{get; set;}
   public string MyField2{get; set;}

}

Then in controller's HttpGet 然后在控制器的HttpGet中

[HttpGet]
public ActionResult MyControllerAction()
{
   var myViewModel = new MyViewModel();
   return View(myViewModel);
}

in view 在视野中

@model MyViewModel
@using(Html.BeginForm("Action","Controller",FormMethod.Post))
{
   @Html.TextBoxFor(m=>m.MyField1)
   ....
}

controller's HttpPost 控制器的HttpPost

[HttpPost]
public ActionResult MyControllerAction(MyViewModel myViewModel)
{
  //do whatever you want
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我想将文本框值发送到MVC中的控制器 - I want to send the textbox value to the controller in MVC 如何获取基于控制器名称的文本框值? - How can I get the textbox value based on the name in controller? 我无法在MVC 4 C#中的视图上获得文本框的值 - I can not get the value of a textbox on a view in mvc 4 C# 如何在ASP.NET MVC 4中的控制器中更改文本框的值 - How can I change value of textbox in controller in ASP.NET MVC 4 如何获取文本框或下拉列表的值并发送到ASP.NET MVC4中的数据库 - How can I get value of textbox or dropdownlist and send to database in ASP.NET MVC4 获取MVC中dropdownlist和textbox的值 - Get value of dropdownlist and textbox in mvc 如何从时间选择器中获取所选时间并将其分配给TextBox值? - How can I get the selected time from my timepicker and assign it to a TextBox value? 如何将文本框值传递给@ html.hidden()以转到我的控制器MVC - how to pass textbox value to @html.hidden() to go to my controller MVC 如何获取DropDownList选定的值,并将其发送到ASP.NET MVC 4中的控制器并使用JavaScript? - How can I get a DropDownList selected value and send it to the controller in ASP.NET MVC 4 and using JavaScript? 如何使用json将web.api中的返回值获取到mvc控制器? - How can i get returned Value from web.api to mvc controller with json?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM