[英]I can not get the value of a textbox in my controller MVC
*你好我有很長時間嘗試它,我沒有得到任何結果,我的問題是下一個:在我的控制器上我不能得到文本框的值,但如果我把表單和文本框放出循環我得到了值。*
**這是我的觀點:**
@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>
我的控制器:
[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");
}
}
你為什么不這么簡單?
基於視圖模型創建視圖,並在視圖中為視圖模型的字段創建文本框,然后您可以輕松地從表單中獲取所需的值。
而且,當你創建文本框時,它應該是@Html.TextBoxFor(....)
例如ViewModel
public class MyViewModel
{
public string MyField1{get; set;}
public string MyField2{get; set;}
}
然后在控制器的HttpGet中
[HttpGet]
public ActionResult MyControllerAction()
{
var myViewModel = new MyViewModel();
return View(myViewModel);
}
在視野中
@model MyViewModel
@using(Html.BeginForm("Action","Controller",FormMethod.Post))
{
@Html.TextBoxFor(m=>m.MyField1)
....
}
控制器的HttpPost
[HttpPost]
public ActionResult MyControllerAction(MyViewModel myViewModel)
{
//do whatever you want
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.