'edit'操作的post参数始终为null

时间:2012-12-10 07:55:02

标签: 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);
    }

    //
    // 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;
    }

视图:

@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
    <legend>Edit</legend>
    @Html.HiddenFor(model => model.Id)

    <div class="editor-label">
        @Html.LabelFor(model => model.FeedId)
    </div>
    <div class="editor-field">
        @Html.DropDownListFor(model => model.FeedId, (SelectList)ViewData["feeds"])
        @Html.ValidationMessageFor(model => model.FeedId)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.TemplateId)
    </div>

    <div class="editor-field">
        @Html.DropDownListFor(model => model.TemplateId, 
              (SelectList)ViewData["templates"])
        @Html.ValidationMessageFor(model => model.TemplateId)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Enable)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Enable)
        @Html.ValidationMessageFor(model => model.Enable)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Alertrule)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Alertrule)
        @Html.ValidationMessageFor(model => model.Alertrule)
    </div>

    <div class="editor-label" style="display: none">
        @Html.LabelFor(model => model.UpdateDateTime)
    </div>
    <div class="editor-field" style="display: none">
        @Html.EditorFor(model => model.UpdateDateTime)
        @Html.ValidationMessageFor(model => model.UpdateDateTime)
    </div>

    <p>
        <input type="submit" value="Save" />
    </p>
</fieldset>
}

表单数据:

Id:33
FeedId:1
TemplateId:1
Enable:false
Alertrule:00000
UpdateDateTime:12/10/2012 3:02:15 PM

1 个答案:

答案 0 :(得分:3)

这是一个常见的问题。模型中似乎有一个Alertrule属性与您的操作参数名称冲突。所以重命名你的行动论点:

[HttpPost]
public ActionResult Edit(AlertRule model)

或重命名AlertRule属性名称以避免冲突。

问题是以下属性:

Alertrule:00000

由于您的action参数的调用方式与默认模型绑定器尝试将值00000绑定到明显失败的alertRule参数的方式相同。

相关问题