【问题标题】:Want to use same model twice on same view with different type?想在不同类型的同一个视图上使用相同的模型两次?
【发布时间】:2016-08-17 22:19:42
【问题描述】:

目前我正在使用 asp mvc 视图 我想在一个视图上呈现数据...具有显示数据并在同一视图上创建表单但无法完成..因为控制器返回 IEnumerable 类型数据并创建表单的 editorfor 不包含该定义。

我有一个模特"T4.Models.Order"

如果我将此模型用作@model IEnumerable<T4.Models.Order>

创建表单剃刀语法显示错误

如果我将此模型用作@model T4.Models.Order

显示数据显示的错误

这是我的代码

@model IEnumerable<T4.Models.Order>

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <p>
        @Html.ActionLink("Create New", "Create")
    </p>
    <table class="table table-striped table-bordered" cellspacing="0" width="100%">
     <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.CustomerID)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.CustomerID)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.OrderDate)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.RequiredDate)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.ShippedDate)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.ShipVia)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Freight)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.ShipName)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.ShipAddress)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.ShipCity)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.ShipRegion)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.ShipPostalCode)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.ShipCountry)
            </th>

        </tr>
    </thead>
        @*@{
            IEnumerable<T4.Models.Order> m = @model;
        }*@
    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.CustomerID)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.EmployeeID)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.OrderDate)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.RequiredDate)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.ShippedDate)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.ShipVia)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Freight)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.ShipName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.ShipAddress)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.ShipCity)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.ShipRegion)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.ShipPostalCode)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.ShipCountry)
            </td>
        </tr>
    }

    </table>
</body>
</html>



@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Order</h4>
        <hr />
        @Html.ValidationSummary(true)

        <div class="form-group">
            @Html.LabelFor(model => model.CustomerID, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.CustomerID)
                @Html.ValidationMessageFor(model => model.CustomerID)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.EmployeeID, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.EmployeeID)
                @Html.ValidationMessageFor(model => model.EmployeeID)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.OrderDate, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.OrderDate)
                @Html.ValidationMessageFor(model => model.OrderDate)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.RequiredDate, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.RequiredDate)
                @Html.ValidationMessageFor(model => model.RequiredDate)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.ShippedDate, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.ShippedDate)
                @Html.ValidationMessageFor(model => model.ShippedDate)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.ShipVia, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.ShipVia)
                @Html.ValidationMessageFor(model => model.ShipVia)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Freight, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Freight)
                @Html.ValidationMessageFor(model => model.Freight)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.ShipName, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.ShipName)
                @Html.ValidationMessageFor(model => model.ShipName)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.ShipAddress, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.ShipAddress)
                @Html.ValidationMessageFor(model => model.ShipAddress)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.ShipCity, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.ShipCity)
                @Html.ValidationMessageFor(model => model.ShipCity)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.ShipRegion, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.ShipRegion)
                @Html.ValidationMessageFor(model => model.ShipRegion)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.ShipPostalCode, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.ShipPostalCode)
                @Html.ValidationMessageFor(model => model.ShipPostalCode)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.ShipCountry, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.ShipCountry)
                @Html.ValidationMessageFor(model => model.ShipCountry)
            </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 ActionResult Index()
{
   return View(db.Order.ToList());
}

注意:我不想更改控制器端的任何内容。

【问题讨论】:

  • 你是否在控制器的一个Action方法中混合显示和创建表单的代码?我认为你应该使用局部视图,用@Html.ActionLink 调用它,并在控制器中单独创建和显示操作。
  • ^抱歉,我的意思是@Html.Action,而不是@Html.ActionLink
  • 我不想使用任何局部视图或控制器端的任何更改..我必须在 cshtml 上使用剃刀语法..所以我也不能使用 html...如果你请给我解决方案有...谢谢你
  • 请提供您的控制器代码。这取决于您的代码现在的外观。 stackoverflow.com/a/13852500/3387187
  • @bounces 您提供的链接也有部分视图解决方案..正如我提到的我不想使用任何部分视图..正如你提到的我也更新了我的控制器端代码..请看那个代码...

标签: asp.net-mvc asp.net-mvc-3 asp.net-mvc-4 razor ienumerable


【解决方案1】:

只需创建模型类的对象并用于呈现您的表单..

