在ASP.NET MVC中实现类似于ListView的功能

30 浏览
0 Comments

在ASP.NET MVC中实现类似于ListView的功能

我有一个使用ASP.NET WebForms构建的现有Web应用程序,并将其转换为MVC 4.0。我有一个管理页面,允许用户在同一页上执行对员工的CRUD操作,例如编辑地址或通过动态添加用户控件同时添加多个地址。我已经使用ListView实现了在同一页上显示、编辑和保存。现在,如果我想在MVC中实现相同的功能,即在同一个视图上显示/编辑/保存,有人可以为这种情况提供一些建议吗?(附注:我已经准备好用于此类CRUD操作的部分视图)任何帮助将不胜感激。

0
0 Comments

问题的原因是希望在ASP.NET MVC中实现类似于ListView的功能,即在同一个视图中展示、编辑和保存数据。为了实现这个功能,作者尝试了两种方法:使用.BeginForm和jquery.unobtrusive-ajax.min来获取部分视图,以及使用jQuery AJAX来实现。作者在Stackoverflow上找到了一个对解决问题非常有帮助的链接,链接地址为:https://stackoverflow.com/questions/5410055。感谢所有人的努力。

解决方法:

1. 使用.BeginForm和jquery.unobtrusive-ajax.min来获取部分视图。

2. 使用jQuery AJAX来实现。

代码示例:

@using (Ajax.BeginForm("Action", "Controller", new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "targetDiv", InsertionMode = InsertionMode.Replace }))
{
    // form fields and submit button
}
// partial view content will be displayed here

$.ajax({
    url: "/Controller/Action",
    type: "POST",
    data: formData,
    success: function (response) {
        $("#targetDiv").html(response);
    }
});

以上就是在ASP.NET MVC中实现类似于ListView的问题的原因和解决方法。希望对大家有所帮助。

0
0 Comments

在ASP.NET MVC中,实现类似ListView的功能时,可能会遇到以下问题:

问题原因:

1. 在视图中,使用了HTML表格来展示模型中的数据。

2. 在控制器中,定义了删除和编辑的操作方法。

解决方法:

1. 在视图中,使用HTML辅助方法来创建表格。

2. 在控制器中,根据需要实现删除和编辑的逻辑。

下面是具体的解决方法:

视图:

@model YourNamespace.YourViewModel

    @foreach (var item in Model.lstEmployees)
    {
        
    }
@Html.DisplayNameFor(model => model.name) @Html.DisplayNameFor(model => model.id) @Html.DisplayNameFor(model => model.designation)
@Html.DisplayFor(modelItem => item.name) @Html.DisplayFor(modelItem => item.id) @Html.DisplayFor(modelItem => item.designation) @Html.ActionLink("Edit", "Edit", new { id = item.id }) @Html.ActionLink("Delete", "Delete", new { id = item.id })

控制器:

public class HomeController : Controller
{
    // ...
    public ActionResult Delete(int id)
    {
        // 执行删除的代码
        return View("Index", data);
    }
    [HttpGet]
    public ActionResult Edit(int id)
    {
        // 绑定现有记录的代码
        return View(_data);
    }
    [HttpPost]
    public ActionResult Edit(string sampleDropdown, Data model)
    {
        // 保存更新数据的代码
        return RedirectToAction("Index", "Home");
    }
}

通过以上方法,可以在ASP.NET MVC中实现类似ListView的功能。

0
0 Comments

问题的原因是:ASP.NET MVC中没有内置的ListView控件,需要使用第三方控件或者手动实现。

解决方法是:如果不想使用第三方控件,可以使用Table、Tr和Td来模拟ListView的效果。通过获取数据列表,使用foreach循环遍历数据,并使用Table、TH、TR和TD来展示数据。同时,可以使用.ActionLink来实现编辑、删除等操作。

以下是示例代码:

@model List

        @foreach (var item in Model)
        {
            
        }
    
Column 1 Column 2 Column 3 Actions
@item.Property1 @item.Property2 @item.Property3 @Html.ActionLink("Edit", "Edit", new { id = item.Id }) @Html.ActionLink("Delete", "Delete", new { id = item.Id }, new { onclick = "return confirm('Are you sure you want to delete this item?');" })

希望这对您有所帮助。

0