无法在主视图中添加部分视图 [英] Trouble to add Partial View in Main View
问题描述
我想创建一个局部视图并将其与主视图合并。但是,我的代码出了问题。任何人都可以通过查看我给出的代码为我解释: -
在我的产品控制器中,我为上传文件/图像编写了这些代码:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult创建(产品)
{
foreach ( string file Request.Files)
{
var postedFile = Request.Files [file];
postedFile.SaveAs(Server.MapPath( 〜/ UploadedFiles /)+ Path。用GetFileName(postedFile.FileName));
}
if (ModelState.IsValid)
{
db.Products.Add(product) ;
db.SaveChanges();
return RedirectToAction( Index跨度>);
}
ViewBag.CategoryId = new SelectList(db.Categorys, CategoryId, 名称,product.CategoryId);
ViewBag.SubCategoryId = new SelectList(db.SubCategories, SubCategoryId, SubCategoryName,product.SubCategoryId );
ViewBag.ModelId = new SelectList(db.Models, ModelId, ModelName,product.ModelId );
return 查看(产品);
}
然后显示文件/图像我只是在我的产品控制器中写下这些代码
< pre lang =c#> public ActionResult List()
{
var uploadedFiles = new List< Product>();
var files = Directory.GetFiles(Server.MapPath( 〜/ UPLOADEDFILES跨度>));
foreach ( var file 在文件中)
{
var fileInfo = new FileInfo(文件);
var product = new Product(){Name = Path.GetFileName(文件)};
product.ImageSize = fileInfo.Length;
product.Path =( 〜/ UploadedFiles /)+路径.GetFileName(文件);
uploadedFiles.Add(product);
}
return 查看(uploadedFiles);
}
public ViewResult _ProductList()
{
return 查看( _ ProductList);
}
产品的索引视图控制器很喜欢
@model IEnumerable< Online_Shopping_Management.Models.Product>
@ {
ViewBag.Title = 索引;
}
< h2>索引< / h2 >
< p>
@ Html.ActionLink( Create new, 创建)
< / p >
< table>
< tr>
< th>
@ Html.DisplayNameFor(model = > model.Category.Name)
< / th >
< th>
@ Html.DisplayNameFor(model = > model.SubCategory.SubCategoryName)
< / th >
< th>
@ Html.DisplayNameFor(model = > model.Model.ModelName)
< / th >
< th>
@ Html.DisplayNameFor(model = > model.ProductName)
< / / span> th >
< th>
@ Html.DisplayNameFor(model = > model.Image)
< / / span> th >
< / tr >
@foreach( var item in Model){
< tr>
< td>
@ Html.DisplayFor(modelItem = > item.Category.Name)
< / td >
< td>
@ Html.DisplayFor(modelItem = > item.SubCategory.SubCategoryName)
< / td >
< td>
@ Html.DisplayFor(modelItem = > item.Model.ModelName)
< / td >
< td>
@ Html.DisplayFor(modelItem = > item.ProductName)
< / / span> td >
< td>
@ Html.Partial( 〜/ Views / Shared / _ProductList.cshtml)
< / td >
< td>
@ Html.ActionLink( 编辑, 编辑, new {id = item.ProductId})|
@ Html.ActionLink( 详细信息, 详细信息, new {id = item.ProductId})|
@ Html.ActionLink( 删除, 删除, new {id = item.ProductId})
< / td >
< / tr >
}
< / table >
我的部分视图型号喜欢:
@ model IEnumerable< Online_Shopping_Management.Models.Product>
@ {
ViewBag.Title = List;
}
< h2>列表< / h2 >
< table>
< tr>
< td>产品名称< / td >
< td>图片名称< / td >
< td>预览< / td >
< / tr >
@foreach( var file in Model)
{
< TR>
< td> @ file.ProductName < / td >
< td> @ file.Name < / td >
< td>
< img src = @ Url.Content(file.Path) /> ;
< / td >
< / tr >
}
< / table < span class =code-keyword>>
现在创建产品之后我想显示索引视图,然后浏览器不显示任何响应,并在项目中显示错误行显示
值不能为null或为空。\\\ nParameter name: contentPath
在产品控制器的索引视图中添加一行
@Html。的RenderPartial( MyView的,模型)
I want create a partial view and merge it with main view. But, something is going wrong with my code. Can anyone explain it for me by seeing my given code:-
In my Product Controller I write those code for upload File/Image :
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Product product)
{
foreach (string file in Request.Files)
{
var postedFile = Request.Files[file];
postedFile.SaveAs(Server.MapPath("~/UploadedFiles/") + Path.GetFileName(postedFile.FileName));
}
if (ModelState.IsValid)
{
db.Products.Add(product);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.CategoryId = new SelectList(db.Categorys, "CategoryId", "Name", product.CategoryId);
ViewBag.SubCategoryId = new SelectList(db.SubCategories, "SubCategoryId", "SubCategoryName", product.SubCategoryId);
ViewBag.ModelId = new SelectList(db.Models, "ModelId", "ModelName", product.ModelId);
return View(product);
}
Then showing File/Image I just write those code in my Product Controller
public ActionResult List()
{
var uploadedFiles = new List<Product>();
var files = Directory.GetFiles(Server.MapPath("~/UploadedFiles"));
foreach (var file in files)
{
var fileInfo = new FileInfo(file);
var product = new Product() { Name = Path.GetFileName(file) };
product.ImageSize = fileInfo.Length;
product.Path = ("~/UploadedFiles/") + Path.GetFileName(file);
uploadedFiles.Add(product);
}
return View(uploadedFiles);
}
public ViewResult _ProductList()
{
return View("_ProductList");
}
My Index view of Product controller is likes
@model IEnumerable<Online_Shopping_Management.Models.Product>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.Category.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.SubCategory.SubCategoryName)
</th>
<th>
@Html.DisplayNameFor(model => model.Model.ModelName)
</th>
<th>
@Html.DisplayNameFor(model => model.ProductName)
</th>
<th>
@Html.DisplayNameFor(model => model.Image)
</th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Category.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.SubCategory.SubCategoryName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Model.ModelName)
</td>
<td>
@Html.DisplayFor(modelItem => item.ProductName)
</td>
<td>
@Html.Partial("~/Views/Shared/_ProductList.cshtml")
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.ProductId }) |
@Html.ActionLink("Details", "Details", new { id=item.ProductId }) |
@Html.ActionLink("Delete", "Delete", new { id=item.ProductId })
</td>
</tr>
}
</table>
My Partial View Model is likes :
@model IEnumerable<Online_Shopping_Management.Models.Product>
@{
ViewBag.Title = "List";
}
<h2>List</h2>
<table>
<tr>
<td> Product Name </td>
<td> Image Name </td>
<td> Preview </td>
</tr>
@foreach (var file in Model)
{
<tr>
<td> @file.ProductName</td>
<td> @file.Name </td>
<td>
<img src="@Url.Content(file.Path)" />
</td>
</tr>
}
</table>
Now after Create Product when I want to show 'Index' view then browser doesn't show any response and following error line shows in project
"Value cannot be null or empty.\r\nParameter name: contentPath"
Add a line after in Index view of Product controller
@Html.RenderPartial("myview",Model)
这篇关于无法在主视图中添加部分视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!