这里是代码

@{
    T4.Models.Order o = new T4.Models.Order();
}

你的表格

@using (Html.BeginForm("Create","Orders"))
    {
        @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Order</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => o.CustomerID, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => o.CustomerID, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => o.CustomerID, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => o.EmployeeID, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => o.EmployeeID, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => o.EmployeeID, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => o.OrderDate, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => o.OrderDate, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => o.OrderDate, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => o.RequiredDate, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => o.RequiredDate, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => o.RequiredDate, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => o.ShippedDate, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => o.ShippedDate, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => o.ShippedDate, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => o.ShipVia, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => o.ShipVia, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => o.ShipVia, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => o.Freight, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => o.Freight, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => o.Freight, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => o.ShipName, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => o.ShipName, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => o.ShipName, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => o.ShipAddress, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => o.ShipAddress, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => o.ShipAddress, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => o.ShipCity, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => o.ShipCity, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => o.ShipCity, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => o.ShipRegion, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => o.ShipRegion, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => o.ShipRegion, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => o.ShipPostalCode, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => o.ShipPostalCode, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => o.ShipPostalCode, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => o.ShipCountry, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => o.ShipCountry, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => o.ShipCountry, "", new { @class = "text-danger" })
            </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>
    }

    <div>
        @Html.ActionLink("Back to List", "Index")
    </div>

【讨论】:

    【解决方案2】:

    如果您想列出现有订单并在同一视图中显示创建表单,您应该创建一个具有此属性的新视图模型。您可以从现有的T4.Models.Order 继承它。

    public class CreateAndListVm : T4.Models.Order
    {
      public List<Order> Orders { set;get;}
      public CreateAndListVm()
      {
        this.Orders = new List<Order>();
      }
    }
    

    现在你的视图将被强类型化到这个视图模型

    @model CreateAndListVm
    <h2>New item</h2>
    @using(Html.BeginForm())
    {
      @Html.TextBoxFor(s=>s.CustomerID)
      <input type="submit" />
    }
    <h3>Existing orders</h3>
    @foreach(var item in Model.Orders)
    {
      <p>@item.CustomerID</p>
    }
    

    当然这意味着,您应该从您的操作方法中发送一个 CreateAndListVm 对象。

    public ActionResult Index()
    {
      var vm = new CreateAndListVm();
      // to do : Load the orders to vm.Orders;
      return View(vm)
    }
    

    如果您不想更改控制器并且它正在将 Order 集合返回到视图,那么您还有另一种选择。不要使用 Html 辅助方法来生成输入表单字段。只需自己编写输入字段的 html 代码即可。

    假设您的操作方法返回一个 Order 类列表

    public ActionResult Index()
    {
      var orders = new List<Order>();
      return View(orders);
    }
    

    现在在你看来,它是一个 Order 集合的强类型

    @model IEnumerable<Order>
    <h2>Create</h2>
    <form action="Create" method="POST">
      <input type="text" name="CustomerID" />
      <input type="submit" />
    </form>
    <h3>Existing orders</h3>
    @foreach(var item in Model)
    {
      <p>@item.CustomerID</p>
    }
    

    【讨论】:

    • 我不想改变控制器端的任何东西,正如我在注释中提到的那样。请仅在视图方面为我提供解决方案。我不想使用任何局部视图。我可以同时使用“@model IEnumerable”和“@model T4.Models.Order”,或者以不同的方式使用cshtml。
    • 检查答案的第二部分。
    • 感谢您的回答@shyju ..您说没有使用Htmlhelper,但我必须使用Htmlhelper...如果有,请提供另一种解决方案。
    • 为什么必须使用 html 辅助方法?它基本上基于模型属性生成输入表单字段标记(就像您为此编写 html 代码的方式一样)。如果您不能使用新的视图模型(就像我在回答的第一部分中提到的那样),您应该简单地为表单输入字段创建 html 标记。
    • 因为我想执行模型验证所以我不想使用任何 html..我想使用剃刀语法..这就是为什么我很困惑...回答你提供给我的答案所有在我的脑海里准备好了......但情况不同。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-22
    相关资源
    最近更新 更多