c# - FormMethod.Post 在 MVC4 中返回查询字符串

标签 c# asp.net asp.net-mvc

我在 _Layout.cshtml 页面中传递了我的查询字符串

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


我有一个更新 View 来更新文本框中的值

 @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" />
         }

在第一次发帖时,我得到这样的网址

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

但是第二次,在发布后我得到了这样的网址

http://localhost:61110/Shopping/Index1

如何在发布表单时始终获取查询字符串值。
我也尝试过这个mvc3: html.beginform search returning empty querystring

但在第二次发布时无法获取查询字符串
任何建议。

编辑:更新操作代码

[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();
    }

最佳答案

您还需要将 new { id = Request.QueryString["UserID"] } 添加到 Update 中的 RedirectToAction 中。

关于c# - FormMethod.Post 在 MVC4 中返回查询字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13005229/

相关文章:

asp.net - Microsoft Jet 数据库引擎找不到对象 'Sheet1$'

c# - Razor View 中不包含 'AsEnumerable' 和 'cannot convert from methodgroup' 的定义

css - 当我将源代码添加到 ASP.NET MVC 元素时,Bootstrap 容器未以实际大小显示

c# - 控制第三方应用并向其子窗口发送消息

c# - 创建可扩展属性类 (OOP)

c# - 如何使用 C# 中的 Keys 枚举检测小写键?

c# - 从函数返回 2 个项目

Java MD5 编码器与 C# MD5CryptoServiceProvider 不匹配

asp.net - ASP.NET 服务器错误页面上的版本信息

c# - KeyVaultClient 在调试时随机抛出 FatalExecutionEngineError