Mvc partial view message

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

the view of my company (located in the company directory) has a strongly typed representation of the list of branches (located in the branch directory),

each branch in the branch has a delete button that I want to send to the delete action in the branch controller.

the deletion action specified in the company controller is currently being called

(there is a deletion action both in the company and in the branch)

I suppose I understand the reason that he does what it is, but what is the best practice in this situation ....

  • if a partial view of the list of branches is in the directory of the company or branch?
  • should the delete action be deleted in the company or branch controller?

I would have thought that the list of branches should be in the branch directory and call the branch controller, but how do I get it when a partial view is loaded into the View company information?

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> 
+4
asp.net-mvc partial-views
source share
1 answer

You need to surround each set of fields that you want to submit using a separate form tag. A page can have more than one form tag. In fact, you might want each partial view to have its own form tag, which is sent to another controller action.

Put partial views where it makes sense. The location of the file has nothing to do with how the form is sent back from the browser.

You can send messages to different controllers like this. One position for the branch controller and one position for 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" /> 
+6
source share

All Articles