kk1076
kk1076

Reputation: 1748

FormMethod.Post to return querystring in MVC4

I passed my querystring in _Layout.cshtml page

  <li>@Html.ActionLink("Shopping", "Index1", new { controller = "Shopping", UserID = "Admin1" })</li>  


I have an update view to update the values from textbox

 @using (Html.BeginForm("Update", "Shopping", new { id = Request.QueryString["UserID"] }, FormMethod.Post, new { id = "myForm" }))
         {
               @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" />
         }

In the first time post , i get the url like this

http://localhost:61110/Shopping/Index1?UserID=Admin1

But for the second time, after post i get the url like this

http://localhost:61110/Shopping/Index1

How to get the querystring value at all times when it the form is posted.
I also tried this mvc3: html.beginform search returning empty querystring

But couldnt get the querystring on second time post
Any suggestions.

EDIT : Update Action Code

[HttpPost]
        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",new { id = Request.QueryString["UserID"] });
                }
                else
                {
                    ModelState.AddModelError("","Can Not Update");
                }
            }
            return View("Index1");
        }

public int UpdatePrice(string id,string productid,int qty,decimal unitrate)
    {
        con.Open(); 
        var total = qty * unitrate;
        SqlCommand cmd = new SqlCommand("Update [Table_name] Set Quantity='" + qty + "',Price='" + total + "' where [User ID]='" + id + "' and [Product ID]='" + productid + "'", con);
        cmd.Parameters.AddWithValue("@total", total);
        return cmd.ExecuteNonQuery();
    }

Upvotes: 2

Views: 4086

Answers (1)

Erik Funkenbusch
Erik Funkenbusch

Reputation: 93434

You need to add the new { id = Request.QueryString["UserID"] } to the RedirectToAction in Update as well.

Upvotes: 2

Related Questions