我无法从视图向控制器发送值怎么办?
这是我的剃刀页面代码:
@using (Html.BeginForm())
{
@for (int i = 0; i < Model.Reward.Count; i++)
{
@Html.EditorFor(model => Model.Reward[i].Price, new { Pric = Model.Reward[i].Price})
@Html.HiddenFor(model => Model.Reward[i].ProductCode, new { id = Model.Reward[i].ProductCode})
<br />
}
<input type="submit" value="Save" />
}
这是控制器部分:
[HttpPost]
public IActionResult Index(int? id, int? Pric)
{
if (ModelState.IsValid)
{
var Product = db.Reward.ToList();
foreach (var i in Product)
{
var rew = db.Reward.Where(x => x.ProductCode == id).FirstOrDefault();
rew.Price = Pric;
};
db.SaveChanges();
}
return View();
}
点击“保存”按钮后出现错误:
System.NullReferenceException: „Object reference not set to an instance of an object.”