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.
Related
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.
i am written the code for CREATE Operation in view which will insert multiple of rows in database using form-tag with for-loop. when click on Create button it will save only one row so i want that when i click on button no.of row should be created.
here code for View..
#model CarParking.Models.Area
#for (int i = 1; i <= Model.NoOfParkingPlaces; i++)
{
using (Html.BeginForm("Create", "ParkingPlace", FormMethod.Post))
{
<label>AreaID:</label>
<input id="AreaID" name="AreaID" type="text" value="#Model.AreaID" />
<label for="Parking_IDs">Parking IDs</label>
<input type="text" value="P.Id: #i" name="PlaceId" />
<input type="submit" value="Create" />
}
}
here code for Controller...
public ActionResult Create(string areaName)
{
Area area = db.Areas.SingleOrDefault(x => x.ParkingPlaceName == areaName);
return View(area);
}
[HttpPost]
public ActionResult Create([Bind(Include = "ParkID,PlaceId,CarID,AreaID")] ParkingPlace parkingPlace)
{
if (ModelState.IsValid)
{
db.ParkingPlaces.Add(parkingPlace);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(parkingPlace);
}
Here some images how my View looksenter image description here
Are you saying you want to post ALL the forms on the page at once?
If so - it doesn't work like that... One option would be to use javascript to AJAX post them all, but the correct way to do it is to use one form, with each of the ParkingPlaces in it.
This article goes through it in detail: http://www.hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysListsCollectionsDictionaries.aspx
#for( int i = 0; i < Model.NoOfParkingPlaces; ++i)
{
#Html.EditorFor(m => m.ParkingPlaces[i].AreaId)
#Html.EditorFor(m => m.ParkingPlaces[i].Id)
... etc
}
and your action parameters become;
public ActionResult Create(IEnumerable<ParkingPlace> ParkingPlaces)
{
I need to do a calculator in ASP.NET MVC.
For the beginning I want to receive the value from the input field in controller and prefix it with the string "123". At the end I will process the expresion received and return the result.
I have the following model:
namespace CalculatorCloud.Models {
public class Calculator
{
public string nr { get; set; }
} }
In the view I am using the model:
#model CalculatorCloud.Models.Calculator
#{
ViewBag.Title = "Calculator";
}
#using (Html.BeginForm("Index", "Home"))
{
<div>
<div class="header">
#Html.TextBoxFor(m => m.nr, new { #id = "nr"})
<input type="button" id="C" name="C" value="C" />
<input type="button" id="back" name="back" value="<-" />
[...]
<div class="sum">
<input type="submit" value="=" />
</div>
</div>
}
The controller is like this:
namespace CalculatorCloud.Controllers
{
public class HomeController : Controller
{
Calculator model = new Calculator();
public ActionResult Index(string nr)
{
model.nr = "123" + nr;
return View(model);
}
}
}
I have the following problem: when pressing on submit button I am expecting to be displayed on the textbox the value from that was previously in the textbox, prefixed with the string "123".
But now it is kept the value from the textbox without the string "123".
Can someone help me with this?
Thank you! :)
If you want to modify the value of a model property in a postback action you will need to remove it from the ModelState:
public ActionResult Index(string nr)
{
ModelState.Remove("nr");
model.nr = "123" + nr;
return View(model);
}
The reason for this is that Html helpers such as TextBoxFor will first look at the value present in the ModelState and then in your view model property when rendering the value. This is by design.
#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)
{
}
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.