【发布时间】:2014-03-27 02:06:41
【问题描述】:
所以,我们可以像这样从控制器返回部分视图:
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);
}
}
}
但是,我应该如何使用返回的局部视图?我在 Views/Home 下创建了 _Address.cshtml,如下所示:
@model MvcApplication1.Models.Address
<p>
This is a partial view of address.
</p>
<p>
@Model.City
</p>
并且,在 Views/Home/Contact.cshtml 的末尾,我添加了这一行:
@Html.Partial(@"~/Views/Home/_Address.cshtml")
我希望看到我的地址所在的城市,但它没有出现。我很困惑。
【问题讨论】:
-
感谢您格式化我的代码。我尝试对其进行格式化,但它拒绝按我想要的方式格式化。
-
其实很简单。只需选择您的代码文本,然后在编辑器中点击
{}按钮,或者确保所有代码都缩进 4 个空格。你也可以用反引号包围内联代码`code` -
好的,谢谢。下次试试。
标签: c# asp.net-mvc asp.net-mvc-4 razor