2012-12-10 66 views
0

控制器:之後的參數總是空

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 
+0

哪些屬性爲空? –

+0

@DarinDimitrov所有。公告的方法參數爲null,[HttpPost] public ActionResult編輯(AlertRule alertrule) –

回答

3

這是一個常見的問題。您的模型中似乎有一個Alertrule屬性,它與您的動作參數名稱衝突。因此重命名您的動作參數:

[HttpPost] 
public ActionResult Edit(AlertRule model) 

或者重命名AlertRule屬性名稱以避免衝突。

的問題是以下屬性:

Alertrule:00000 

因爲你的行動的說法被稱爲默認模型綁定嘗試值00000綁定到alertRule參數,這顯然無法以同樣的方式。

+0

哈,有意思,謝謝! –

相關問題