表单操作发布 null
我的秃头增长速度超出了应有的速度。几天前,我第一次发布这个问题。我现在知道了问题所在并让它发挥作用......有点。另一个问题又出现了。
为了解决上一个问题,我手动创建了 requestedDays[{0}].DateOfLeave
名称,其中 {0}
是 Guid
。这使我的控制器能够正确接收 List<>的价值观。
使用 本文的方法,生成的名称是 requestedDays[{0}].DayRequested.DateOfLeave
我的控制器无法正确接收,因为该名称中包含类 DayRequested.DateOfLeave
。
[Authorize, HttpPost]
public ActionResult Create(LeaveRequest leaveRequest, List<DayRequested> requestedDays)
{
}
我试图找出手动生成的解决方法,但到目前为止我尝试过的方法都不起作用。 您可以在此处查看我的验证方法。我确实知道 Sanderson 关于验证的文章的第二部分 然而,验证未传递到方法中的内容是相当困难的。
这是我在部分视图中使用的 ViewModel。
public class LeaveRequestRow
{
public LeaveRequestRow(DayRequested dayRequested, List<SelectListItem> leaveRequestType)
{
this.DayRequested = dayRequested;
this.LeaveRequestType = leaveRequestType;
}
public List<SelectListItem> LeaveRequestType { set; get; }
public DayRequested DayRequested { set; get; }
}
有人对如何进行有任何想法吗?我应该将下拉列表转换为 jQuery 构建控件并停止使用 ViewModel 吗?
My baldness is growing more rapidly than it should be. I first posted this question a couple days ago. I now know the problem and have it working... sort of. Another problem surfaced in it's place.
To solve the previous problem, I manually created the name to requestedDays[{0}].DateOfLeave
where {0}
was a Guid
. This allowed my controller to properly receive the List<> of values.
Using this article's method, the name generated is requestedDays[{0}].DayRequested.DateOfLeave
which my controller doesn't properly receive because the name has the class in it, DayRequested.DateOfLeave
.
[Authorize, HttpPost]
public ActionResult Create(LeaveRequest leaveRequest, List<DayRequested> requestedDays)
{
}
I have tried to figure out work-arounds with the manual generation, but nothing I have tried works thus far. You can see my validation method here. I do know about the second part of Sanderson's article on validation however, it is quite hard to validate something that isn't being passed into the method.
This is my ViewModel I am using in my partial view.
public class LeaveRequestRow
{
public LeaveRequestRow(DayRequested dayRequested, List<SelectListItem> leaveRequestType)
{
this.DayRequested = dayRequested;
this.LeaveRequestType = leaveRequestType;
}
public List<SelectListItem> LeaveRequestType { set; get; }
public DayRequested DayRequested { set; get; }
}
Does anyone have any ideas on how to proceed? Should I convert my dropdown to a jQuery build control and stop using the ViewModel?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
绑定 1-N 个复杂类型的控制器参数可能有点棘手。
你的代码示例与我星期五一天结束时的大脑不相符,但我会尝试一下。
假设 LeaveRequest 类如下所示:
发布的表单键必须是:
这是简单的部分。 DayRequested 列表的 1-N 绑定变得有点奇怪。假设 DayRequested 对象如下所示:
您发布的表单键如下所示:
然后,默认的 MVC 绑定器应将所有 10 个表单值转换为您的两个方法参数……一个 POCO 和一个 POCO 列表。
Binding 1-N controller arguments of complex types can be kind of tricky.
Your code examples are not meshing with my fried end of day Friday brain but I'll give it a shot.
Assuming the LeaveRequest class looks like this:
The posted form keys must be:
That is the easy part. The 1-N binding of a list of DayRequested gets a little weird. Say the DayRequested object looks like this:
Your posted form keys look like:
The default MVC binder should then trun all 10 form values into your two method arguments ... a POCO and a List of POCOs.
我已经解决了这个问题,尽管没有我希望的那么优雅。所有
TextBoxFor
都必须更改为TextBox
以及执行此操作所需的其他更改。然后名称就正确生成了,我可以继续前进。这确实破坏了验证消息出现在字段旁边的能力,尽管 ValidationSummary 仍然有效。我稍后将致力于修复该问题,并在我的网站上发布代码示例和解决方案。I have solved this, though not as elegantly as I had hoped. All
TextBoxFor
had to be changed toTextBox
along with the addtional changes needed with doing this. The names then were correctly generated and I could move on. This did break the ability for the validation message to appear next to the field, thoughValidationSummary
still does work. I will be working on fixing that later on and post code samples and a solution on my website.