Here i am fetching the value from database and showing it in a input field
<input type="text" id="ss" value="@item.Quantity"/>
and the value fetching from database is 1
.Then i am changing the input field value to 2
and passing that value to the controller in a action click
<a id="imgUpdate" href="@Url.Action("Update", "Shopping", new { id = Request.QueryString["UserID"], productid = item.ProductID, qty = item.Quantity, unitrate = item.Rate })">
But in the controller part i am getting that old value
1 for qty
.But i need that updated value 2
in qty
public ActionResult Update(string id, string productid, int qty, decimal unitrate)
{
if (ModelState.IsValid)
{
int _records = UpdatePrice(id,productid,qty,unitrate);
if (_records > 0)
{
return RedirectToAction("Index1", "Shopping");
}
else
{
ModelState.AddModelError("","Can Not Update");
}
}
return View("Index1");
}
Any suggestion?
EDIT:
@using (Html.BeginForm("Update", "Shopping", FormMethod.Post))
{
@Html.Hidden("id", @Request.QueryString["UserID"] as string)
@Html.Hidden("productid", item.ProductID as string)
@Html.TextBox("qty", item.Quantity)
@Html.Hidden("unitrate", item.Rate)
<input type="submit" value="Update" />
}
The values of TextBox will be passed (send) from View to Controller using Model class and will be later inserted in SQL Server database using ADO.Net in ASP.Net MVC Razor. Note: For beginners in ASP.Net MVC, please refer my article ASP.Net MVC Hello World Tutorial with Sample Program example.
You can do it with ViewModels like how you passed data from your controller to view. and in your HttpPost action, use a parameter with same name as the textbox name. If you want to post to another controller, you may use this overload of the BeginForm method.
The other way of passing the data from Controller to View can be by passing an object of the model class to the View. Erase the code of ViewData and pass the object of model class in return view. Import the binding object of model class at the top of Index View and access the properties by @Model.
I'll just try to answer the question but my examples very simple because I'm new at mvc. Hope this help somebody.
[HttpPost] ///This function is in my controller class
public ActionResult Delete(string txtDelete)
{
int _id = Convert.ToInt32(txtDelete); // put your code
}
This code is in my controller's cshtml
> @using (Html.BeginForm("Delete", "LibraryManagement"))
{
<button>Delete</button>
@Html.Label("Enter an ID number");
@Html.TextBox("txtDelete") }
Just make sure the textbox name and your controller's function input are the same name and type(string).This way, your function get the textbox input.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With