如何将文本框值从视图传递到MVC 4中的控制器?

bal*_*569 12 c# asp.net-mvc asp.net-mvc-4

这里我从数据库中获取值并在输入字段中显示它

<input type="text" id="ss" value="@item.Quantity"/>
Run Code Online (Sandbox Code Playgroud)

并且从数据库中获取的值是1.然后我将输入字段值更改为2 并在操作单击中将该值传递给控制器

 <a id="imgUpdate"  href="@Url.Action("Update", "Shopping", new { id = Request.QueryString["UserID"], productid = item.ProductID, qty = item.Quantity, unitrate = item.Rate })"> 
Run Code Online (Sandbox Code Playgroud)

但在控制器部分我得到的是old value1对qty.但我需要updated value 2qty

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");
        }
Run Code Online (Sandbox Code Playgroud)

有什么建议吗?

编辑:

     @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" />
     }
Run Code Online (Sandbox Code Playgroud)

web*_*per 11

你可以使用简单的形式:

@using(Html.BeginForm("Update", "Shopping"))
{
    <input type="text" id="ss" name="qty" value="@item.Quantity"/>
    ...
    <input type="submit" value="Update" />
}
Run Code Online (Sandbox Code Playgroud)

并添加这里属性:

[HttpPost]
public ActionResult Update(string id, string productid, int qty, decimal unitrate)
Run Code Online (Sandbox Code Playgroud)


Eon*_*dan 3

您的链接是在页面加载时生成的时生成的,因此它将始终具有原始值。您需要通过 javascript 设置链接

您也可以将其包装在表单中并具有隐藏字段idproductid和 隐藏字段unitrate

这是一个示例

超文本标记语言

<input type="text" id="ss" value="1"/>
<br/>
<input type="submit" id="go" onClick="changeUrl()"/>
<br/>
<a id="imgUpdate"  href="/someurl?quantity=1">click me</a>
Run Code Online (Sandbox Code Playgroud)

JS

function changeUrl(){
   var url = document.getElementById("imgUpdate").getAttribute('href');
   var inputValue = document.getElementById('ss').value;
   var currentQ = GiveMeTheQueryStringParameterValue("quantity",url);
    url = url.replace("quantity=" + currentQ, "quantity=" + inputValue);
document.getElementById("imgUpdate").setAttribute('href',url)
}

    function GiveMeTheQueryStringParameterValue(parameterName, input) {
    parameterName = parameterName.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + parameterName + "=([^&#]*)");
    var results = regex.exec(input);
    if (results == null)
        return "";
    else
        return decodeURIComponent(results[1].replace(/\+/g, " "));
}
Run Code Online (Sandbox Code Playgroud)

这可以根据需要进行清理和扩展,但该示例有效