我这样称呼我的部分观点:
<% Html.RenderPartial("~/controls/users.ascx"); %>
我可以将参数传递给局部视图吗?我将如何在实际的 users.ascx 页面中访问它们?
我这样称呼我的部分观点:
<% Html.RenderPartial("~/controls/users.ascx"); %>
我可以将参数传递给局部视图吗?我将如何在实际的 users.ascx 页面中访问它们?
You could pass a model object to the partial (for example a list of strings):
<% Html.RenderPartial("~/controls/users.ascx", new string[] { "foo", "bar" }); %>
Then you strongly type the partial and the Model
property will be of the appropriate type:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<System.Collections.Generic.IEnumerable<string>>" %>
<% foreach (var item in Model) { %>
<div><%= Html.Encode(item) %></div>
<% } %>
There is another overload for RenderPartial that will pass your model through.
<% Html.RenderPartial("~/controls/users.ascx", modelGoesHere); %>
How to access? Just like you normally would with any view:
<%= Model.MagicSauce %>
花了一些时间才明白,但 MVC 意味着您以一种或另一种方式使用模型、视图和控制器来处理几乎所有事情,包括部分视图。起初,这三个元素如何组合在一起可能有点令人生畏。直到现在我才做过一个,而且它有效——哇哦!
希望这对下一个人有所帮助.... 抱歉,我使用的是 razor 而不是 .Net 表单。我还将数据从 SQL Server 数据库提取到开发人员可能使用的实体框架中。我也可能对 WebGrid 有点过分了,它比 foreach 语句要优雅得多。一个基本的@webgrid.GetHtml() 将显示每一列和每一行。
在这个工作示例中,用户上传了图片。他们的图片使用局部视图以编辑形式显示。ImageID 和 FileName 元数据保存在 SQL Server 中,而文件本身保存在 ~/Content/UserPictures 目录中。
我知道它有点大,因为没有显示上传和编辑个人数据的所有细节。只关注使用局部视图的密切相关部分,尽管加入了一些额外的 EF。命名空间是 S&G 的 MVCApp3。
部分视图模型ViewModels.cs
除了 ImageID 和 FileName 之外,SQL Server 图像表还包括更多列,例如 [Caption]、[Description]、MD5 哈希以防止多次上传同一图像,以及上传日期。ViewModel 将实体提炼到用户查看其图片所需的最低限度。
public class Picts
{
public int ImageID { get; set; }
public string FileName { get; set; }
}
主视图视图Edit.cshtml
请注意强制类型转换/转换为 ViewData[]。
@Html.Partial(
partialViewName: "Picts",
model: (IEnumerable<MVCApp3.Models.Picts>)ViewData["Picts"]
)
如果您没有将强类型模型设置为用于局部视图,您将收到“传递到字典中的模型项的类型为 'System.Data.Entity.DynamicProxies... ”错误,因为它假定您'正在传递父/主模型。
部分视图查看Picts.cshtml(显示整个文件内容)
@model IEnumerable<MVCApp3.Models.Picts>
@{
var pictsgrid = new WebGrid(Model);
}
@pictsgrid.GetHtml(
tableStyle: "grid",
displayHeader: false,
alternatingRowStyle: "alt",
columns: pictsgrid.Columns(
pictsgrid.Column(format:@<text><img src="@Url.Content("~/Content/Users/" + @item.FileName)" alt="@item.ImageID" width="200" />
@Html.ActionLink(linkText: "Delete", actionName: "DeletePicture", routeValues: new { id = @item.ImageID })
</text>)
))
控制器IdentityController.cs
将数据内容设置为部分视图将使用的 ViewData["MyPartialViewModelKeyName"]。你可以给字典键任何你想要的名字,但我给它 ViewData["Picts"] 是为了与局部视图文件名和它的视图模型类定义保持一致。
因为图片可能在多个用户之间共享,所以在 Entity Framework 中存在一个多对多表,其中包含相应的 PITA 查询,使用嵌套的 froms 和内部连接来仅返回属于用户或与用户共享的图片:
public class IdentityController : Controller
{
private EzPL8Entities db = new EzPL8Entities();
// GET: /Identity/Edit/5
[Authorize]
public ActionResult Edit(int? id)
{
if (id == null)
return new HttpNotFoundResult("This doesn't exist");
// get main form data
ezpl8_UsersPhysicalIdentity ezIDobj = db.ezpl8_UsersPhysicalIdentity.Find(id)
// http://learnentityframework.com/LearnEntityFramework/tutorials/many-to-many-relationships-in-the-entity-data-model/
// get partial form data for just this user's pictures
ViewData["Picts"] = (from user in db.ezpl8_Users
from ui in user.ezpl8_Images
join image in db.ezpl8_Images
on ui.ImageID equals image.ImageID
where user.ezpl8_UserID == id
select new Picts
{
FileName = image.FileName,
ImageID = image.ImageID
}
).ToList();
return View(ezIDobj);
}
// Here's the Partial View Controller --not much to it!
public ViewResult Picts(int id)
{
return View(ViewData["Picts"]);
}
[Authorize] //you have to at least be logged on
public ActionResult DeletePicture(int id)
{
//ToDo: better security so a user can't delete another user's picture
// TempData["ezpl8_UserID"]
ezpl8_Images i = db.ezpl8_Images.Find(id);
if (i != null)
{
var path = System.IO.Path.Combine(Server.MapPath("~/Content/Users"), i.FileName);
System.IO.File.Delete(path: path);
db.ezpl8_Images.Remove(i);
db.SaveChanges();
}
return Redirect(Request.UrlReferrer.ToString());
}
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
}
// get main form data
ezpl8_UsersPhysicalIdentity ezIDobj = db.ezpl8_UsersPhysicalIdentity.Find(id)
// http://learnentityframework.com/LearnEntityFramework/tutorials/many-to-many-relationships-in-the-entity-data-model/
// get partial form data for just this user's pictures
ViewData["Picts"] = (from user in db.ezpl8_Users
from ui in user.ezpl8_Images
join image in db.ezpl8_Images
on ui.ImageID equals image.ImageID
where user.ezpl8_UserID == id
select new Picts
{
FileName = image.FileName,
ImageID = image.ImageID
}
).ToList();
return View(ezIDobj);
}
// 这里是局部视图控制器——没什么大不了的!公共 ViewResult Picts(int id) { return View(ViewData["Picts"]); }
[Authorize] //you have to at least be logged on
public ActionResult DeletePicture(int id)
{
//ToDo: better security so a user can't delete another user's picture
// TempData["ezpl8_UserID"]
ezpl8_Images i = db.ezpl8_Images.Find(id);
if (i != null)
{
var path = System.IO.Path.Combine(Server.MapPath("~/Content/Users"), i.FileName);
System.IO.File.Delete(path: path);
db.ezpl8_Images.Remove(i);
db.SaveChanges();
}
return Redirect(Request.UrlReferrer.ToString());
}
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
}