来自ActionLink的参数之一不会传递给控制器 查看控制器动作 public ActionResult Update(string nrAccount, DateTime? manualDate) { _docs.Update(manualDate, nrAccount); return RedirectToAction("Index"); } 中的Update

问题描述

我正在使用MVC5。

  • 参数(ObservableObject)之一传递给控制器​​
  • 一个参数(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"); } 中的Update

_docs

没有错误出现,什么也没有保存到数据库。当我调试代码时,看不到传递给变量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正确传递了。

用户可以选择一个日期DatePicker:

enter image description here

解决方法

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

更正您的表单操作,它也可能带有一个括号,即from collections import namedtuple Hook = namedtuple('Hook','mc_cmd callback run_after') class HookKinds: def __init__(self,before,after): self.before = before self.after = after class HookMgr: def __init__(self): self._hooks = {} def reg(self,mc_cmd,callback,run_after = False): hook = Hook(mc_cmd,run_after); if mc_cmd in self._hooks: if run_after: self._hooks[mc_cmd].after = hook else: self._hooks[mc_cmd].before = hook else: if run_after: self._hooks[mc_cmd] = HookKinds(None,hook) else: self._hooks[mc_cmd] = HookKinds(hook,None) self._hooks[mc_cmd] def unreg(self,run_after = False): if run_after: if self._hooks[mc_cmd].after is not None: self._hooks[mc_cmd].after = None else: if self._hooks[mc_cmd].before is not None: self._hooks[mc_cmd].before = None if (self._hooks[mc_cmd].before is None) and (self._hooks[mc_cmd].after is None): del self._hooks[mc_cmd] hooks = HookMgr() call = lambda x: x hooks.reg('EditFile',call,True) 而不是@Url.Action("Update","Home")

在控制器中的@Url.Action("Update","Home"))方法中添加HttpPost属性。

如注释中所述,将您的@ Action.Link替换为将导致GET的@ Action.Link,并将导致一个POST。

为了获得帐号,请为此添加一个隐藏字段。

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

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

viewModel

PaymentDate

控制器

    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");
        }
    }

下面以单击保存为例,将表格中的所有数据发布到服务器,并根据需要使用默认模型绑定将其放入模型中。