How I can pass data in view using Post method and ViewBag? - asp.net-mvc

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.

Related

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

i want to get the form data form one gsp and want to access that data in another gsp and want to insert that data in table, how can i do this?

This is one gsp where i created one form
<div class="form-group">
<label for="msg_to">To</label>
<input type="email" id="msg_to" name="msg_to" class="form-control" style="width:500px;" required="">
</div>
<div class="form-group">
<label for="msg_subject">Subject</label>
<input type="text" id="msg_subject" class="form-control" style="width:500px;"name="msg_subject" required="" maxlength="50">
</div>
<div class="form-group">
<label for="msg_body">Body</label>
<textarea class="form-control" id="msg_body" style="width: 500px; height: 250px" name="msg_body" required="" ></textarea>
</div>
%{--<button type="submit" class="btn btn-default">Send</button>--}%
<g:actionSubmit value="Send" action="g_Inbox" style="color: #000000"></g:actionSubmit>
</form>
The data of above form must be access to another controller after submit and data should be display in the table form on that another view.
I fetch above data in following action:
def g_Inbox(){
def msg_to = params.msg_to;
def msg_subject = params.msg_subject;
def msg_body = params.msg_body;
println msg_to
println msg_subject
println msg_body
render(view: 'g_Inbox', model: [m_to : msg_to , m_sub : msg_subject , m_body: msg_body] )
}
from here i want to send it to view and want to add in new row of table..
You're in luck. What you want to do is very easy with Grails. But... you have to do your homework.
Things to read and digest
(best served with pizza and iced tea)
Groovy Server Pages -
https://grails.github.io/grails-doc/latest/guide/theWebLayer.html#gsp
Controllers - https://grails.github.io/grails-doc/latest/guide/theWebLayer.html#controllers
Once you understand GSP and controllers, you'll know exactly what to do.
Simple if You know how it works
render(view: 'g_Inbox', model:[params:params])
Access the params in gsp by
${params.msg_to}
and for Another Controller
redirect controller:'Controller' action: 'create',params:params

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)
}

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

Resources