简体   繁体   中英

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

Use

<td>@Html.ActionLink("Edit", "Edit", new { id = issueditem.id})</td>

I think you can use Ajax for this task.

    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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM