ASP.net MVC - 将表单值绑定到不同的名称

发布于 2024-12-10 12:35:17 字数 1285 浏览 1 评论 0原文

我有一个视图,能够创建和编辑一个已分成部分视图的项目:

MainEditView.cshtml
_CreateChildDialog.cshtml
_EditChildDialog.cshtml

我为“创建”和“子”项目都有单独的 ViewModel:

public class CreateChildViewModel
{
    public string ItemText { get; set; }
}


public class EditChildViewModel 
{
    public string ItemText { get; set; }
}

因为“编辑”和“创建”对话框的部分视图都将被渲染在同一页面上,我会遇到表单 ID 和名称的冲突...因为它们都称为 ItemText。

是否可以在不编写自定义模型绑定程序的情况下自定义这些元素的绑定?

我想做类似的事情:

public class EditChildViewModel
{
    [BindFrom("EditItemText")]
    public string ItemText { get; set; }
}

或者将 ViewModel 属性重命名为更有意义:

public class EditChildViewModel 
{
    public string EditItemText { get; set; }
}

public class CreateChildViewModel
{
    public string CreateItemText { get; set; }
}

编辑 根据与达林的对话,我想把这一点说得更清楚。

我的父母有一个编辑操作。 当您编辑父级时,您永远不会在调用 ParentController.Edit 操作时创建新的子级或编辑子级。

我有一个单独的 Child 对象控制器,它有一个 Create 和 Edit 方法:

public class ChildController 
{
    public ActionResult Edit() {}
    public ActionResult Create() {}
}

当您编辑或创建子对象时,我使用 jQuery 调用异步发布到此控制器。基本上,我使用 jquery 对话框来创建/编辑一个子项,当我在对话框上单击“确定”时,该子项将立即保存。即使在单击父项的“编辑”操作的“保存”之前,也会发生这种情况。

I have a View that has the ability to both create and edit an item that I have separated into partial views:

MainEditView.cshtml
_CreateChildDialog.cshtml
_EditChildDialog.cshtml

I have separate ViewModels for both the Create and Child items:

public class CreateChildViewModel
{
    public string ItemText { get; set; }
}


public class EditChildViewModel 
{
    public string ItemText { get; set; }
}

Since the partial views for the Edit and Create dialog boxes will both be rendered on the same page, I will have a conflict for form id's and names...since they are both called ItemText.

Is it possible to customize the binding of these elements without writing a custom model binder?

I would like to do something like:

public class EditChildViewModel
{
    [BindFrom("EditItemText")]
    public string ItemText { get; set; }
}

Or does it just make more sense to rename the ViewModel properties to:

public class EditChildViewModel 
{
    public string EditItemText { get; set; }
}

public class CreateChildViewModel
{
    public string CreateItemText { get; set; }
}

EDIT
Based on converstation with Darin I want to make this a little more clear.

My Parent has an Edit action.
When you edit the Parent, you would never create a new child or edit a child when you are calling the ParentController.Edit action.

I have a separate controller for the Child object that has a Create and Edit method:

public class ChildController 
{
    public ActionResult Edit() {}
    public ActionResult Create() {}
}

I am using jQuery calls to asynchronously post to this controller when you edit or create a child. Basically I use a jquery dialog to create/edit a child that will get saved immediately when I click Ok on the dialog. This would happen even before clicking save for the Edit action of the parent.

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

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

发布评论

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

评论(1

昔梦 2024-12-17 12:35:17

我会使用编辑器模板。通常,您会将这两个视图模型打包到一个主视图模型中,该模型将由主视图使用:

public class MyViewModel
{
    public CreateChildViewModel Create { get; set; }
    public EditChildViewModel Edit { get; set; }
}

然后:

@model MyViewModel
@Html.EditorFor(x => x.Create)
@Html.EditorFor(x => x.Edit)

我将用相应的编辑器模板替换这两个部分视图(~/Views/Shared/EditorTemplates/CreateChildViewModel .cshtml~/Views/Shared/EditorTemplates/EditChildViewModel.cshtml)。编辑器模板将生成相应输入元素的正确名称和 ID。

就我个人而言,我更喜欢编辑器/显示模板而不是部分模板,因为它们可以更好地处理输入元素的命名。

I would use editor templates. Normally you would pack those two view models into a main view model which will be used by the main view:

public class MyViewModel
{
    public CreateChildViewModel Create { get; set; }
    public EditChildViewModel Edit { get; set; }
}

and then:

@model MyViewModel
@Html.EditorFor(x => x.Create)
@Html.EditorFor(x => x.Edit)

and I would replace the two partials by their corresponding editor templates (~/Views/Shared/EditorTemplates/CreateChildViewModel.cshtml and ~/Views/Shared/EditorTemplates/EditChildViewModel.cshtml). The editor templates will take of generating proper names and ids of the corresponding input elements.

Personally I tend to prefer editor/display templates instead of partials as they handle better naming of input elements.

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