我想在我的控制器中建立一個請求的動作,它將接受表格的請求:Product/Create/[Category ID]
。URL的ID值不會持續獲取「產品/創建/ [類別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);
}
您也可以在創建操作的的GetRequest,我試着堅持將ID爲模型的CategoryId
屬性看。 Create Action的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;
}
}
難道你需要將視圖模型轉換爲一個域對象,而不是'category.Products.Add(viewModel);'? –
我有隱式轉換方法在ViewModel類中進行轉換。我已將它列入我更新的問題中。 – naz786
你確定'CategoryId'正在你的'Index'控制器方法中被填充嗎? –