Asp.net mvc 4 在MVC中为所有CRUD操作创建单一视图

Asp.net mvc 4 在MVC中为所有CRUD操作创建单一视图,asp.net-mvc-4,Asp.net Mvc 4,我是MVC新手。希望使用单个视图应用简单的CRUD操作。我不希望使用单独的视图,如创建、编辑、删除、读取。我使用代码优先的方法使用实体框架 @model IEnumerable<WareHouse.WareHouseType> @{ ViewBag.Title = "Index"; Layout = "~/Views/Shared/_Layout.cshtml"; WebGrid grid = new WebGrid(M

我是MVC新手。希望使用单个视图应用简单的CRUD操作。我不希望使用单独的视图,如创建、编辑、删除、读取。我使用代码优先的方法使用实体框架

    @model IEnumerable<WareHouse.WareHouseType>
    @{
        ViewBag.Title = "Index";
        Layout = "~/Views/Shared/_Layout.cshtml";
        WebGrid grid = new WebGrid(Model);
    }

    @using(Html.BeginForm()) {
    <div class="tdhdr1" >Warehouse Type Detailsdfgdgfdgdfg
            </div>

        @Html.Partial("_WHT")
        @grid.GetHtml( fillEmptyRows: true,
        alternatingRowStyle: "alternative-row",
        headerStyle: "tdhdr2",

        mode: WebGridPagerModes.All,
        firstText: "<< First",
        previousText: "< Previous", nextText: "Next >",
        lastText: "Last >>",columns: new [] {
        grid.Column("WareHouseTypeID",header: "ID", canSort: false),
        grid.Column("WareHouseTypeName",header:"Name dfgsdfsdf")
    })
    } 
我想要文本框来输入数据。 显示有关特定主机的所有信息的网格视图。 执行操作的按钮添加、更新、删除。 我已尝试以下代码。请帮助我解决此问题

    @model IEnumerable<WareHouse.WareHouseType>
    @{
        ViewBag.Title = "Index";
        Layout = "~/Views/Shared/_Layout.cshtml";
        WebGrid grid = new WebGrid(Model);
    }
    @using(Html.BeginForm) {
    <div class="tdhdr1" >Warehouse Type Details
            </div>


    @grid.GetHtml( fillEmptyRows: true,
        alternatingRowStyle: "alternative-row",
        headerStyle: "tdhdr2",

        mode: WebGridPagerModes.All,
        firstText: "<< First",
        previousText: "< Previous", nextText: "Next >",
        lastText: "Last >>",columns: new [] {
        grid.Column("WareHouseTypeID",header: "ID", canSort: false),
        grid.Column("WareHouseTypeName",header:"Name ")
    })
    }
    @model IEnumerable<WareHouse.WareHouseType>
    @{
        ViewBag.Title = "Index";
        Layout = "~/Views/Shared/_Layout.cshtml";
        WebGrid grid = new WebGrid(Model);
    }

    @using(Html.BeginForm()) {
    <div class="tdhdr1" >Warehouse Type Detailsdfgdgfdgdfg
            </div>

        @Html.Partial("_WHT")
        @grid.GetHtml( fillEmptyRows: true,
        alternatingRowStyle: "alternative-row",
        headerStyle: "tdhdr2",

        mode: WebGridPagerModes.All,
        firstText: "<< First",
        previousText: "< Previous", nextText: "Next >",
        lastText: "Last >>",columns: new [] {
        grid.Column("WareHouseTypeID",header: "ID", canSort: false),
        grid.Column("WareHouseTypeName",header:"Name dfgsdfsdf")
    })
    } 
以上代码仅适用于gridview。 我希望其他控件也在同一视图上。是否可以使用局部视图? 我不能为添加编辑器。同样的观点

    @model IEnumerable<WareHouse.WareHouseType>
    @{
        ViewBag.Title = "Index";
        Layout = "~/Views/Shared/_Layout.cshtml";
        WebGrid grid = new WebGrid(Model);
    }

    @using(Html.BeginForm()) {
    <div class="tdhdr1" >Warehouse Type Detailsdfgdgfdgdfg
            </div>

        @Html.Partial("_WHT")
        @grid.GetHtml( fillEmptyRows: true,
        alternatingRowStyle: "alternative-row",
        headerStyle: "tdhdr2",

        mode: WebGridPagerModes.All,
        firstText: "<< First",
        previousText: "< Previous", nextText: "Next >",
        lastText: "Last >>",columns: new [] {
        grid.Column("WareHouseTypeID",header: "ID", canSort: false),
        grid.Column("WareHouseTypeName",header:"Name dfgsdfsdf")
    })
    } 
但它抛出错误**传递到字典中的模型项的类型为“System.Collections.Generic.List`1[WareHouseType]”,但此字典需要类型为“WareHouseType.WareHouseType”的模型项**

    @model IEnumerable<WareHouse.WareHouseType>
    @{
        ViewBag.Title = "Index";
        Layout = "~/Views/Shared/_Layout.cshtml";
        WebGrid grid = new WebGrid(Model);
    }

    @using(Html.BeginForm()) {
    <div class="tdhdr1" >Warehouse Type Detailsdfgdgfdgdfg
            </div>

        @Html.Partial("_WHT")
        @grid.GetHtml( fillEmptyRows: true,
        alternatingRowStyle: "alternative-row",
        headerStyle: "tdhdr2",

        mode: WebGridPagerModes.All,
        firstText: "<< First",
        previousText: "< Previous", nextText: "Next >",
        lastText: "Last >>",columns: new [] {
        grid.Column("WareHouseTypeID",header: "ID", canSort: false),
        grid.Column("WareHouseTypeName",header:"Name dfgsdfsdf")
    })
    } 
部分视图代码

@model WareHouse.WareHouseType

@{
    Layout = null;
}

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>AddProduct</title>    
</head>
<body>
    <div>      
           <label>Id:</label>
            @Html.TextBox("Id", Model.WareHouseTypeID)
             <label>Name:</label>
            @Html.TextBox("Name", Model.WareHouseTypeName)

    </div>
</body>
</html>                                                                                    
    @model IEnumerable<WareHouse.WareHouseType>
    @{
        ViewBag.Title = "Index";
        Layout = "~/Views/Shared/_Layout.cshtml";
        WebGrid grid = new WebGrid(Model);
    }

    @using(Html.BeginForm()) {
    <div class="tdhdr1" >Warehouse Type Detailsdfgdgfdgdfg
            </div>

        @Html.Partial("_WHT")
        @grid.GetHtml( fillEmptyRows: true,
        alternatingRowStyle: "alternative-row",
        headerStyle: "tdhdr2",

        mode: WebGridPagerModes.All,
        firstText: "<< First",
        previousText: "< Previous", nextText: "Next >",
        lastText: "Last >>",columns: new [] {
        grid.Column("WareHouseTypeID",header: "ID", canSort: false),
        grid.Column("WareHouseTypeName",header:"Name dfgsdfsdf")
    })
    } 
一般视图代码:

    @model IEnumerable<WareHouse.WareHouseType>
    @{
        ViewBag.Title = "Index";
        Layout = "~/Views/Shared/_Layout.cshtml";
        WebGrid grid = new WebGrid(Model);
    }

    @using(Html.BeginForm()) {
    <div class="tdhdr1" >Warehouse Type Detailsdfgdgfdgdfg
            </div>

        @Html.Partial("_WHT")
        @grid.GetHtml( fillEmptyRows: true,
        alternatingRowStyle: "alternative-row",
        headerStyle: "tdhdr2",

        mode: WebGridPagerModes.All,
        firstText: "<< First",
        previousText: "< Previous", nextText: "Next >",
        lastText: "Last >>",columns: new [] {
        grid.Column("WareHouseTypeID",header: "ID", canSort: false),
        grid.Column("WareHouseTypeName",header:"Name dfgsdfsdf")
    })
    } 
控制器代码

    @model IEnumerable<WareHouse.WareHouseType>
    @{
        ViewBag.Title = "Index";
        Layout = "~/Views/Shared/_Layout.cshtml";
        WebGrid grid = new WebGrid(Model);
    }

    @using(Html.BeginForm()) {
    <div class="tdhdr1" >Warehouse Type Detailsdfgdgfdgdfg
            </div>

        @Html.Partial("_WHT")
        @grid.GetHtml( fillEmptyRows: true,
        alternatingRowStyle: "alternative-row",
        headerStyle: "tdhdr2",

        mode: WebGridPagerModes.All,
        firstText: "<< First",
        previousText: "< Previous", nextText: "Next >",
        lastText: "Last >>",columns: new [] {
        grid.Column("WareHouseTypeID",header: "ID", canSort: false),
        grid.Column("WareHouseTypeName",header:"Name dfgsdfsdf")
    })
    } 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WareHouse;

namespace CommonMasterUI.Controllers
{
    public class WHTypeController : Controller
    {


              WareHouse.Context ctx = new WareHouse.Context();
        IList<WareHouseType> list2 = new List<WareHouseType>();
        [HttpGet]
        public ActionResult Index()
        {
            IList<WareHouseType> wht = new List<WareHouseType>();  
            WareHouseRepository wr = new WareHouseRepository(ctx);
            wht = wr.GetWareHouseTP().ToList();


            for (int i = 0; i < wht.Count; i++)
            {
                WareHouseType wt = new WareHouseType();

                wt.WareHouseTypeID = wht.ElementAt(i).WareHouseTypeID;
                wt.WareHouseTypeName = wht.ElementAt(0).WareHouseTypeName.ToString();
                list2.Add(wt);

            }


            return View(list2.ToList());
        }
    }
}

谢谢您能否共享此的示例代码…可能与此重复。请验证上述链接,并让我们知道它是否完全满足您的要求。我已经完成了上述解决方案。现在,我已为文本框创建了部分视图。如果您仍然存在任何问题,请发布正确的代码。但它会引发错误**传递到字典中的模型项的类型为“System.Collections.Generic.List1”[WareHouse.WareHouseType]”,但此字典需要类型为“WareHouse.WareHouseType.”***的模型项。在某些情况下,您使用单个实体而不是所需的数组调用视图,是否可以发布控制器代码?