MVC局部视图发布 [英] mvc partial view post

查看:122
本文介绍了MVC局部视图发布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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. 分支机构列表局部视图应该位于公司或分支机构目录中吗?
  2. 删除分支动作应该驻留在公司还是分支控制者中?

我认为分支列表应该在分支目录中,并调用分支控制器,但是当将部分视图加载到公司详细信息视图中时,如何获取它呢?

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?

希望如此

谢谢

标记

        <% 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>

推荐答案

您需要用单独的表单标签将要提交的每组字段括起来.每页上可以有多个表单标签.实际上,您可能希望每个局部视图都具有自己的表单标签,该表单标签可以提交给不同的控制器操作.

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" />

这篇关于MVC局部视图发布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