在 ASP.NET MVC 中使用 EditCreate.ascx 部分编辑和创建视图
如果您查看 NerdDinner 创建和编辑晚餐的示例,您会发现他们使用部分(ViewUserControl 或 ASCX)DinnerForm 将创建和编辑晚餐的功能放入一个文件中,因为它本质上是相同的,并且他们使用 RenderPartial( “晚餐表格”)。
这种方法对我来说似乎很好,但我遇到了一个问题,您必须向 Form 标记添加附加路由值或 html 属性。
这会自动获取当前操作和控制器:
<% using (Html.BeginForm()) { %>
但是,如果我使用另一个允许传入 enctype 或任何其他属性的 BeginForm() 重载,我必须这样做:
<% using ("Create", "Section", new { modal = true }, FormMethod.Post, new { enctype = "multipart/form-data" }))
正如您所看到的,我们失去了自动检测的能力我们在哪个 View 中调用 RenderPartial("OurCreateEditFormPartial")。我们不能在其中包含硬编码值,因为在编辑视图中此回发将失败或不会回发到正确的控制器操作。
这种情况我该怎么办?
If you look at the NerdDinner example of creating and editing dinners then you see they use a partial (ViewUserControl or ASCX) DinnerForm to put the functionality of creating and editing dinners into one file because it is essential the same and they use it using RenderPartial("DinnerForm").
This approach seems fine for me but I've run into a problem where you have to add additonal route values or html properties to the Form tag.
This picks up the current action and controller automatically:
<% using (Html.BeginForm()) { %>
However, if I use another BeginForm() overload which allows to pass in enctype or any other attribute I have to do it like this:
<% using ("Create", "Section", new { modal = true }, FormMethod.Post, new { enctype = "multipart/form-data" }))
and as you can see we lose the ability to automatically detect in which View we are calling RenderPartial("OurCreateEditFormPartial"). We can't have hardcoded values in there because in Edit View this postback will fail or won't postback to the right controller action.
What should I do in this case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
添加以下 HTML 帮助器怎么样:
然后你可以这样做:
它不是 100% 完美,但你也可以添加一个额外的 HTML 帮助器,这将使其更加简化:
让我知道这是否有帮助。
干杯
What about adding the following HTML helpers:
Then you could go something like this:
Its not 100% perfect but you could also add an additional HTML helper which would streamline it a little more:
Let me know if this helps.
Cheers
我在一个旧线程中回答,因为它在谷歌搜索中排名第二,而我正在寻找同样的东西:)在这个SO帖子上找到:
Html.BeginForm 和添加属性
使用 MVC3(不确定 MVC2),您可以为操作和控制器传递 null 并获取 BeginForm() 的默认路由使用。
干杯!
I'm answering in an old thread because it came up number two on a Google search whilst I was looking for the same thing :) Found on this SO post:
Html.BeginForm and adding properties
With MVC3 (not sure about MVC2) you can pass null in for the action and controller and get the default routes that BeginForm() would use.
Cheers!