[英]How to do edit correctly in Asp.net MVC
I'm using a variable called linenumber to get number of lines in relation to the number of items. 我正在使用一个名为linenumber的变量来获取与项目数相关的行数。 However line number keeps returning 0. I'm pulling the list of items from a database so I'm not if that is the reason why.
但是行号一直返回0。我正在从数据库中提取项目列表,所以如果那是原因,我就不会这样做。 My question is there another way to get the edit done.
我的问题是还有另一种方法可以完成编辑。
View 视图
@Html.HiddenFor(model => model.item.lineNum)
</div>
<br /><br />
<h4>Issued Items</h4>
<hr />
<div class="form-group">
<table>
<tr>
<th class="col-md-4">Item Number</th>
<th class="col-md-4">Item Description</th>
<th class="col-md-4">Expense Account</th>
<th class="col-md-2">Quantity Requested</th>
<th class="col-md-2">Quantity Issued</th>
<th class="col-md-1">UOM</th>
<th class="col-md-1">Item Price</th>
</tr>
@{
foreach (var issueditem in ViewBag.IssuedItems)
{
<tr>
<td class="col-md-4">@issueditem.itemNumber</td>
<td class="col-md-4">@issueditem.description</td>
<td class="col-md-4">@issueditem.expense_account.getDescription</td>
<td class="col-md-2">@issueditem.quantity.ToString()</td>
<td class="col-md-2">@issueditem.quantityI.ToString()</td>
<td class="col-md-1">@issueditem.selecteduomtext </td>
<td class="col-md-1">@issueditem.price.ToString()</td>
<td>@Html.ActionLink("Edit", "Edit", new { id = issueditem.lineNum })</td>
</tr>
}
}
<tr></tr>
</table>
Controller 控制者
public ActionResult Edit(int id)
{
getIssue.item = getIssue.items[id - 1];//Returns the requested item for editing
return View(getIssue);
}
/// <summary>
/// Gets the changes submitted from the user and updates the Item in the List
[HttpPost]
public ActionResult Edit(Issue issue)
{
int indx = issue.item.lineNum - 1;
getIssue.items[indx] = issue.item;
//return View(getIssue);
return RedirectToAction("IssueItem", "Issue", new { id = indx });
}
simply use issuedItem.id this will give you the id for every record 只需使用issueItem.id,这将为您提供每条记录的ID
Use 采用
<td>@Html.ActionLink("Edit", "Edit", new { id = issueditem.id})</td>
I think you can use Ajax for this task. 我认为您可以将Ajax用于此任务。
function edit(id) {
$.ajax({
url: "Controller/Method",
type: "POST",
data: {"id": your id},
success: function(data) {
//redirect anywhere you want if success
}, error: function (data) {
//do what you want if error
}
});
}
<td><input type="button" onclick="edit(this)" /></td>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.