gpt4 book ai didi

jquery - 如何从 mvc 中的单个 View 插入/更新/删除记录

转载 作者:行者123 更新时间:2023-11-28 01:30:30 25 4
gpt4 key购买 nike

我尝试使用单个 View 页面插入/更新/删除记录。但到目前为止无法找到任何解决方案。如果有人有任何例子或有关它的任何信息,请帮助我。

我有这样的 table

<table id="responsive-table">
<thead>
<tr>
<th>
Name
</th>
<th>
Age
</th>
<th>Actions</th>
</tr>
</thead>

<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.name)
</td>
<td>
@Html.DisplayFor(modelItem => item.age)
</td>
<td>
<a id="btnEdit" data-id="@item.TimeSheetID " class="action-icon clsEdit"><i class="pms-edit"></i></a>
<a id="btnDelete" data-id="@item.TimeSheetID" class="action-icon clsDelete"><i class="pms-delete"></i></a>
</td>
</tr>
}
</tbody>
</table>

Note do not want to use modal popup and another div for hide show just want to insert and update record in a table row.

在这里,我想使用上面的表格进行插入/更新和删除,但对此一无所知,还想在此处放置一些输入文本以管理 name 和 image。所以任何人都有任何信息或链接,请分享。

最佳答案

<强>1。 HomeController.cs

public class HomeController : Controller
{
MVCDbContext _db = new MVCDbContext();
public ActionResult Index()
{
return View(_db.Model1.ToList());
}

[HttpGet]
public ActionResult add(Model1 objmo,int?id)
{
if (id == null)
{
return View();
}
else
{
Model1 objdata = _db.Model1.Find(id);
objmo.fname = objdata.fname;
objmo.lname = objdata.lname;
return View(objdata);
}
}
[HttpPost]
public ActionResult add(Model1 obj)
{
if (obj.Aref == 0)
{
_db.Model1.Add(obj);
}
else
{
Model1 objdata = _db.Model1.Where(x => x.Aref == obj.Aref).FirstOrDefault();
objdata.fname = obj.fname;
objdata.lname = obj.lname;
}
_db.SaveChanges();
return RedirectToAction("Index");
}
public ActionResult delete(int? id)
{
_db.Model1.Remove(_db.Model1.Find(id));
_db.SaveChanges();
return RedirectToAction("Index");
}
}

2. Index.cshtml

@model IEnumerable<mvc_crud_dropdown.Models.Model1>

@{
ViewBag.Title = "Index";
}


<p>
@Html.ActionLink("Create New", "add")
</p>
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.Aref)
</th>
<th>
@Html.DisplayNameFor(model => model.fname)
</th>
<th>
@Html.DisplayNameFor(model => model.lname)
</th>
<th></th>
</tr>

@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Aref)
</td>
<td>
@Html.DisplayFor(modelItem => item.fname)
</td>
<td>
@Html.DisplayFor(modelItem => item.lname)
</td>
<td>
@Html.ActionLink("Edit", "add", new { id=item.Aref }) |
@Html.ActionLink("Delete", "Delete", new { id = item.Aref })
</td>
</tr>
}

</table>

关于jquery - 如何从 mvc 中的单个 View 插入/更新/删除记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30438881/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com