如何从视图中的文本框的值传递给控制器MVC 4? [英] How to pass a textbox value from view to a controller in MVC 4?
本文介绍了如何从视图中的文本框的值传递给控制器MVC 4?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在这里获取从数据库中的值并显示在输入字段是
<输入类型=文本ID =SS值=@ item.Quantity/>
和从数据库中获取的值 1
。然后,我改变输入字段值 2
和值传递给控制器的动作点击
<一个ID =imgUpdate的href =@ Url.Action(更新,购物,新{ID =的Request.QueryString [用户名],产品ID = item.ProductID,数量= item.Quantity,unitrate = item.Rate})>
但在控制器部分我得到了旧值
1 数量
。但我需要的更新值2
在数量
公众的ActionResult更新(字符串ID,字符串的ProductID,诠释数量,小数unitrate)
{
如果(ModelState.IsValid)
{
诠释_records = UpdatePrice(ID,产品ID,数量,unitrate);
如果(_records大于0)
{
返回RedirectToAction(索引1,购物);
}
其他
{
ModelState.AddModelError(,无法更新);
}
}
返回视图(索引1);
}
任何建议?
编辑:
@using(Html.BeginForm(更新,购物,FormMethod.Post))
{ @ Html.Hidden(ID,@的Request.QueryString [用户名]作为字符串)
@ Html.Hidden(产品ID,item.ProductID为字符串)
@ Html.TextBox(数量,item.Quantity)
@ Html.Hidden(unitrate,item.Rate) <输入类型=提交值=更新/>
}
解决方案
您可以用简单的形式:
@using(Html.BeginForm(更新,购物))
{
<输入类型=文本ID =SSNAME =数量值=@ item.Quantity/>
...
<输入类型=提交值=更新/>
}
和添加属性在这里:
[HttpPost]
公众的ActionResult更新(字符串ID,字符串的ProductID,诠释数量,小数unitrate)
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 value
1 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" />
}
解决方案
You can use simple form:
@using(Html.BeginForm("Update", "Shopping"))
{
<input type="text" id="ss" name="qty" value="@item.Quantity"/>
...
<input type="submit" value="Update" />
}
And add here attribute:
[HttpPost]
public ActionResult Update(string id, string productid, int qty, decimal unitrate)
这篇关于如何从视图中的文本框的值传递给控制器MVC 4?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文