表单操作发布 null

发布于 2024-10-03 14:43:05 字数 1595 浏览 4 评论 0原文

我的秃头增长速度超出了应有的速度。几天前,我第一次发布这个问题。我现在知道了问题所在并让它发挥作用......有点。另一个问题又出现了。

为了解决上一个问题,我手动创建了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

晨曦÷微暖 2024-10-10 14:43:05

绑定 1-N 个复杂类型的控制器参数可能有点棘手。

你的代码示例与我星期五一天结束时的大脑不相符,但我会尝试一下。

假设 LeaveRequest 类如下所示:

public class LeaveRequest {
    public string Text { get; set; }
    public string Number { get; set; }
}

发布的表单键必须是:

leaveRequest.Text
leaveRequset.Number

这是简单的部分。 DayRequested 列表的 1-N 绑定变得有点奇怪。假设 DayRequested 对象如下所示:

public class DayRequested  {
    public string Words { get; set; }
    public string Data { get; set; }
}

您发布的表单键如下所示:

requestedDays[0].Data
requestedDays[0].Words
requestedDays[1].Data    
requestedDays[1].Words
requestedDays[2].Data    
requestedDays[2].Words
requestedDays[3].Data    
requestedDays[3].Words

然后,默认的 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:

public class LeaveRequest {
    public string Text { get; set; }
    public string Number { get; set; }
}

The posted form keys must be:

leaveRequest.Text
leaveRequset.Number

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:

public class DayRequested  {
    public string Words { get; set; }
    public string Data { get; set; }
}

Your posted form keys look like:

requestedDays[0].Data
requestedDays[0].Words
requestedDays[1].Data    
requestedDays[1].Words
requestedDays[2].Data    
requestedDays[2].Words
requestedDays[3].Data    
requestedDays[3].Words

The default MVC binder should then trun all 10 form values into your two method arguments ... a POCO and a List of POCOs.

晚风撩人 2024-10-10 14:43:05

我已经解决了这个问题,尽管没有我希望的那么优雅。所有 TextBoxFor 都必须更改为 TextBox 以及执行此操作所需的其他更改。然后名称就正确生成了,我可以继续前进。这确实破坏了验证消息出现在字段旁边的能力,尽管 ValidationSummary 仍然有效。我稍后将致力于修复该问题,并在我的网站上发布代码示例和解决方案。

I have solved this, though not as elegantly as I had hoped. All TextBoxFor had to be changed to TextBox 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, though ValidationSummary still does work. I will be working on fixing that later on and post code samples and a solution on my website.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文