How to pass text value to action parameter? - asp.net-mvc

i need to pass input value to action method in controller
i have text and button , so I need to click on the button and take the value of input to parameter and re back to the same view but in post status
i tried many things but not works
i tried to create post form and the type of button is submit but does not work
this is my view code
<form method="post">
<input type="text" id="Comments" , name="ID" value="" />
<button type="submit">Go</button>
</form>
this is my controller code
public ActionResult Index()
{
Entities db = new Entities();
var Query = from acc in db.tbl_Accounts
select acc ;
var accList= Query.ToList<tbl_Accounts>();
return View(accList);
}
[HttpPost]
public ActionResult Index(int ID)
{
Entities db = new Entities();
var Query = from Acc in db.tbl_Accounts
where Acc.id== ID
select Acc ;
var accList= Query.ToList<tbl_Accounts>();
return View(accList);
}

In your view model use #Html.BeginForm redirect to action method in controller.
In View
#using (Html.BeginForm("youractionname", "yourcontrollername",FormMethod.Post, new { #class = "form-horizontal"}))
{
<input type="text" id="Comments" name="ID" />
<button type="submit">Go</button>
}
In Controller
public ActionResult Index()
{
Entities db = new Entities();
var Query = from acc in db.tbl_Accounts
select acc ;
var accList= Query.ToList<tbl_Accounts>();
return View(accList);
}
[HttpPost]
public ActionResult Index(int ID)
{
Entities db = new Entities();
var Query = from Acc in db.tbl_Accounts
where Acc.id== ID
select Acc ;
var accList= Query.ToList<tbl_Accounts>();
return View(accList);
}
You have taken an integer datatype in the action method so that you enter the digit value in the textbox
Note: Please verify your action and controller name In view Model

There is a few typo mistakes in your code, if you correct it as following then it will work as expected:
<form method="post">
<input type="text" id="Comments" name="ID" />
<button type="submit">Go</button>
</form>
So now check only the parameter value, if it is there:
[HttpPost]
public ActionResult Index(int ID)
{//Put a breakpoint here and check if ID has value
return View();
}
If the value exists there, than assume that it is okay.
Here is a screenshot that I have tested this answer.

Related

How to sum 2 input text numbers using controller in MVC

i have 3 input text and one button in my view like this
#using (Html.BeginForm()) {
<input id="Text1" name="txtNum1" type="text" placeholder="num 1" onkeypress='return event.charCode >= 48 && event.charCode <= 57'/>
<input id="Text2" name="txtNum2" type="text" placeholder="num 2" onkeypress='return event.charCode >= 48 && event.charCode <= 57'/>
<input id="Text3" name="txtNum3" type="text" readonly="readonly" />
<input id="Submit1" type="submit" value="Sum" />
}
in my controller ,when i click submit button ,i want to sum the 2 input text and put result in the third text , this is my controller
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(int txtNum1=0,int txtNum2=0, int txtNum3=0)
{
txtNum3= txtNum1+ txtNum2;
return View(txtNum3);
}
when i put numbers in the 1st and 2nd text then click submit it post back but i don't show any result in the 3rd text .
so how can i fix this code?
You need to create a Model class first of all, in your case it will contains three properties as you have three textboxes and the input will be numbers, so use int.
Your model class will be :
public class SumModel
{
public int Number1 { get;set;}
public int Number1 {get;set;}
pubilc int Result {get { return Number1 + Number2;}}
}
then in your view you will need to specify which Model it is fimiliar with of, which means which model this view is binded with :
#model YourNameSpace.Models.SumModel
#using (Html.BeginForm())
{
#Html.TextBoxFor(x=>x.Number1)
#Html.TextBoxFor(x=>x.Number2)
#Html.TextBoxFor(x=>x.Result, new {#readonly = "readonly"})
<input id="Submit1" type="submit" value="Sum" />
}
and in controller action add the parameter of type SumModel:
[HttpPost]
public ActionResult Index(SumModel model)
{
return View(model);
}
after the model posted the View will be re-rendered with the third textbox with the sum value of two textboxes.

How to check each form controller's type posted to action method?

