Hi,Gloops
For it I have to remember how to edit data from a sub-page, and that for an element in a list.
You can finish this by using bootstrap modal, click edit button to call the modal out, and get the data to the modal, edit in it, then submit to your controller
and save to the database, the following is a demo:
Controller:
public ActionResult Index()
{
var model = _db.MyModels.ToList();
return View(model);
}
public ActionResult Edit(MyModel myModel)
{
var update = _db.MyModels.Find(myModel.Id);
TryUpdateModel(update, "", new string[] {"opinions" });
_db.SaveChanges();
return RedirectToAction("Index");
}
View:
@默 IEnumerable<Gloops1131.Models.MyModel>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Id)
</th>
<th>
@Html.DisplayNameFor(model => model.opinions)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Id)
</td>
<td>
@Html.DisplayFor(modelItem => item.opinions)
</td>
<td>
<button type="button"
class="btn btn-primary"
data-toggle="modal"
data-target="#exampleModal"
data-id="@item.Id"
data-opinion="@item.opinions">Edit</Button>
</td>
</tr>
}
</table>
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Edit opinions</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<form method="post" action="/Home/Edit">
<div class="modal-body">
<input type="hidden" id="id" name="id" />
<div class="form-group">
<label>Opinion:</label>
<input type="text" class="form-control" id="opinions" name="opinions" />
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<input type="submit" class="btn btn-primary" value="Save" />
</div>
</form>
</div>
</div>
</div>
The Javascript in the above view:
$('#exampleModal').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget);
var id = button.data('id');
var opinion = button.data('opinion');
var modal = $(this);
modal.find('.modal-body input[name="id"]').val(id);
modal.find('.modal-body input[name="opinions"]').val(opinion);
});
Result:
Best Regards,
Jerry Cai
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.