简体   繁体   English

从控制器返回局部视图?

[英]Return a partial view from a controller?

So, we can return a partial view from a controller like this:所以,我们可以像这样从控制器返回一个局部视图:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication1.Models;

namespace MvcApplication1.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";

            return View();
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your app description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }

        public PartialViewResult Address()
        {
            Address a = new Address 
            { 
                Line1 = "111 First Ave N.", 
                Line2 = "APT 222", 
                City = "Miami", 
                State = "FL", 
                Zip = "33133" 
            };

            return PartialView(@"~/Views/Home/_Address.cshtml", a);
        }
    }
}

But, how am I supposed to use the returned partial view?但是,我应该如何使用返回的局部视图? I created _Address.cshtml under Views/Home, like this:我在 Views/Home 下创建了 _Address.cshtml,如下所示:

@model MvcApplication1.Models.Address

<p>
    This is a partial view of address.
</p>
<p>
  @Model.City
</p>

And, at the end of Views/Home/Contact.cshtml, I added this line:并且,在 Views/Home/Contact.cshtml 的末尾,我添加了这一行:

@Html.Partial(@"~/Views/Home/_Address.cshtml")

And I expect to see the City of my address, but it doesn't show up.我希望看到我的地址所在的城市,但它没有出现。 I am confused.我很迷惑。

When the partial takes a different model than the method you're including it in you need to use the overload that takes a model parameter and supply the model for the view.当部分采用与包含它的方法不同的模型时,您需要使用采用模型参数并为视图提供模型的重载。 By default it uses the same model as the including view.默认情况下,它使用与包含视图相同的模型。 Typically you only need the path if it's in a different, non-shared folder.通常,如果路径位于不同的非共享文件夹中,则您只需要该路径。 If it's in the same controller's folder, using just the name should do the trick.如果它在同一个控制器的文件夹中,只使用名称应该可以解决问题。

@Html.Partial("_Address", Model.Address)

On the other hand, if you're asking how do I get the partial view from an action included in my page, then you want to use the Action method instead of the Partial method.另一方面,如果您问我如何从包含在我的页面中的操作获取局部视图,那么您希望使用Action方法而不是Partial方法。

@Html.Action("Address")

EDIT编辑

To make the partial work you need to pass a Contact model to the contact view.要使部分工作,您需要将Contact模型传递给联系人视图。

public ActionResult Contact()
{
     var contact = new Contact
     {
        Address = new Address
                  { 
                       Line1 = "111 First Ave N.",
                       Line2 = "APT 222",
                       City = "Miami",
                       State = "FL",
                       Zip = "33133"
                  }
     }

     return View(contact);
}

demo for you:为您演示:

    public ActionResult Update(Demo model)
{
    var item = db.Items.Where(item => item.Number == model.Number).First();
    if (item.Type=="EXPENSIVE")
    {
        return PartialView("name Partial", someViewModel);
    }
}

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

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