Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a textbox value from view to a controller in MVC 4?

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 value1 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" />
     }
like image 774
bala3569 Avatar asked Oct 18 '12 12:10

bala3569


People also ask

What are ways to pass two TextBox values from view to controller?

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.

How can we transfer data from view to controller?

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.

How pass data from view to model in MVC?

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.


1 Answers

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.

like image 115
sssD Avatar answered Oct 25 '22 08:10

sssD