mrb*_*lah 34 asp.net-mvc renderpartial
我这样称呼我的部分观点:
<% Html.RenderPartial("~/controls/users.ascx"); %>
Run Code Online (Sandbox Code Playgroud)
我可以将参数传递给局部视图吗?我将如何在实际的users.ascx页面中访问它们?
Dar*_*rov 32
您可以将模型对象传递给partial(例如字符串列表):
<% Html.RenderPartial("~/controls/users.ascx", new string[] { "foo", "bar" }); %>
Run Code Online (Sandbox Code Playgroud)
然后,您强烈键入partial,Model
属性将是适当的类型:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<System.Collections.Generic.IEnumerable<string>>" %>
<% foreach (var item in Model) { %>
<div><%= Html.Encode(item) %></div>
<% } %>
Run Code Online (Sandbox Code Playgroud)
Joh*_*ell 17
RenderPartial还有另一个重载,它将通过您的模型.
<% Html.RenderPartial("~/controls/users.ascx", modelGoesHere); %>
Run Code Online (Sandbox Code Playgroud)
怎么访问?就像你通常会对任何观点一样:
<%= Model.MagicSauce %>
Run Code Online (Sandbox Code Playgroud)
花了一段时间才沉入其中,但MVC意味着您可以以某种方式使用模型,视图和控制器,包括部分视图.三个元素如何组合在一起起初可能有点令人生畏.我从来没有做过,直到现在,它的确有效 - 喔!
希望这有助于下一个人....抱歉,我使用的是剃须刀而不是.Net表格.我还将数据从SQL Server数据库提取到Entity Framework,开发人员可能会使用它.我也可能对WebGrid有点过分了,它比foreach声明更优雅.基本的@ webgrid.GetHtml()将显示每个列和行.
在此工作示例中,用户已上传图片.他们的图片使用局部视图以编辑形式显示.ImageID和FileName元数据在SQL Server中保留,而文件本身保存在〜/ Content/UserPictures目录中.
我知道它有点大,因为没有显示上传和编辑个人数据的所有细节.只是使用部分视图的密切关注部分,虽然有一些奖励EF投入.名称空间是S&G的MVCApp3.
部分视图模型 ViewModels.cs
除了ImageID和FileName之外,SQL Server Images表还包含更多列,例如[Caption],[Description],MD5哈希以防止同一图像被多次上传,以及上传日期.ViewModel将实体提取到用户查看其图片所需的最低限度.
public class Picts
{
public int ImageID { get; set; }
public string FileName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
主视图查看 Edit.cshtml
请注意强制类型转换/转换为ViewData [].
@Html.Partial(
partialViewName: "Picts",
model: (IEnumerable<MVCApp3.Models.Picts>)ViewData["Picts"]
)
Run Code Online (Sandbox Code Playgroud)
如果你没有设置强类型模型用于部分视图,你会得到一个" 传递到字典中的模型项是'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>)
))
Run Code Online (Sandbox Code Playgroud)
控制器 IdentityController.cs
将数据内容设置为部分视图将使用的ViewData ["MyPartialViewModelKeyName"].你可以给字典键任意你想要的名字,但我给它ViewData ["Picts"]与部分视图文件名及其视图模型类定义一致.
因为图片可以在多个用户之间共享,所以在实体框架中存在具有相应PITA查询的多对多表,其使用嵌套的from和内部联接来仅返回属于用户或与用户共享的图片:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
54894 次 |
最近记录: |