0

* 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");
            }

}
2
  • Ok, you have said what your problem is, but what is your question? Commented May 2, 2014 at 16:25
  • NOTE: what im looking for in this process is get the ruta,Ficha and FechaInicio in the method, and call the view fichaconduce with those parameters Commented May 2, 2014 at 16:34

1 Answer 1

2

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(....)

For example ViewModel

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

}

Then in controller's 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]
public ActionResult MyControllerAction(MyViewModel myViewModel)
{
  //do whatever you want
}
Sign up to request clarification or add additional context in comments.

7 Comments

i cant not do it like this, becouse my view is based on a stored procedure and i can get the value of the model, the thing is that, when the textbox is inside the loop doesn give me any value
does your view have a viewmodel?
yes, look @model IList < TBWeb.Models.SP_GetALlRoutes_Result >
no, this textbox will get the date from the user, and then i will make a search with this date and the others parameters, so this textbox is not going to get value from the model, is from the user
even if you get the value from use you can always have it as a field in viewmodel and use textboxfor, else how do you expect the value to be binded and passed to controller?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.