我有一个简单的表 - 使用 SqlCommand 语句有选择地查询,然后使用 SqlDataReader 读入 HTML 表。我希望能够通过使用简单的 HTML 表单(从控制器获取其功能 - 即:UpdateProductPage 和 UpdateProduct)来更新表格内容。我正在使用包装视图模型来将产品与其特定类型相关联。但是,我似乎无法打开包含用于更新内容的 HTML 表单的视图,因为我被以下错误阻止:
“传递到字典中的模型项的类型为‘Homework_5.ViewModels.WrapperViewModel’,但此字典需要‘System.Collections.Generic.List`1[Homework_5.ViewModels.ProductViewModel]’类型的模型项”
我正在努力理解这个错误。
这是我的包装视图模型:
namespace Homework_5.ViewModels
{
public class WrapperViewModel
{
public ProductViewModel product;
public List<TypeViewModel> typeList = new List<TypeViewModel>();
}
}
UpdateProductPage 控制器 - 确实有效(使用断点测试) - 永远不会遇到问题:
//Update an existing product in the database
public ActionResult UpdateProductPage(int id)
{
WrapperViewModel updateModel = new WrapperViewModel();
updateModel.product = Global.productList.Where(p => p.ProductID == id).FirstOrDefault();
try
{
SqlCommand webstoreCommand = new SqlCommand("Select * from ProductType", webstoreConnection);
webstoreConnection.Open();
SqlDataReader webstoreReader = webstoreCommand.ExecuteReader();
while (webstoreReader.Read())
{
TypeViewModel type = new TypeViewModel();
type.TypeID = (int)webstoreReader["ID"];
type.TypeName = webstoreReader["TypeName"].ToString();
updateModel.typeList.Add(type);
}
}
catch (Exception)
{
ViewBag.Status = 0;
}
finally
{
webstoreConnection.Close();
}
return View(updateModel);
}
UpdateProduct 控制器 - 永远不会运行(根据断点 - 在到达此控制器之前抛出异常:
[HttpPost]
public ActionResult UpdateProduct(int Id, string Description, double Price, int Quantity, string Image, int TypeID)
{
try
{
SqlCommand webstoreUpdateCommand = new SqlCommand("Update Product Set Description = '" + Description + "', Price = '" + Price + "', Quantity = '" + Quantity + " where ID = " + Id, webstoreConnection);
webstoreConnection.Open();
}
catch (Exception)
{
ViewBag.Status = 0;
}
finally
{
webstoreConnection.Close();
}
return RedirectToAction("ViewProductPage");
}
HTML 表单(带有必要的输入 - 应该预先填写相应的信息:
model Homework_5.ViewModels.WrapperViewModel
@{
ViewBag.Title = "UpdateProductPage";
}
<h2>Update Selected Product - ID Value: @Model.product.ProductID</h2>
<div class="update-product-contents">
@using (Html.BeginForm("UpdateProduct", "AdminProduct", FormMethod.Post))
{
@Html.Hidden("Id", Model.product.ProductID, new { @class = "form-control" });
<label>Description:</label>
@Html.TextBox("Description", Model.product.Description, new { @class = "form-control", @type = "text", required = "required" });
<label>Price:</label>
@Html.TextBox("Price", Model.product.Price, new { @class = "form-control", @type = "number", required = "required" });
<label>Quantity:</label>
@Html.TextBox("Quantity", Model.product.Quantity, new { @class = "form-control", @type = "number", required = "required" });
<label>Image:</label>
@Html.TextBox("Image", Model.product.ImagePath, new { @class = "form-control", @type = "file", required = "required" });
<label>Product Type:</label>
@Html.DropDownList("TypeID", new SelectList(Model.typeList, "TypeID", "TypeName", Model.product.Type.TypeID), new { @class = "form-control", required = "required" });
<button class="btn btn-outline-dark" type="submit">Add to product database</button>
}
</div>
最后是打开更新页面的按钮:
<a href="@Url.Action("UpdateProductPage", "AdminProduct", new { id = product.ProductID })">
<i class="fas fa-edit" id="edit-icon"></i>
</a>