简体   繁体   English

返回视图中的部分视图

[英]Return partial view within view

I have a view Index.cshtml : 我有一个Index.cshtml视图:

@{
    ViewBag.Title = "Index";
}

<h2>Add</h2>

<p>Begin building a question set by clicking the button below.</p>
<input type="button" value="Create" onclick="location.href='@Url.Action("Product", "Add")'" />

Which calls the Product action within my AddController: 哪个在我的AddController中调用Product动作:

public ActionResult Product()
{
   ViewBag.Partial = true;
   return PartialView("Product");
}

What I'm trying to achieve is: 我想要实现的是:

When a user loads the page the content from my partial view ( Product.cshtml ) is hidden, however when they click the "Create" button which calls the Product action in my AddController I want to then load the Product.cshtml partial view into my Index.cshtml so the latter still has its original content, but this time with the Product.cshtml injected into it. 当用户加载页面时,隐藏了我的局部视图( Product.cshtml )中的内容,但是,当他们单击“创建”按钮时,该按钮在AddController调用Product动作,我想将Product.cshtml局部视图加载到我的Index.cshtml因此后者仍然具有其原始内容,但是这次将Product.cshtml注入其中。

At the moment, it's only returning the partial view and not both views. 目前,它仅返回部分视图,而不是两个视图。

Is this achievable in MVC? 这在MVC中可以实现吗?

Yes, basically what you need is to get your PartialView through AJAX call and load it into your page. 是的,基本上,您需要通过AJAX调用获取PartialView并将其加载到页面中。 Easiest to do with jQuery 最简单的使用jQuery

You can do it with simple JavaScript. 您可以使用简单的JavaScript来实现。

In buttons onclick put name of your loading function, like onclick="partialLoad()" 在按钮上, onclick放置加载函数的名称,例如onclick="partialLoad()"

Put div with some id on your page, like <div id="loadFrame"></div> 在页面上放置一些id div ,例如<div id="loadFrame"></div>

Then your script: 然后您的脚本:

function parialLoad() {
    $("#loadFrame").load("/Product");
}

My suggestion would be the following. 我的建议如下。

Move your Products.cshtml to the Views/Shared/EditorTemplates or DisplayTemplates directory 将您的Products.cshtml移至Views / Shared / EditorTemplates或DisplayTemplates目录

Have a ViewModel that you populate that contains a list of Products that you pass to and from your view. 有一个您填充的ViewModel,其中包含要传递给视图和从视图传递的产品的列表。

Thus: 从而:

HomeController.cs HomeController.cs

public ActionResult Index()
{
    var model = new ProductViewModel();

    return View(model);
}

[HttpPost]
public ActionResult Index(ProductViewModel model)
{
    if (model == null)
    {
        model = new ProductViewModel();
    }
    if (model.Products == null)
    {
        model.Products = new List<Product>();
    }
    model.Products.Add(new Product { Name = "Some Product", Amount = model.Products.Count + 1});

    return View(model);
}

ProductViewModel.cs ProductViewModel.cs

public class ProductViewModel
{
    public List<Product> Products { get; set; }
}

Index.cshtml Index.cshtml

@model DynamicViewExample.Controllers.ProductViewModel
@{
    ViewBag.Title = "About";
}

@using (Html.BeginForm())
{
    <h2>Add</h2>


    @Html.DisplayFor(m => m.Products)

    <p>Begin building a question set by clicking the button below.</p>
    <input type="submit" value="Add Product" />
}

And finally, the DisplayTemplate 最后是DisplayTemplate
Product.cshtml Product.cshtml

@model DynamicViewExample.Models.Product
<div>
    @Html.HiddenFor(x => x.Name)
    @Html.HiddenFor(x => x.Amount)
    @Html.DisplayFor(x => x.Name)
    @Html.DisplayFor(x => x.Amount)
</div>

The reason I had to include the HiddenFor properties, was to enable the Products collection to populate otherwise you would get an empty collection all the time. 我之所以必须包含HiddenFor属性,是为了使Products集合能够填充,否则您将始终得到一个空集合。

Again, this was a display template, that would return the following result 同样,这是一个显示模板,将返回以下结果

Add

Some Product 1 一些产品1
Some Product 2 一些产品2
Some Product 3 一些产品3
Some Product 4 一些产品4
Some Product 5 一些产品5
Begin building a question set by clicking the button below. 单击下面的按钮开始构建问题集。

I hope this guides you into the direction you are wishing to go 我希望这可以引导您进入您希望的方向

Good Luck 祝好运
Gawie Schneider 盖维·施耐德(Gawie Schneider)

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

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