View:
#using (Html.BeginForm())
{
#Html.Label("WebLinkLabel")
#Html.Editor("WebLinkTextbox")
#Html.Label("WebEnabledLabel")
#Html.CheckBox("WebEnabledCheckbox")
<input name="Save" type="submit" value="Save" />
}
Action Method:
[HttpPost]
public ActionResult Index(FormCollection collection)
{
foreach (var key in collection.AllKeys)
{
var typee = collection.GetType();
}
return View();
}
GetType() gives me that:
collection.AllKeys.GetType()
{Name = "String[]" FullName = "System.String[]"} System.Type {System.RuntimeType}
I would like to know like the first controller is of type Textbox, another one is Checkbox etc..

how to get the parameters of a request from a view in controller

I am new to mvc and I want to know how to get the parameters of a request from a view.
This is my code for view:
#using (Ajax.BeginForm("LoadPartialView", new AjaxOptions())) {
<input type="submit" value="Submit" id="submit1"/> }
I want to get the value Submit or id submit1 in action method LoadPartialView of the controller. How can I achieve this?
Without knowing anything about your view model or controller methods, you can get the value by doing the following:
View:
#using (Ajax.BeginForm("MyAction", new AjaxOptions())) {
<input type="submit" name="submit" value="Submit" id="submit1"/>
}
Controller:
[HttpPost]
public ActionResult MyAction(FormCollection f)
{
var submitValue = f["submit"];
}
Alternatively, type your view to a model with a submit property and pass this to your controller method, doing something like the following:
Model:
public class MyModel
{
public String Submit { get; set; }
}
View:
#model MyModel
#using (Ajax.BeginForm("MyAction", new AjaxOptions())) {
<input type="submit" name="Submit" value="Submit" id="submit1"/>
}
Controller:
[HttpPost]
public ActionResult MyAction(MyModel model)
{
var submitValue = model.Submit;
}
The latter model is obviously more extensible as you can then group this value with other form values.
You need name attribute for any input field to be serialized by default to the FormcCollection, for example:
<input type="text" value="yourvalue" name="yourname" id="yourid"/>
Then you can retrieve such value in following way:
public ActionResult YourAction(FormCollection formCollection)
{
var val = formsCollection["yourname"];
//...
}

MVC - two text box with the same parameter

#using (Html.BeginForm("Orders1", "Track", FormMethod.Post))
{
#Html.TextBox("order")<br />
#Html.TextBox("order")
<input type="submit" value="Submit" />
}
Hello how can i use two text box and pass value to the same parameter? The first textbox work but the other textbox doesn't.
public ActionResult Orders1(IEnumerable<int> order)
{
var query = from a in context.CM_Checkout_Details
where a.CheckoutDetails_ID == order // my error
select a;
return View(query);
}
}
simple solution would be to change your parameter from int to IEnumerable<int>
public ActionResult Orders1(IEnumerable<int> orders)
{
}

MVC ERROR Illegal characters in path.

can someone tell me if im on the right track? Im trying to display my query but i get an error. I have two textbox with the same parameter and that parameter is declared as an IEnumerable.
[HttpPost]
public ActionResult Orders1(IEnumerable<int> order)
{
using (CostcoEntities1 context = new CostcoEntities1())
{
var query = string.Empty;
foreach (var orderID in order)
{
query = (from a in context.CM_Checkout_Details
where a.CheckoutDetails_ID == orderID
select a).ToString();
}
return View(query);
}
}
this is what my controller looks like..
I am trying to read the two numbers(Id) in the text box and diplay data based on those id.
#using (Html.BeginForm("Orders1", "Track", FormMethod.Post))
{
#Html.TextBox("order")<br />
#Html.TextBox("order")
<input type="submit" value="Submit" />
}
First thing, change the names of the textboxes so that they are not the same:
#using (Html.BeginForm("Orders1", "Track", FormMethod.Post))
{
#Html.TextBox("order1")<br />
#Html.TextBox("order2")
<input type="submit" value="Submit" />
}
Next, change the signature of your action method:
[HttpPost]
public ActionResult Orders1(string order1, string order2)
MVC's model binding will try to match order1 and order2 to stuff in Request.Form, for example, which should pick up the textbox values.

Resources