我打電話給我這樣的局部視圖:將參數傳遞給我的部分視圖?
<% Html.RenderPartial("~/controls/users.ascx"); %>
我可以傳遞參數給局部視圖?我如何在實際的users.ascx頁面中訪問它們?
我打電話給我這樣的局部視圖:將參數傳遞給我的部分視圖?
<% Html.RenderPartial("~/controls/users.ascx"); %>
我可以傳遞參數給局部視圖?我如何在實際的users.ascx頁面中訪問它們?
你可以通過一個模型對象的部分(例如字符串列表):
<% Html.RenderPartial("~/controls/users.ascx", new string[] { "foo", "bar" }); %>
然後你強類型的局部和Model
性能會得到相應的類型:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<System.Collections.Generic.IEnumerable<string>>" %>
<% foreach (var item in Model) { %>
<div><%= Html.Encode(item) %></div>
<% } %>
如果我創建一個具有很多屬性的類,我想我必須在我的控制器中初始化那個類,然後在視圖中將它傳遞給用戶控件? – mrblah 2009-12-15 18:08:48
您在控制器中初始化類,傳遞給視圖作爲模型,並且視圖渲染部分重新傳遞模型。 – 2009-12-15 18:15:49
RenderPartial還有另外一個重載模塊可以讓你的模型通過。
<% Html.RenderPartial("~/controls/users.ascx", modelGoesHere); %>
如何訪問?就像你通常會與任何觀點:
<%= Model.MagicSauce %>
+1 MagicSauce使用 – 2012-04-19 05:17:40
過了好一會兒下沉,但MVC意味着你的使用模型,視圖和控制器這樣或那樣的只是對一切g,包括部分視圖。一開始,所有這三個元素如何融合在一起可能有點嚇人。我從來沒有做過,直到現在,它的工作原理 - 嗚呼!
希望這可以幫助下一個人......對不起,我使用剃刀而不是.Net形式。我還將數據從SQL Server數據庫提取到開發人員可能使用的實體框架中。我也可能用WebGrid做了一點小事,它比foreach語句更優雅。一個基本的@ webgrid.GetHtml()將顯示每一列和每一行。
在此工作示例中,用戶已上傳圖片。他們的照片使用局部視圖以編輯形式顯示。 ImageID和FileName元數據在SQL Server中保存,而文件本身保存在〜/ Content/UserPictures目錄中。
我知道這有點兒大,因爲沒有顯示上傳和編輯個人數據的所有細節。剛開始使用的局部視圖的鍺部分都集中在,儘管有一些獎金EF拋出該命名空間是MVCApp3對於s & G.
局部視圖模型 ViewModels.cs
的SQL服務器映像除了ImageID和FileName之外,表格還包含更多列,例如[Caption],[Description],MD5哈希以防止多次上傳相同的圖像以及上傳日期。 ViewModel將實體蒸餾至用戶看到他們的圖片所需的最低限度。
public class Picts
{
public int ImageID { get; set; }
public string FileName { get; set; }
}
主視圖查看 Edit.cshtml
注投/轉換爲強類型的計算機[]。
@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」]與部分vi ew文件名及其視圖模型類定義。
因爲圖片可能會在多個用戶之間共享,所以在實體框架中有一個多對多的表格,使用嵌套的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);
}
}
你搖滾很棒。你用一種真正點擊的方式將所有這些拼出來,從而避免我錯過最後期限。我<3你花時間去做這一切!謝謝!!! – divamatrix 2013-06-11 19:33:16
// 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);
}
//這裏的局部視圖控制器 - 不是太多了! 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);
}
}
密切相關的問題:http://stackoverflow.com/questions/6549541/how-to-pass-parameters-to-a-partial-view-in-asp- net-mvc – 2013-03-18 23:25:39