这里我从数据库中获取值并在输入字段中显示它
<input type="text" id="ss" value="@item.Quantity"/>
从数据库获取的值为1
。然后我将输入字段值更改为2
并在动作中将该值传递给控制器单击
<a id="imgUpdate" href="@Url.Action("Update", "Shopping", new { id = Request.QueryString["UserID"], productid = item.ProductID, qty = item.Quantity, unitrate = item.Rate })">
但是在控制器部分,我old value
获得了qty
1。但我需要updated value 2
中的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");
}
有什么建议吗?
修改
@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" />
}
答案 0 :(得分:10)
您可以使用简单的表格:
@using(Html.BeginForm("Update", "Shopping"))
{
<input type="text" id="ss" name="qty" value="@item.Quantity"/>
...
<input type="submit" value="Update" />
}
并在此添加属性:
[HttpPost]
public ActionResult Update(string id, string productid, int qty, decimal unitrate)
答案 1 :(得分:3)
如果要将新信息传递给应用程序,则需要使用POST表单。在Razor中,您可以使用以下
查看代码:
@* 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" />
}
控制人员的行动
[HttpGet]
public ActionResult Update(){
//[...] retrive your record object
return View(objRecord);
}
[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");
}else{
ModelState.AddModelError("","Can Not Update");
}
}
return View("Index1");
}
注意,或者,如果要使用@Html.TextBoxFor(model => model.Quantity)
,您可以输入名称(respectecting case)"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" />
}
发布操作
[HttpPost]
public ActionResult Update(Record rec){ //Alternatively you can also use FormCollection object as well
if(TryValidateModel(rec)){
//update code
}
return View("Index1");
}
答案 2 :(得分:2)
页面加载时会生成您的链接,因此它始终具有原始值。您需要通过javascript
设置链接您也可以将其包装在表单中,并为id
,productid
和unitrate
这是你的sample。
<强> 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>
<强> 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, " "));
}
这可以根据需要进行清理和扩展,但示例有效
答案 3 :(得分:1)
在视图中尝试以下操作以检查每个输出。当第二次调用视图时,第一个更新。我的控制器使用键ShowCreateButton并具有可选参数_createAction并带有默认值 - 您可以将其更改为您的键/参数
@Html.TextBox("_createAction", null, new { Value = (string)ViewBag.ShowCreateButton })
@Html.TextBox("_createAction", ViewBag.ShowCreateButton )
@ViewBag.ShowCreateButton
答案 4 :(得分:0)
我只是试着回答这个问题,但我的例子非常简单,因为我是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") }
只需确保文本框名称和控制器的功能输入具有相同的名称和类型(字符串)。这样,您的函数将获得文本框输入。