Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mvc partial view post

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>
like image 407
foz1284 Avatar asked Nov 19 '10 22:11

foz1284


1 Answers

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" />
like image 194
rboarman Avatar answered Oct 06 '22 18:10

rboarman