【发布时间】: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");
}
_docs的Update
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)和<input type="submit" value="Submit"/>的变化吗? -
如果您将日期时间更改为不可为空,它会起作用吗?
-
@noobprogrammer 好的,但是如何添加路由参数来提交?
-
@Ben 我想我做不到。我返回到我的模型(然后将其传递给视图)一些日期列,这些列在数据库中为空,因此我需要使它们在模型中为空。
-
还是不行。我需要将值从文本框传递到控制器。 ManualDate 列中 db 中的几乎所有值都是空值,这就是为什么这里返回空值并将这个空值传递给控制器。问题是如何将值(用户选择的日期)从这个 TextBox 传递给控制器,我该怎么做?
标签: javascript c# asp.net-mvc