【问题标题】:One of the parameters from ActionLink doesn't pass to controllerActionLink 的参数之一未传递给控制器
【发布时间】:2020-10-22 14:44:28
【问题描述】:

我正在使用 MVC5。

  • 参数之一 (nrAccount) 传递给控制器​​
  • 另一个参数 (manualDate) 没有。

我做错了什么?

查看

<form action="@Url.Action("Update", "Home"))" method="post">
  <tr>
    <td>
      @Html.DisplayFor(modelItem => item.Account)
    </td>
    <td class="choice">
      @Html.TextBoxFor(modelItem => item.PaymentDate, new { @type = "date", @class = "form-control datepicker" })
    </td>
    <td>
      @Html.ActionLink("Save", "Update", "Home", new { nrAccount = item.Account, manualDate = item.PaymentDate },null)
    </td>
  </tr>
</form>

控制器动作

public ActionResult Update(string nrAccount, DateTime? manualDate)
{
    _docs.Update(manualDate, nrAccount);

    return RedirectToAction("Index");
}

_docsUpdate

public void Update(DateTime? manualDate, string nrAccount)
{
    var toUpdate = GetByAccount(nrAccount);

    toUpdate.DATE_MANUAL = manualDate;
    _context.SaveChanges();
}

public IEnumerable<CustomersTable> GetAll()
{
    return _context.CustomersTable;
}

public CustomersTable GetByAccount(string nrAccount)
{
    return GetAll().FirstOrDefault(k => k.FOPKTO_ACCOUNT_ == nrAccount);
}

没有错误出现,只是没有任何东西保存到数据库中。当我调试我的代码时,我看不到传递给变量manualDate 的日期,不知道为什么。但nrAccount 传递正确。

用户可以选择日期一DatePicker:

【问题讨论】:

  • 你能检查一下 @Html.ActionLink("Save", "Update", "Home", new { nrAccount = item.Account, manualDate = item.PaymentDate },null)&lt;input type="submit" value="Submit"/&gt; 的变化吗?
  • 如果您将日期时间更改为不可为空,它会起作用吗?
  • @noobprogrammer 好的,但是如何添加路由参数来提交?
  • @Ben 我想我做不到。我返回到我的模型(然后将其传递给视图)一些日期列,这些列在数据库中为空,因此我需要使它们在模型中为空。
  • 还是不行。我需要将值从文本框传递到控制器。 ManualDate 列中 db 中的几乎所有值都是空值,这就是为什么这里返回空值并将这个空值传递给控制器​​。问题是如何将值(用户选择的日期)从这个 TextBox 传递给控制器​​,我该怎么做?

标签: javascript c# asp.net-mvc


【解决方案1】:

问题在于,当您应该使用 POST 时,您正在使用 GET 请求。并且您的 GET 包含帐户但不包含日期文本框中的日期,因为它仅包含初始值而不是用户将其更改为的值。因此,要解决此问题,您应该通过进行以下更改将您的 GET 更改为帖子。

更正您的表单操作,它也有一个括号,即@Url.Action("Update", "Home") 而不是@Url.Action("Update", "Home"))

HttpPost 属性添加到控制器中的Update 方法。

正如 cmets 中提到的,将您的 @Action.Link 替换为 GET,而提交按钮将导致 POST。

这样您就可以获得帐号,为此添加一个隐藏字段。

最后,由于 textboxfor 的工作方式,您需要更改更新方法以接收具有 AccountPaymentDate 属性的对象。

这里有一些与你的非常相似的演示代码,向你展示我的意思。

视图模型

    public class HomeViewModel
    {
        public string Account { get; set; }
        public DateTime? PaymentDate { get; set; }
    }

控制器

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            var item = new HomeViewModel
            {
                Account = "theAccountNumber",
                PaymentDate = null
            };

            var viewModel = new List<HomeViewModel>()
            {
                item
            };

            return View(viewModel);
        }       

        [HttpPost]
        public ActionResult Update(HomeViewModel item)
        {
            //call your _docs method here  
            return RedirectToAction("Index");
        }
    }

视图(注意帐户的隐藏字段)

@model List<AccountDate.Controllers.HomeViewModel>

<table>
    @foreach (var item in Model)
    {
        <form action="@Url.Action("Update", "Home")" method="post">
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.Account)
                    @Html.HiddenFor(modelItem => item.Account)      

                </td>
                <td class="choice">
                    @Html.TextBoxFor(modelItem => item.PaymentDate, new { @type = "date", @class = "form-control datepicker" })
                </td>
                <td>
                    <input type="submit" value="Save" />                     
                </td>
            </tr>
        </form>
    }
</table>

按照我的示例,当单击“保存”时,表单中的所有数据都将发布到服务器,并根据需要使用默认模型绑定放入模型中。

【讨论】:

  • 我尝试使用 BeginForm。不幸的是,仍然出错并返回空值。我将与我公司的开发人员交谈。无论如何,谢谢你的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-05
  • 1970-01-01
  • 2012-11-16
  • 2018-07-07
  • 2017-08-06
  • 2018-07-14
相关资源
最近更新 更多