我正试图在我的控制器中为一个请求构建一个操作,该操作将接受形式为Product/Create/[Category ID]
的请求。
当用户在Product/Index/[Category Id]
页面上时,他们可以看到该类别ID特定的所有产品。
他们可以点击"创建"按钮来处理Get
请求:Product/Create/[Category ID]
。我需要将上一个请求中的Category ID
持久化到这个PostRequest中,以便为特定的Category ID
创建产品。
我的控制器的创建操作如下所示:
public ActionResult Create(int? id)
{
if (id == null)
{
return HttpNotFound();
}
return View(new ProductViewModel{CategoryId = id.Value});
}
// POST: Product/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Create([Bind(Include = "Id,Name,Image,Price,CategoryId")] ProductViewModel viewModel)
{
if (ModelState.IsValid && viewModel.Image != null)
{
var category = await db.Categories.FindAsync(viewModel.CategoryId);
if (category == null)
{
return HttpNotFound();
}
category.Products.Add(viewModel);
await db.SaveChangesAsync();
return RedirectToAction("Index/" + viewModel.CategoryId);
}
return View(viewModel);
}
您还可以在Create Action的GetRequest中看到,我试图将id持久化到模型的CategoryId
属性中。创建操作的PostRequest中的模型应该包含它从GetRequest获得的CategoryId
的值。
Product/Index/[Category ID]
视图中的模型是一个列表:
@model List<ValueVille.Models.ProductViewModel>
@using MvcApplication8.Models
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p class="btn btn-default">
@Html.ActionLink("Create New", "Create", new { id = Model[0].CategoryId})
</p>
<table class="panel panel-default table">
<tr>
<th>
@Html.DisplayNameFor(model => model[0].Name)
</th>
<th>
@Html.DisplayNameFor(model => model[0].Image)
</th>
<th>
@Html.DisplayNameFor(model => model[0].Price)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.Image(item.ByteImage, "Product_Image", "100")
</td>
<td>
@Html.DisplayFor(modelItem => item.Price)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
@Html.ActionLink("Details", "Details", new { id=item.Id }) |
@Html.ActionLink("Delete", "Delete", new { id=item.Id })
</td>
</tr>
}
</table>
<div>
@Html.ActionLink("Back to List", "Index", new { Controller = "Category" }, new { id = Model[0].Id})
</div>
在同一视图中,Create
ActionLink如下所示:
<p class="btn btn-default">
@Html.ActionLink("Create New", "Create", new { id = Model[0].CategoryId})
</p>
在浏览器中,id
总是给出值0,所以当从Product/Index/[Category ID]
页面单击Create
时,我总是被带到Product/Create/0
。
具有转换方法的ProductViewModel类:
public class ProductViewModel
{
public int Id { get; set; }
[Required, Display(Name="Product Name")]
public string Name { get; set; }
[DataType(DataType.Upload)]
public HttpPostedFileBase Image { get; set; }
public string OutputImage { get; set; }
public Byte[] ByteImage { get; set; }
[Required]
public Decimal Price { get; set; }
public int CategoryId { get; set; }
public static byte[] ConvertToByte(ProductViewModel model)
{
if (model.Image != null)
{
byte[] imageByte = null;
BinaryReader rdr = new BinaryReader(model.Image.InputStream);
imageByte = rdr.ReadBytes((int)model.Image.ContentLength);
return imageByte;
}
return null;
}
// ViewModel => Model | Implicit type Operator
public static implicit operator Product(ProductViewModel viewModel)
{
var model = new Product
{
Id = viewModel.Id,
Name = viewModel.Name,
Image = ConvertToByte(viewModel),
Price = viewModel.Price
};
return model;
}
// Model => ViewModel | Implicit type Operator
public static implicit operator ProductViewModel(Product model)
{
var viewModel = new ProductViewModel
{
Id = model.Id,
Name = model.Name,
OutputImage = string.Format("data:image/jpg;base64,{0}", Convert.ToBase64String(model.Image)),
ByteImage = model.Image,
Price = model.Price,
CategoryId = model.Id
};
return viewModel;
}
}
无论出于何种原因,似乎都没有填充CategoryId
。我建议您修改索引页面的视图模型,将类别id明确定义为一个单独的属性:
public class ProductIndexViewModel
{
public IList<ValueVille.Models.ProductViewModel> Products;
public int? CategoryId
}
在索引操作方法中显式设置CategoryId
值。
将索引视图的模型定义替换为:
@model ProductIndexViewModel
创建动作链接:
<p class="btn btn-default">
@Html.ActionLink("Create New", "Create", new { id = Model.CategoryId })
</p>
和产品循环:
@foreach (var item in Model.Products) {
请将Index.cshtml
的其余视图代码包括在问题中
我怀疑问题出在new { id = Model[0].CategoryId }
上。假设您在Index页面上以某种形式的循环显示所有产品的列表,请尝试使用本地产品参考,而不是模型上的索引:
@foreach (var p in Model)
{
...
<p class="btn btn-default">
@Html.ActionLink("Create New", "Create", new { id = p.CategoryId })
</p>
...
}