仅提交选定的字段作为 asp.net mvc 中的属性

发布于 2024-08-20 12:41:30 字数 261 浏览 4 评论 0原文

我有一个编辑部门详细信息的视图。在此视图中,列出了所有员工,用户可以选择在该部门工作的员工。有没有办法只提交选定的员工作为department.Employees(不使用javascript)?

public ActionResult(Department department)
{
    Save(department); // department.Employees should only contain checked employees
}

I have a view for editing department details. In this view, all employees are listed and user selects which employees work in this department. Is there a way to submit only the selected employees as department.Employees (without using javascript)?

public ActionResult(Department department)
{
    Save(department); // department.Employees should only contain checked employees
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

你另情深 2024-08-27 12:41:30

如果没有 javascript 来操作表单,您就无法真正从页面中选择“提交”内容,但为什么需要这样做呢?

如果您只是从列表中选择员工,您可以循环模型中的所有员工并为他们创建一个复选框(您可能需要手动渲染如下 - Html.Checkbox 似乎不适用于相同的多个复选框name)

<input type="checkbox" value="<%= Html.Encode(employee.Id)%>" name="employeeForDepartment" />

然后在您的操作中您可以执行以下操作:

public ActionResult Bla(int departmentId, int[] employeeForDepartment) .....

假设员工 ID 是 int。然后您可以相应地处理该列表(仅提交选中的员工)。它没有您之前使用的模型绑定,但在这种情况下,它并没有真正以这种方式进行处理。

You cant really choose what you 'submit' from the page without javascript to manipulate the form, but why do you need to do it like that?

If you just selecting employees from a list, you could just loop around all the employees in the model and create a check box for them (you may have to manually render as below - Html.Checkbox doesnt seem to work for multiple check boxes of same name)

<input type="checkbox" value="<%= Html.Encode(employee.Id)%>" name="employeeForDepartment" />

Then in your action you can do something like this:

public ActionResult Bla(int departmentId, int[] employeeForDepartment) .....

assuming that the employee id is an int. Then you can process that list accordingly (only checked employees will be submitted). It doesnt have the model binding you were using previously, but in this case, it doesnt really lend its self to processing in that way.

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