【发布时间】:2020-02-28 19:49:43
【问题描述】:
有人能说出我的代码有什么问题吗?我已经搜索了几天的解决方案,但找不到任何解决方案。来自 Form 的值不会传递给视图,只有像 OrderDate、CompanyID 等“硬编码”的值才会添加到数据库中。 我正在创建我的第一个 MVC 在线商店,并为此苦苦挣扎。
编辑:要查看的数据来自 db.Selection,但我需要使用附加参数将数据保存到模型 db.Cart。也许 FormCollection 不是这样做的正确方法,但什么是?
网络控制器:
public ActionResult Index()
{
return View(db.Selection);
}
public ActionResult AddtoCart()
{
return View("AddtoCart");
}
[HttpPost]
[ValidateAntiForgeryToken()]
public ActionResult AddtoCart(FormCollection form)
{
string Author = form["Author"];
string ISBN = form["ISBN"];
string BookName = form["BookName"];
string Publisher = form["Publisher"];
string Price = form["Price"];
var cart = new Cart();
cart.Orderdate = DateTime.Now;
cart.CompanyID = 12345;
cart.ISBN = Convert.ToInt32(ISBN);
cart.BookName = BookName;
cart.Price = Convert.ToInt32(Price);
cart.IsInCart = true;
cart.Author = Author;
cart.Publisher = Publisher;
cart.SentToJvs = false;
cart.Reference = "NV kevät 1999";
db.Cart.Add(cart);
db.SaveChanges();
return RedirectToAction("Index");
}
查看
@model IEnumerable<STGchannelMVC.Models.Selection>
@using STGchannelMVC.Controllers
@{
ViewBag.Title = "Index";
}
@using (Html.BeginForm("AddtoCart", "NV", FormMethod.Post))
{
<p>
Lisää tuotteet ostoskoriin
</p>
@Html.AntiForgeryToken()
<div class="form-group">
<table class="table">
<tr>
<th hidden>
@Html.DisplayNameFor(model => model.BookID)
</th>
<th>
@Html.DisplayNameFor(model => model.ISBN)
</th>
<th>
@Html.DisplayNameFor(model => model.Author)
</th>
<th>
@Html.DisplayNameFor(model => model.BookName)
</th>
<th>
@Html.DisplayNameFor(model => model.Publisher)
</th>
<th>
@Html.DisplayNameFor(model => model.Price)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td hidden>
@Html.DisplayFor(modelItem => item.BookID)
</td>
<td>
@Html.DisplayFor(modelItem => item.ISBN)
</td>
<td>
@Html.DisplayFor(modelItem => item.Author)
</td>
<td>
@Html.DisplayFor(modelItem => item.BookName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Publisher)
</td>
<td>
@Html.DisplayFor(modelItem => item.Price)
</td>
<td>
<button type="submit" class="btn btn-primary">Add to Cart</button>
</td>
</tr>
}
</table>
</div>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
型号
public class Cart
{
public int OrderID { get; set; }
public Nullable<System.DateTime> Orderdate { get; set; }
[Display(Name = "CompanyID")]
public Nullable<long> CompanyID { get; set; }
[Display(Name = "ISBN")]
public Nullable<long> ISBN { get; set; }
[Display(Name = "BookName")]
public string BookName { get; set; }
[Display(Name = "Price")]
public Nullable<decimal> Price { get; set; }
public Nullable<bool> IsInCart { get; set; }
[Display(Name = "Author")]
public string Author { get; set; }
[Display(Name = "Publisher")]
public string Publisher { get; set; }
public Nullable<bool> SentToJvs { get; set; }
public string Reference { get; set; }
}
【问题讨论】:
-
您确定您将正确的模型传递给控制器吗?看起来它正在等待
FormCollection form -
不,我不确定 =D 我知道我的代码有问题,也许是这样。我必须看看,或者如果你有答案,请告诉我。
-
在执行您正在执行的表单时传递给控制器的模型是视图模型,在这种情况下似乎是
Selection,但控制器方法需要模型FormCollection
标签: asp.net-mvc