【问题标题】:MVC, Normal partial view and List partial view. not working together!!! S.O.S. xDMVC、普通局部视图和列表局部视图。不合作!!!求救xD
【发布时间】:2017-04-21 06:11:51
【问题描述】:

我有一个包含 2 个部分视图的视图。

@model ListViewModel
....
@{        
    Html.RenderPartial("EditCartItemsPartical");
    Html.RenderPartial("ShowProductINFO", Model.Products);
}

我只想用第一个部分创建一个 from,用第二个部分创建一个列表。

部分视图

EditCartItemsPartical.cshtml

@model  TestNewATM3._0.Models.CartItem
@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()
    <div class="form-horizontal">
        <div class="form-group">
            @Html.LabelFor(model => model.CartId, "CartId", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.CartId, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.CartId, "", new { @class = "text-danger" })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.ProductId, "ProductId", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.ProductId, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.ProductId, "", new { @class = "text-danger" })
            </div>
        </div>
        .... // more control for CartItem
        <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>
}

ShowProductINFO.cshtml

@model IEnumerable<TestNewATM3._0.Models.AllProduct2>
<p>@Html.ActionLink("Create New", "Create")</p>
<table class="table">
    <tr>
        <th>ID</th>
        <th>@Html.DisplayNameFor(model => model.Title)</th>
    </tr>
    @foreach (var item in Model) {
        <tr>
            <td>@Html.DisplayFor(model => item.Id)</td>
            <td>@Html.DisplayFor(modelItem => item.Title)</td>
        </tr>
    }
</table>

在我的控制器中我得到了这个。

public ActionResult Edit()
{
    var db = ApplicationDbContext.Create();
    var list = new ListViewModel
    {
        Products = db.AllProduct2s.ToList(),
        Users = db.Users.ToList(),
        Cart = db.Carts.ToList(),
        CartItem = db.CartItems.ToList()
    };
    return View(list);
}

但问题是我不能同时显示两个部分,因为如果我在我看来发送list,那么我的第一个部分会出现问题,因为它需要@model TestNewATM3.0.Models.CartItem。但如果我不发送列表我的列表将不会显示,因为我不发送它。

那么如何同时显示一个普通的局部表单视图和一个列表局部视图呢?

【问题讨论】:

  • 只需Html.RenderPartial("EditCartItemsPartical", new CartItem());(将CartItem 的新实例传递给部分视图)。
  • 您可能需要阅读 this question/answer 以了解您可能遇到的错误(即使您没有在问题中包含它)
  • 谢谢我会看看它

标签: asp.net-mvc visual-studio razor


【解决方案1】:

我在理解您的示例时遇到了一点困难(即您有一个带有创建按钮的编辑视图),看起来您正在尝试创建一个视图来编辑购物车。

注意:为了清楚起见,我建议将模型的 CartItem 属性重命名为 CartItems,但我将在下面的示例中使用当前的属性名称。

您可以像这样为列表中的每个项目呈现部分视图, 但这种方法不仅有点混乱:

foreach(var cartItem in Models.CartItem){

    Html.RenderPartial("EditCartItemsPartical", cartItem);

}

一种更简单且性能更高的方法是编辑第一个视图,使其包含购物车项目的集合

像这样

@model  IEnumerable<TestNewATM3._0.Models.CartItem>

@using (Html.BeginForm()) 
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
    @foreach(var modelItem in Model.CartItem){      
        <h4>CartItem</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => modelItem.CartId, "CartId", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => modelItem.CartId, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => modelItem.CartId, "", new { @class = "text-danger" })
            </div>
        </div>

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

        <div class="form-group">
            @Html.LabelFor(model => model.Aantal, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => modelItem.Aantal, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => modelItem.Aantal, "", new { @class = "text-danger" })
            </div>
        </div>
    }       
    <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Save" class="btn btn-default" />
            </div>
    </div>      
</div>
}

当然,这将需要您更改服务器端代码以接收购物车物品的集合,但从用户的角度来看,不必多次发回表单,完成更改将带来更好的体验.

免责声明:我在记事本中更改了上述视图,可能需要稍作调整才能完美运行

我希望这会有所帮助。

【讨论】:

  • 呃,很抱歉这个问题对你来说不是很清楚。我尝试在 1 个普通视图中获得 2 个部分视图。如果只使用Html.RenderPartial("ShowProductINFO", Model.Products);,该列表就可以正常工作,但如果我添加其他部分,它的崩溃并说它需要一个像:@model TestNewATM3._0.Models.CartItem 这样的模型,但我不知道如何给那个模型。感谢您的回答
猜你喜欢
  • 2015-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-15
相关资源
最近更新 更多