【问题标题】:How to insert/update/Delete record from a single view in mvc如何从 mvc 中的单个视图中插入/更新/删除记录
【发布时间】:2015-08-06 23:11:41
【问题描述】:

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

我有类似的桌子

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

注意不想使用modal popup和另一个div来隐藏显示只想在表格行中插入和更新记录。

在这里,我想使用上表进行插入/更新和删除,但对它没有任何想法,并且还想在此处放置一些输入文本以管理 name and image。所以任何人都有任何信息或链接,而不是请分享。

【问题讨论】:

    标签: jquery html asp.net asp.net-mvc-5


    【解决方案1】:

    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>

    【讨论】:

    • 这是关于控制器的,但是关于索引页面我应该如何管理我的输入控件
    • 无法正常工作并给出类似The view 'add' or its master was not found or no view engine supports the searched locations. The following locations were searched:的错误
    • @user2963609 : 这个视图是强类型视图,您可以通过添加另一个普通视图来使用输入控件。
    • 我怎么能像你说的那样做。我不知道
    【解决方案2】:

    【讨论】:

    • 我访问过这个链接,但他们使用的是webgrid 是否还有其他文章
    【解决方案3】:
    猜你喜欢
    • 2013-07-04
    • 1970-01-01
    • 1970-01-01
    • 2014-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多