无法在 mvc4 中使用视图模型概念从同一视图输入数据并在同一视图中显示数据

本文关键字:视图 数据 显示 输入 模型 mvc4 | 更新日期: 2023-09-27 18:36:04

我在理解 mvc 4 中的视图模型概念时遇到了一些困难。我知道创建简单的注册表。我也知道使用视图显示数据。我想在一个视图中同时执行这两个,但无法实现。这就是我正在尝试的。这是模型。

 public class Table
        {  
            public int id { get; set; }
            public string name { get; set; }
            public string address { get; set; }
            public int pincode { get; set; }
        }

这是我的视图代码。

@model c3card.Website.Models.Table
    @{
        ViewBag.Title = "Create";
        Layout = "~/Views/Shared/_LayoutC3Card.cshtml";
    }
    <h2>Create</h2>
    @using (Html.BeginForm()) 
    {
        @Html.AntiForgeryToken()
        <div class="form-horizontal">
            <h4>Table</h4>
            <hr />
            @Html.ValidationSummary(true)
            <div>
                @Html.LabelFor(model => model.name, new { @class = "control-label col-md-2" })
                @Html.EditorFor(model => model.name)  @Html.ValidationMessageFor(model => model.name)
            </div>
            <div>
                @Html.LabelFor(model => model.address, new { @class = "control-label col-md-2" })
                @Html.EditorFor(model => model.address)
            </div>
            <div>
                @Html.LabelFor(model => model.pincode, new { @class = "control-label col-md-2" })
                @Html.EditorFor(model => model.pincode)
            </div>
            <input type="submit" value="Create" class="btn btn-default" />
        </div>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.address)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.pincode)
        </th>
        <th></th>
    </tr>
@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.address)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.pincode)
        </td>
    </tr>
}
</table>
}

我的主要疑问就在这里。上面的表格工作正常。如果我想显示项目,那么我应该把这行代码放在@model IEnumerable<c3card.Website.Models.Table>但是如果我把这行@Html.EditorFor(model => model.name)会出错。所以我想将数据发布到数据库,我想在同一视图中显示。我怎样才能做到这一点?

一旦我输入名称,地址,密码等数据,然后单击提交按钮,我想在下表中显示所有数据。这就是我正在努力实现的目标。我可以通过使用viewbag概念来实现,但我正在避免viewbag概念。

它似乎工作正常,没有错误。现在最后一件事是如何将模型返回到视图?

请注意:获取详细信息是DAL层中的方法。我已经编写了 linq 查询来根据参数检索数据。如果您有任何疑问,请告诉我。

提前谢谢。

这是我的视图代码。

@model c3card.Dal.EDModel.MyViewModel
@{
    ViewBag.Title = "Create";
    Layout = "~/Views/Shared/_LayoutC3Card.cshtml";
}
<h2>Create</h2>
@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()
    <div class="form-horizontal">
        <h4>ts_upld_doc</h4>
        <hr />
        @Html.ValidationSummary(true)

        <div class="form-group">
            <div class="col-md-10">
                @Html.EditorFor(x => x.upload_document.upld_clientid)
            </div>
        </div>
        <div class="form-group">
            <div class="col-md-10">
                @Html.EditorFor(x => x.upload_document.upld_employeeid)
            </div>
        </div>
        <div class="form-group">
            <div class="col-md-10">
                @Html.EditorFor(x => x.upload_document.upld_empcitizenid)
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

现在的问题是提交按钮没有启动。这是我的视图模型。

public class MyViewModel
    {
        public ts_upld_doc upload_document { get; set; }
        public IList<ts_upld_doc> Upload_List { get; set; }
    }

表 ts_upld_doc 包含许多字段,如 client_id、emp_id、citizen_id 等,所以我不在这里发布。

这是我的视图模型。

public class MetaDataClass
    {
        public virtual int upld_docid { get; set; }
        public virtual string upld_docname { get; set; }
        public virtual string upld_contentlabel { get; set; }
        public virtual string upld_contentvalue { get; set; }
    }
   public class DeleteDocApproval
   {
       public ts_upld_doc upload_document { get; set; }
       public IList<ts_upld_doc> Upload_List { get; set; }
       public IList<MetaDataClass> test { get; set; }
   }

这是我使用 GET 的索引方法

public ActionResult Index()
        {
            DeleteDocApproval model = new DeleteDocApproval()
            {
                upload_document = new ts_upld_doc(),
                Upload_List = new List<ts_upld_doc>(),
                test = new List<ts_upld_doc>()
                {
                }
            };

        }

实际上,为了填充我的网格视图,我有一些像这样的大查询。但是我无法将其写在 actuion 方法中。

无法在 mvc4 中使用视图模型概念从同一视图输入数据并在同一视图中显示数据

要在同一视图上显示表单和列表,您需要为此创建ViewModel

所以这是你的课。

 public class Table
    {
        public int id { get; set; }
        public string name { get; set; }
        public string address { get; set; }
        public int pincode { get; set; }
    }

现在创建视图模型,其中包含此类和此类的列表。

public class MyViewModel
    {
        public Table Registertable { get; set; }
        public IList<Table> UserList { get; set; }
    }

要在视图中使用此视图模型。 编写此代码。

@model IeInTouch.Web.Models.MyViewModel

我不是为表单编写所有代码。

这只是样本。在表单内

@Html.TextBoxFor(x => x.Registertable.name) 

显示表头的日期。

@Html.DisplayFor(x => x.Registertable.name)

现在运行 foreach 循环以显示数据。

@foreach(var user in Model.UserList)
{
    @Html.DisplayFor(modelItem  => user.address)
}

现在从操作中传递此视图模型

 public ActionResult Test()
        {
            MyViewModel myViewModel = new MyViewModel()
            {
                Registertable = new Table(),
                UserList = new List<Table>(){
                    new Table(){
                        name="xyz",
                        id=1
                    },
                    new Table(){
                        name="ABC",
                        id=2
                    }
                }
            };
            return View(myViewModel);
        }