C# 在MVC4中,如何将文本框值从视图传递到控制器?

C# 在MVC4中,如何将文本框值从视图传递到控制器?,c#,asp.net-mvc,asp.net-mvc-4,C#,Asp.net Mvc,Asp.net Mvc 4,这里,我从数据库中获取值,并将其显示在输入字段中 <input type="text" id="ss" value="@item.Quantity"/> 但是在控制器部分,我得到了数量的旧值1。但是我需要数量中的更新值2 public ActionResult Update(string id, string productid, int qty, decimal unitrate) { if (ModelState.IsValid)

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

<input type="text" id="ss" value="@item.Quantity"/>
但是在控制器部分,我得到了
数量
旧值
1。但是我需要
数量
中的
更新值2

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");
        }
有什么建议吗

编辑:

     @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" />
     }
@* By default BeginForm use FormMethod.Post *@
@using(Html.BeginForm("Update")){
     @Html.Hidden("id", Model.Id)
     @Html.Hidden("productid", Model.ProductId)
     @Html.TextBox("qty", Model.Quantity)
     @Html.TextBox("unitrate", Model.UnitRate)
     <input type="submit" value="Update" />
}
@使用(Html.BeginForm(“更新”,“购物”,FormMethod.Post))
{
@Html.Hidden(“id”,@Request.QueryString[“UserID”]作为字符串)
@Html.Hidden(“productid”,item.productid为字符串)
@Html.TextBox(“数量”,item.Quantity)
@Html.Hidden(“单位费率”,item.Rate)
}

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

您也可以将其包装成一个表单,并为
id
productid
unitrate
设置隐藏字段

这是给你的礼物

HTML

<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>

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

您可以使用简单的表单:

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

当您想向应用程序传递新信息时,您需要使用POST表单。在Razor中,您可以使用以下命令

查看代码:

     @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" />
     }
@* By default BeginForm use FormMethod.Post *@
@using(Html.BeginForm("Update")){
     @Html.Hidden("id", Model.Id)
     @Html.Hidden("productid", Model.ProductId)
     @Html.TextBox("qty", Model.Quantity)
     @Html.TextBox("unitrate", Model.UnitRate)
     <input type="submit" value="Update" />
}
请注意,或者,如果要使用
@Html.TextBoxFor(model=>model.Quantity)
,则可以使用名为(区分大小写)
“Quantity”
的输入,或者可以更改POST Update()以接收对象参数,该参数的类型与严格类型化的视图相同。下面是一个例子:

型号

public class Record {
    public string Id { get; set; }
    public string ProductId { get; set; }
    public string Quantity { get; set; }
    public decimal UnitRate { get; set; }
}
查看

@using(Html.BeginForm("Update")){
     @Html.HiddenFor(model => model.Id)
     @Html.HiddenFor(model => model.ProductId)
     @Html.TextBoxFor(model=> model.Quantity)
     @Html.TextBoxFor(model => model.UnitRate)
     <input type="submit" value="Update" />
}

我将尝试回答这个问题,但我的示例非常简单,因为我是mvc的新手。希望这能帮助别人

    [HttpPost]  ///This function is in my controller class
    public ActionResult Delete(string txtDelete)
    {
        int _id = Convert.ToInt32(txtDelete); // put your code           
    }
此代码位于控制器的cshtml中

  >   @using (Html.BeginForm("Delete", "LibraryManagement"))
 {
<button>Delete</button>
@Html.Label("Enter an ID number");
@Html.TextBox("txtDelete")  }  
@使用(Html.BeginForm(“删除”、“图书馆管理”))
{
删除
@Html.Label(“输入ID号”);
@Html.TextBox(“txtDelete”)}

只需确保文本框名称和控制器的函数输入是相同的名称和类型(字符串)。这样,您的函数就可以获得文本框输入

在您的视图中尝试以下操作,以检查每个视图的输出。第一个在第二次调用视图时更新。我的控制器使用键ShowCreateButton,并具有可选参数_createAction和默认值-您可以将其更改为您的键/参数

@Html.TextBox("_createAction", null, new { Value = (string)ViewBag.ShowCreateButton })
@Html.TextBox("_createAction", ViewBag.ShowCreateButton )
@ViewBag.ShowCreateButton

我没有使用jquery..没有jquery还有其他方法吗?像asp.net WebForms这样..单击“输入类型更新”时,它不会进入更新操作..保留一个断点并检查它..这里有什么问题???@bala3569在浏览器中检查表单操作(页面源代码),然后检查您的输入(绑定按输入名称(如果存在)工作,然后按id工作)。您的页面必须在
Update
click之后重新加载。您的页面会重新加载,但值保持不变,并且不会调用更新操作方法。尝试如下。单击“输入类型更新”时,它不会移动到更新操作中。保留一个断点并进行检查。这里有什么问题???@bala3569通常您有两个a一个帖子和一个get,get将返回(get)查看和发布将保存您的数据。我将更新我的答案A它工作正常..但问题是我无法在更新发布方法中传递查询字符串在第一个加载页url是这样的,但在更新后单击此处传递查询字符串…查看我的edit@bala3569确保表单字段与操作符号匹配图雷
    [HttpPost]  ///This function is in my controller class
    public ActionResult Delete(string txtDelete)
    {
        int _id = Convert.ToInt32(txtDelete); // put your code           
    }
  >   @using (Html.BeginForm("Delete", "LibraryManagement"))
 {
<button>Delete</button>
@Html.Label("Enter an ID number");
@Html.TextBox("txtDelete")  }  
@Html.TextBox("_createAction", null, new { Value = (string)ViewBag.ShowCreateButton })
@Html.TextBox("_createAction", ViewBag.ShowCreateButton )
@ViewBag.ShowCreateButton