asp.net - 如何从 @using (Html.BeginForm(

标签 asp.net asp.net-mvc-4 razor actionresult html.beginform

在我的 Razor View 中,我有一个简单的表单,它将接受用户的日期:

@using (Html.BeginForm("CancelByDate", "ClassExample", FormMethod.Post, new { id = "dateForm" }))
   {
      <input id="dateInput" type="date" width="10" class="date" />
      <input type="submit" value="Continue" />
   }
如何在我的 CancelByDate 操作中获取这些数据?
我尝试了几种方法:
    public ActionResult CancelByDate(DateTime dateInput)  // <---- This value will be null
    {
        return View();  
    }

    public ActionResult CancelByDate(String dateInput)  // <---- This value will be null
    {
        return View();  
    }

    public ActionResult CancelByDate(object dateInput) // <---- This value will be some object, but there is no way to find out what is the underlying type even with GetType(); 
    {
        return View();
    }
所以我想知道我错过了什么?

最佳答案

为日期定义一个名称,如下所示

<input id="dateInput" type="date" width="10" class="date" name="dateInput"/>

此名称应与 Controller 操作中的参数名称匹配。

如果是这样,该值将被自动绑定(bind)。

关于asp.net - 如何从 @using (Html.BeginForm(,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22397138/

相关文章:

razor - 模式在 Blazor 服务器端应用程序中不起作用

c# - 返回 MVC 中的倒数第二个 URL(返回应用了先前过滤条件的 View )?

c# - 用代码修复 'The DELETE statement conflicted with the REFERENCE constraint'错误

javascript - 基于 ASP 复选框隐藏 div

asp.net-mvc-4 - PayPal - 一起使用 REST API 和自适应支付

asp.net-mvc-4 - ASP.NET MVC DotNetOpenAuth 授权服务器操作方法

C# Web 应用程序 -> 代理所有请求 -> 从另一个 Web 应用程序返回内容(反向代理)

asp.net - 如何从 ASP.NET MVC 中的后台线程解析应用程序 URL?

c# - 如何在 Web Api 中使用 Api key 使用表单例份验证进行服务身份验证

c# - 将一天添加到模型数据中