mvc部分视图帖子

发布于 2024-10-03 15:22:39 字数 1232 浏览 7 评论 0原文

我有一个公司对象,其中包含分支对象列表,

我的公司视图(驻留在公司目录中)有一个强类型分支列表视图(驻留在分支目录中),

分支视图中的每个分支都有一个删除按钮我想将其发布到分支控制器中的删除操作。

目前调用的删除操作是公司控制器中的操作

(公司和分支机构都有删除操作)

我相信我理解它这样做的原因,但是在这种情况下最佳实践是什么...... .

  1. 分支列表部分视图应该驻留在公司目录还是分支目录中?
  2. 删除分支操作应该驻留在公司还是分支控制器中?

我认为分支列表应该位于分支目录中并调用分支控制器,但是当部分视图加载到公司详细信息视图中时,如何让它执行此操作?

希望这是有道理的,

谢谢,

马克

        <% foreach (var item in Model) { %>

    <tr>
        <td>
                    <form action="Edit" method="get">
            <input type="submit" value="Edit" id="Submit1" /> 
            <input type="hidden" name="id" value="<%= item.Id %>" /> 
        </form>
        |
        <form action="Branch" method="get">
            <input type="submit" value="Details" id="Submit2" /> 
            <input type="hidden" name="id" value="<%= item.Id %>" /> 
        </form>
        |
        <form action="BranchDelete" method="post">
            <input type="submit" value="BranchDelete" id="Submit1" /> 
            <input type="hidden" name="id" value="<%= item.Id %>" /> 
        </form>

I have a company object which has a list of branch objects,

my company view(residing in the company directory) has a strongly typed branch list view (residing in the branch directory)in it,

each branch in the branch view has a delete button which I want to post to a delete action in the branch controller.

at present the invoked delete action is the one in the company controller

(there is a delete action in both company and branch)

I believe I understand the reason it is doing what it is, however what is the best practice in this situation....

  1. should the branch list partial view reside in the company or branch directory?
  2. should the delete branch action reside in the company or the branch controller?

I would think the branch list should be in the branch directory and call the branch controller, but how do I get it to do this when the partial view is loaded into the company details View?

Hope that made sense,

Thanks,

Mark

        <% foreach (var item in Model) { %>

    <tr>
        <td>
                    <form action="Edit" method="get">
            <input type="submit" value="Edit" id="Submit1" /> 
            <input type="hidden" name="id" value="<%= item.Id %>" /> 
        </form>
        |
        <form action="Branch" method="get">
            <input type="submit" value="Details" id="Submit2" /> 
            <input type="hidden" name="id" value="<%= item.Id %>" /> 
        </form>
        |
        <form action="BranchDelete" method="post">
            <input type="submit" value="BranchDelete" id="Submit1" /> 
            <input type="hidden" name="id" value="<%= item.Id %>" /> 
        </form>

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

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

发布评论

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

评论(1

魔法唧唧 2024-10-10 15:22:39

您需要用单独的表单标签包围要提交的每组字段。每页可以有多个表单标签。事实上,您可能希望每个分部视图都有自己的表单标签,提交到不同的控制器操作。

将部分视图放在最有意义的地方。文件位置与表单从浏览器提交回来的方式无关。

您可以像这样发布到不同的控制器。一名担任分公司财务总监,一名担任公司财务总监。

<% using (Html.BeginForm("RemoveBranch", "Branch", FormMethod.Post, new { @class = "branchform" }))
   {

       Html.RenderPartial("~/Views/Branch/BranchView.ascx");    

}%>

<% using (Html.BeginForm("RemoveCompany", "Company", FormMethod.Post, new { @class = "companyform" }))
   {

       Html.RenderPartial("~/Views/Company/CompanyView.ascx");    

}%>

在每个视图或部分视图中,您只需要一个提交按钮:

<input type="submit" value="Delete" />

You need to surround each set of fields that you want submitted with a separate form tag. You can have more than one form tag per page. In fact, you might want each partial view to have its own form tag that submits to a different controller action.

Put the partial views wherever makes most sense. The file location has nothing to do with how the form is submitted back from the browser.

You can post to different controllers like this. One posts to the Branch controller and one posts to the Company controller.

<% using (Html.BeginForm("RemoveBranch", "Branch", FormMethod.Post, new { @class = "branchform" }))
   {

       Html.RenderPartial("~/Views/Branch/BranchView.ascx");    

}%>

<% using (Html.BeginForm("RemoveCompany", "Company", FormMethod.Post, new { @class = "companyform" }))
   {

       Html.RenderPartial("~/Views/Company/CompanyView.ascx");    

}%>

In each view or partial view, all you need is a submit button:

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