mvc partial view post - asp.net-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....
should the branch list partial view reside in the company or branch directory?
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>

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

Related

How I can pass data in view using Post method and ViewBag?

First view send form
foreach p in Model
<form method="post">
<td class="pricelight"><input type="radio" name="price"value="#p.PriceLight.ToString("")"/>#p.PriceLight.ToString("")</td>
<td class="fromtabletime"><input type="radio" name="price"value="#p.PriceOk.ToString("")/>#p.PriceOk.ToString("")</td>
<td class="totable"><input type="radio" name="price"value="#p.PriceHigh.ToString("")/>#p.PriceHigh.ToString("")</td>
<td class="button13"><button type="submit" asp-action="Buy" asp-route-id="#p.PhoneId" asp-controller="Home">Next</button></td>
</form>
Then I need to handle data in post
Controller
[HttpPost]
public ActionResult Buy(int?id,Order order,string price,#p.PriceLight,#p.PriceOk,#p.PriceHigh)
{
string ryt={price};
ViewBag.price=ryt;
context.Orders.Add(order);
context.SaveChanges();
ViewBag.phoneId=id;
return View("Thanks, " + order.OrderName);
}
Then I want to get it in new view
#using System.Linq;
#using System;
#model List<Searchphone>
#{
ViewBag.Title = "PhoneId";
}
<h2 class="orderinformation">Order Data</h2>
<form method="post" class="formpass">
<h3> <input type="hidden" name="#ViewBag.price" value="#ViewBag.price" />#ViewBag.price</h3>
<br>
<input type="hidden" id="PhoneId"value="#ViewBag.PhoneId" name="PhoneId">
<label for="OrderSurName">Surname</label><br>
<input type="text" placeholder="Enter Surname" name="OrderSurName" required><br>
<label for="OrderName">Имя</label><br>
<input type="text" placeholder="Enter Name" name="OrderName" required><br>
<button type="submit" class="button7">To pay</button>
</form>
1) I dont get it in view, only in URL. How I can get it in view using Post method?Why does not work viewbag?
2) I want to compare price value with #p.PriceLight from the first view in post method. But how I can pass #p.PriceLight in method?
1) I dont get it in view, only in URL. How I can get it in view using Post method?Why does not work viewbag?
You should first debug to check if the ViewBag has a value in your controller action. Since the ViewBag are used in hidden input, you can't directly see it on page, you can press f12 in your view to check if the ViewBag is rendered. I used your codes and can get it in the view.
2) I want to compare price value with #p.PriceLight from the first view in post method. But how I can pass #p.PriceLight in method?
The form only submit the checked radio button value, if you want to get the value of PriceLight, define a hidden field in the form in your first view.

POST and GET in the same form created by BeginForm method

How to create a form that has a search function (pull data from the database) AND a submit function (add data to the database) at the same time using the BeginForm() method? I am reviewing the overloads on MSDN and I don't seem to find one.
Code:
#using (Html.BeginForm()){
<table>
#*Bunch of textboxes and dropdown lists*#
</table>
<div id=" buttonHolder">
<input id="Search" type="button" value="Search" />
<input id="Reset1" type="reset" value="Reset" />
<input id="Submit1" type="submit" value="Add" />
</div>
}
You can use two approaches here:
handle onsubmit and fetch/save data with AJAX (you can do it even with Html.BeginForm but it's easier to go just with regular <form ...)
#using (Html.BeginForm("DoIt", "DoItAction", FormMethod.Post, new { onsubmit = "submitWithAjax(event); return false;" }))
create two separate forms with a different action/controller pair

Create a search page in ActiveAdmin

I'm using ActiveAdmin to deploy my project. And I had some problem when I was developing.
I had some database table, e.g: "Worker", "Product", "Task". I wanted to create a page to search on those table with many situation.
I created a simple page:
ActiveAdmin.register_page "my page" do
content do
panel "Search details" do
panel "By Name" do
render :partial => "form"
end
end
end
end
And this is _form.html.erb
<form action="" method="post">
<input type="text" value="" name="taskid">Task ID</input>
<input type="text" value="" name="productid">Product ID</input>
<input type="text" value="" name="workerid">Worker ID</input>
<input type="submit" value="Submit"/>
</form>
But I don't know how can I call a controller from a form? (which controller was defined)
And How can I render or show the result to the content area of "my_page" in Activeadmin from a controller?
Anyone may help me? plz!
Design the form such that it uses the existing active admin filters and displays the results accordingly. You may want to look at the HTML of your filter forms (using firebug), in order to get the required params, and the submit action. Here is the partial which I use to search my User model (constructed from user filters):
<div class="panel_contents">
<form method="get" id="q_search" class="filter_form" action="/admin/users" accept-charset="UTF-8">
<div class="filter_form_field filter_string">
<label for="q_email" class=" label">Search User Email</label><br/>
<input type="text" name="q[email_contains]" id="q_email" />
<input type="submit" value="Go" name="commit" id="q_submit" />
</div>
</form>
</div>
and displays the result in the existing format. You don't need to design new views for that.

Do MVC3 non-sequential hidden input indexes need to come first?

MVC3 non-sequential index hidden inputs for model binding..
<input type="hidden" name="Index" value="whatever" />
Does it matter if they go before, after, in the middle of the other related inputs to be posted?
Does it matter at all where they end up in the posted data?
For example, can they all be lumped together and it still works?
<input type="text" name="[A].Id" value="1" />
<input type="text" name="[B].Id" value="2" />
<input type="hidden" name="Index" value="A" />
<input type="hidden" name="Index" value="B" />
No, the order of your form fields does not matter, nore where they appear on the html page.
The most important factor for MVC3 is the name of the fields must match to the name of your controller/action parameter.
If you have two fields with the same name however, only one value will be returned into your action.
As long as the hidden fields are located inside of the form it should not matter the order in which they are placed. Please see code sample below. Notice how the hidden fields are put anywhere inside of the form.
#using (Html.BeginForm())
{
#Html.ValidationSummary(false, "Please correct the following errors")
#Html.HiddenFor(m => m.CoolStuffId)
#Html.Partial("_EditCoolStuff", Model)
<fieldset class="ui-grid-a">
<div class="ui-block-a"><a data-role="button" href="#Url.Action("ActionPlan", "Store", new { id = Model.StoreID })">Cancel</a></div>
<div class="ui-block-b"><button type="submit" data-theme="a">Submit</button></div>
</fieldset>
#Html.HiddenFor(m => m.TypeId)
}

ASP.NET MVC Create querystring from form

I am trying to create a simple search box that results in something like http://www.example.com/Search?s=searchTerm I have the routing setup so that it accepts a url like this, and does the right thing. The problem I'm having is getting the form to create the querystring. I have tried many variations of:
<% using (Html.BeginForm("Search", "Home", FormMethod.Get, new { ???? })) {%>
<input id="submitSearch" class="searchBox" type="text" runat="server"/>
<input type="submit" value="Search" /> <%} %>
I'm not sure how to setup the Html.BeginForm so it grabs the submitSearch value and passes it to /Search?s=valueHere. This seems like I am missing something easy.
You need to set name on the input box to s.
<% using (Html.BeginForm("Search", "Home", FormMethod.Get, new { })) { %>
<input id="s" name="s" class="searchBox" type="text" />
<input type="submit" value="Search" />
<% } %>
Also, note that I changed the id to s as well, since common practice is to have the same value for name and id. However, it is only the name attribute that affects the query string name in the request.
And as David noted in a comment, the runat="server" is not needed in ASP.NET MVC.

Resources