C# '的Post参数;编辑';动作总是空的
控制器:C# '的Post参数;编辑';动作总是空的,c#,asp.net-mvc,C#,Asp.net Mvc,控制器: public ActionResult Edit(int id) { SetViewData(); var rule = _db.AlertRules.Find(id); ViewData["rule"] = rule.Feed.Name + " - " + rule.Template.Name; return View(rule); } // // PO
public ActionResult Edit(int id)
{
SetViewData();
var rule = _db.AlertRules.Find(id);
ViewData["rule"] = rule.Feed.Name + " - "
+ rule.Template.Name;
return View(rule);
}
//
// POST: /AlertRule/Edit/5
[HttpPost]
public ActionResult Edit(AlertRule alertrule)
{
SetViewData();
alertrule.UpdateDateTime = DateTime.Now;
if (ModelState.IsValid)
{
_db.Entry(alertrule).State = EntityState.Modified;
_db.SaveChanges();
return RedirectToAction("Index");
}
else
{
TempData["error"] = "Cant update rule({0})"
+ alertrule.Id;
}
return View(alertrule);
}
private void SetViewData()
{
var feeds = new SelectList(_db.Feeds.OrderBy(f => f.Name), "Id", "Name");
var templates =
new SelectList(_db.AlertRuleTemplates.OrderBy(f => f.Name), "Id", "Name");
ViewData["templates"] = templates;
ViewData["feeds"] = feeds;
}
视图:
这是一个常见的问题。模型中似乎有一个
Alertrule
属性与操作参数名称冲突。因此,请重命名您的操作参数:
[HttpPost]
public ActionResult Edit(AlertRule model)
或者重命名AlertRule
属性名称以避免冲突
问题在于以下属性:
Alertrule:00000
由于操作参数的调用方式与默认模型绑定器尝试将值
00000
绑定到alertRule
参数的调用方式相同,因此显然失败。这是常见的问题。模型中似乎有一个Alertrule
属性与操作参数名称冲突。因此,请重命名您的操作参数:
[HttpPost]
public ActionResult Edit(AlertRule model)
或者重命名AlertRule
属性名称以避免冲突
问题在于以下属性:
Alertrule:00000
由于调用动作参数的方式与调用默认模型绑定器的方式相同,因此默认模型绑定器试图将值
00000
绑定到alertRule
参数,该参数显然失败。@DarinDimitrov all。post方法的参数为null,[HttpPost]公共操作结果编辑(AlertRule AlertRule)@DarinDimitrov all。post方法的参数为null,[HttpPost]公共操作结果编辑(AlertRule AlertRule)