如何将文本框值传递给 beginform routevalues

How to pass a textbox value to beginform routevalues

我的 mvc 中有一个文本框 view.I 想在 beginform 路由中传递文本框数据 values.how 吗?

查看:

@using (Html.BeginForm("Index", "InwardDetail", FormMethod.Post))
{
    <fieldset style="width:80%;margin-left:auto;margin-right:auto;margin-top:20px;min-width:60%">
         <div>
            <table class="tableView" style="width:100%">
                <tr>
                    <td>
                        @Html.DevExpress().Label(lbl=>{
                       lbl.Name = "lblFromDate";
                       lbl.Text = "From Date";
                   }).GetHtml()
                    </td>
                    <td>
                        @Html.TextBox("txtFromDate", value: DateTime.Now.ToString("dd/MM/yyyy"), htmlAttributes: new {id="fromDate", Class="textbox",style="width:70px"})
                    </td>

                    <td>
                        @Html.DevExpress().Button(but=>{
                       but.Name = "butView";
                       but.Text = "View";

                       but.UseSubmitBehavior = true;
                                       }).GetHtml()
                    </td>
                </tr>
                <tr>
                    <td colspan="9">
                        @Html.Partial("InwardDetailPartial")
                    </td>
                </tr>
            </table>
        </div>
    </fieldset>
}

控制器:

public ActionResult Index(string fDate)
        {
            _unitOfWork = new UnitOfWork();
            blInwarddetail = new InwardRegisterBL(_unitOfWork);
            var result = blInwarddetail.GetInwardList(fDate);
            return View("Index", result);
        }

如果我点击按钮,值应该传递给控制器​​。

您使用 @Html.TextBox("txtFromDate", ..) 意味着您使用 name="textFromDate" 生成输入。当您提交表单时,将发送 name/value 对表单控件 - 在您的情况下它将是 txtFromDate: 27/06/2015.

但是您 post 访问的方法没有名为 txtFromDate 的参数(只有一个名为 fDate 的参数)。您需要将方法更改为

[HttpPost]
public ActionResult Index(string txtFromDate)
{
  ....
}

然而,您的代码中有许多问题需要解决

首先,您应该创建一个视图模型来表示您想要在视图中display/edit的内容

public class FilterViewModel
{
  [Display(Name = "...")] // some user friendly display name
  [Required(ErrorMesage = "Please enter a valid date")]
  public DateTime Date { get; set; }
}

请注意,您显示的代码显示您输入的是日期,而不是字符串,因此 属性 应该是 DateTime(而不是 string)。这也确保您获得 属性 的客户端和服务器验证。我还给您的属性起一个更具描述性的名称 fDate(我无法开始猜测这可能意味着什么 - 也许是 FinishDate?)

在 GET 方法中

public ActionResult Index()
{ 
  FilterViewModel model = new FilterViewModel();
  model.Date = DateTime.Today;
  return View(model);
}

并在视图中

@model yourAssembly.FilterViewModel
@using (Html.BeginForm())
{
  ....
  @Html.LabelFor(m => m.Date)
  @Html.TextBoxFor(m => m.Date, "{0:dd/MM/yyyy}", new { @class = "textbox" })
  @Html.ValidationMessageFor(m => m.Date)
  ....
}

请注意,您现在已与模型强绑定 属性。第二个参数指定格式字符串。似乎没有必要覆盖 id 属性(这将是 id="Date"),并使用 @class = ".." 添加 class 名称。另请注意,由于您要添加 class 名称,因此您应该删除 style="width:70px" 并改用 css。您还应该删除 table 元素。 Html table 元素用于表格数据,不用于布局。

最后 post 方法将是

[HttpPost]
public ActionResult Index(FilterViewModel model)
{
  if (!ModelState.IsValid)
  {
    return View(model);
  }
  // model.Date will contain the value enter by the user
}

最后我会质疑这是否真的应该是 POST。形成代码,您似乎没有更改任何数据,也许这应该是 FormMethod.GET?或者更好的是,使用 ajax 根据过滤器更新当前页面。