简体   繁体   English

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

[英]ListView like implementation in ASP.NET MVC

I have an existing web application built using ASP.NET WebForms and I am converting it to MVC 4.0. 我有一个使用ASP.NET WebForms构建的现有Web应用程序,并将其转换为MVC 4.0。 I have an administrative page that allows user to do CRUD operations on same page for employee such as edit address or add multiple addresses at same time by dynamically adding usercontrols. 我有一个管理页面,该页面允许用户在员工的同一页面上执行CRUD操作,例如编辑地址或通过动态添加用户控件来同时添加多个地址。 I have implemented ListView for displaying, editing and saving on same page. 我已经实现了ListView以便在同一页面上显示,编辑和保存。 Now if I want to achieve same thing ie display/Edit/Save on the same view in MVC could someone suggest good practices for such scenario. 现在,如果我想实现相同的目标,即在MVC中的同一视图上显示/编辑/保存,那么有人可以针对这种情况提出好的做法。 (ps I have partial views ready for such CRUD operations) (请注意,我有部分视图可用于此类CRUD操作)

Any help will be greatly appreciated. 任何帮助将不胜感激。

Yes there are many way you can achieve this in MVC. 是的,您可以通过多种方式在MVC中实现此目标。 Just like ListView,there are many third party controls which acts as ListView in MVC. 就像ListView一样,在MVC中有许多第三方控件充当ListView。 But if you don not want to use those third party controls,then you can go with Table,Tr and Td. 但是,如果您不想使用这些第三方控件,则可以使用Table,Tr和Td。 For that purpose, lets take an example. 为此,让我们举个例子。 Your model is returning the list of data so that you need to display that list on the View. 您的模型正在返回数据列表,因此您需要在视图上显示该列表。 So get that list and use foreach loop and just use Table,TH,TR and TD. 因此,获取该列表并使用foreach循环,仅使用Table,TH,TR和TD。 You can use @Html.ActionLink for Edit,Delete,etc. 您可以使用@ Html.ActionLink进行编辑,删除等操作。 Hope this will help you. 希望这会帮助你。

I have implemented the similar thing in the MVC: 我已经在MVC中实现了类似的功能:

View: 视图:

 // Creating html Table from data present  in model
    <table class="content-wrapper">
        <tr>
            <th>
                @Html.DisplayName("Name")
            </th>
            <th>
                @Html.DisplayName("ID")
            </th>
            <th>
                @Html.DisplayName("Designation")
            </th>
            <th>
            </th>
        </tr>
        @foreach (var item in Model.lstEmployees)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.name)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.id)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.designation)
                </td>
                <td>
                    @Html.ActionLink("Edit", "Edit", new { id = item.id })
                    @Html.ActionLink("Delete", "Delete", new { id = item.id })
                </td>
            </tr>
        }

Controller: 控制器:

public ActionResult Delete(Data model)
{
   //Code for delete
    return View("Index", data);
}

[HttpGet]
        public ActionResult Edit(Int32 Id)
        {
            //code for binding the existing records
            return View(_data);
        }



   [HttpPost]
    public ActionResult Edit(string sampleDropdown, Data model)
    {
        //code for saving the updated data
        return RedirectToAction("Index", "Home");

    }

As I wanted to use the same view to show/edit/save, I found a solution to implement AJAX and render partial views for CRUD operations. 因为我想使用相同的视图来显示/编辑/保存,所以我找到了一种解决方案来实现AJAX并为CRUD操作呈现部分视图。 This can be achieved in many ways but I selected two options: 这可以通过多种方式实现,但是我选择了两个选项:

  1. Using @Ajax.BeginForm and jquery.unobtrusive-ajax.min and just fetching partialview. 使用@ Ajax.BeginForm和jquery.unobtrusive-ajax.min并仅获取partialview。

  2. Using jQuery AJAX to acheive this. 使用jQuery AJAX实现这一点。

Here is a link I found in Stackoverflow very helpful: Using Ajax.BeginForm with ASP.NET MVC 3 Razor 这是我在Stackoverflow中发现的一个非常有用的链接: 将Ajax.BeginForm与ASP.NET MVC 3 Razor一起使用

(Thanks all for your effort) (谢谢大家的努力)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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